5

In matrix multiplication, assume that the A is a 3 x 2 matrix (3 rows, 2 columns ) and B is a 2 x 4 matrix (2 rows, 4 columns ), then if a matrix C = A * B, then C should have 3 rows and 4 columns. Why does numpy not do this multiplication? When I try the following code I get an error : ValueError: operands could not be broadcast together with shapes (3,2) (2,4)

a = np.ones((3,2))
b = np.ones((2,4))
print a*b

I try with transposing A and B and alwasy get the same answer. Why? How do I do the matrix multiplication in this case?

jeffery_the_wind
  • 17,048
  • 34
  • 98
  • 160

1 Answers1

19

The * operator for numpy arrays is element wise multiplication (similar to the Hadamard product for arrays of the same dimension), not matrix multiply.

For example:

>>> a
array([[0],
       [1],
       [2]])
>>> b
array([0, 1, 2])
>>> a*b
array([[0, 0, 0],
       [0, 1, 2],
       [0, 2, 4]])

For matrix multiply with numpy arrays:

>>> a = np.ones((3,2))
>>> b = np.ones((2,4))
>>> np.dot(a,b)
array([[ 2.,  2.,  2.,  2.],
       [ 2.,  2.,  2.,  2.],
       [ 2.,  2.,  2.,  2.]])

In addition you can use the matrix class:

>>> a=np.matrix(np.ones((3,2)))
>>> b=np.matrix(np.ones((2,4)))
>>> a*b
matrix([[ 2.,  2.,  2.,  2.],
        [ 2.,  2.,  2.,  2.],
        [ 2.,  2.,  2.,  2.]])

More information on broadcasting numpy arrays can be found here, and more information on the matrix class can be found here.

Daniel
  • 19,179
  • 7
  • 60
  • 74
  • One should be careful with the sparse.linalg numpy extension that defines the "LinearOperator" class. In this class, the "*" operator is interpreted as the usual matrix dot product. – Guillaume Aug 26 '14 at 15:28
  • when should one use in numpy matrices vs arrays? Until recently I wasn't even aware that there was a matrix API. – Charlie Parker Aug 14 '17 at 23:38
  • @CharlieParker I would not recommend using matrices, I believe they are slated to be deprecated. – Daniel Aug 15 '17 at 14:19