numpy matrix multiplication shapes
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.
jeffery_the_wind
Currently a Doctoral student in the Dept. of Math and Stats at Université de Montréal. In the past I have done a lot of full stack development and applied math. Now trying to focus more on the pure math side of things and theory. Always get a lot of help from the Stack Exchange Community! Math interests include optimization and Algebra although in practice I do a lot of machine learning.
Updated on June 05, 2022Comments
-
jeffery_the_wind almost 2 years
In matrix multiplication, assume that the
A
is a 3 x 2 matrix (3 rows, 2 columns ) andB
is a 2 x 4 matrix (2 rows, 4 columns ), then if a matrixC = A * B
, thenC
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?
-
Guillaume over 9 yearsOne 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.
-
Charlie Parker almost 7 yearswhen should one use in numpy matrices vs arrays? Until recently I wasn't even aware that there was a matrix API.
-
Daniel almost 7 years@CharlieParker I would not recommend using matrices, I believe they are slated to be deprecated.