numpy matrix multiplication shapes [duplicate]

旧街凉风 提交于 2019-11-30 17:09:50

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/18255583/numpy-matrix-multiplication-shapes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!