python numpy ValueError: operands could not be broadcast together with shapes

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

In numpy, I have two "arrays", X is (m,n) and y is a vector (n,1)

using

X*y 

I am getting the error

ValueError: operands could not be broadcast together with shapes (97,2) (2,1)  

When (97,2)x(2,1) is clearly a legal matrix operation and should give me a (97,1) vector

EDIT:

I have corrected this using X.dot(y) but the original question still remains.

回答1:

dot is matrix multiplication, but * does something else.

We have two arrays:

  • X, shape (97,2)
  • y, shape (2,1)

With Numpy arrays, the operation

X * y 

is done element-wise, but one or both of the values can be expanded in one or more dimensions to make them compatible. This operation are called broadcasting. Dimensions where size is 1 or which are missing can be used in broadcasting.

In the example above the dimensions are incompatible, because:

97   2  2   1 

Here there are conflicting numbers in the first dimension (97 and 2). That is what the ValueError above is complaining about. The second dimension would be ok, as number 1 does not conflict with anything.

For more information on broadcasting rules: http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html

(Please note that if X and y are of type numpy.matrix, then asterisk can be used as matrix multiplication. My recommendation is to keep away from numpy.matrix, it tends to complicate more than simplify things.)

Your arrays should be fine with numpy.dot; if you get an error on numpy.dot, you must have some other bug. If the shapes are wrong for numpy.dot, you get a different exception:

ValueError: matrices are not aligned 

If you still get this error, please post a minimal example of the problem. An example multiplication with arrays shaped like yours succeeds:

In [1]: import numpy  In [2]: numpy.dot(numpy.ones([97, 2]), numpy.ones([2, 1])).shape Out[2]: (97, 1) 


回答2:

Per Wes McKinney's Python for Data Analysis

The Broadcasting Rule: Two arrays are compatable for broadcasting if for each trailing dimension (that is, starting from the end), the axis lengths match or if either of the lengths is 1. Broadcasting is then performed over the missing and/or length 1 dimensions.

In other words, if you are trying to multiply two matrices (in the linear algebra sense) then you want X.dot(y) but if you are trying to broadcast scalars from matrix y onto X then you need to perform X * y.T.

Example:

>>> import numpy as np >>> >>> X = np.arange(8).reshape(4, 2) >>> y = np.arange(2).reshape(1, 2)  # create a 1x2 matrix >>> X * y array([[0,1],        [0,3],        [0,5],        [0,7]]) 


回答3:

It's possible that the error didn't occur in the dot but after. For example try this

a = np.random.randn(12,1) b = np.random.randn(1,5) c = np.random.randn(5,12) d = np.dot(a,b) * c 

np.dot(a,b) will be fine; however np.dot(a,b) * c is clearly wrong (12x5 X 5x5 = 12x5 which cannot element-wise multiply 5x12) but numpy will give you

ValueError: operands could not be broadcast together with shapes (12,1) (1,5) 

The error is misleading; however there is an issue on that line.



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