python numpy ValueError:操作数无法与形状一起广播
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24560298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python numpy ValueError: operands could not be broadcast together with shapes
提问by yayu
In numpy, I have two "arrays", X
is (m,n)
and y
is a vector (n,1)
在 numpy 中,我有两个“数组”,X
是(m,n)
和y
是一个向量(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
何时 (97,2)x(2,1)
显然是合法的矩阵运算,应该给我一个(97,1)
向量
EDIT:
编辑:
I have corrected this using X.dot(y)
but the original question still remains.
我已使用此方法进行了更正,X.dot(y)
但原始问题仍然存在。
回答by DrV
dot
is matrix multiplication, but *
does something else.
dot
是矩阵乘法,但*
做其他事情。
We have two arrays:
我们有两个数组:
X
, shape (97,2)y
, shape (2,1)
X
, 形状 (97,2)y
, 形状 (2,1)
With Numpy arrays, the operation
使用 Numpy 数组,操作
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.
是按元素完成的,但可以在一维或多维中扩展其中一个或两个值以使其兼容。此操作称为广播。尺寸为 1 或缺失的维度可用于广播。
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.
这里的第一维(97 和 2)中存在相互矛盾的数字。这就是上面的 ValueError 所抱怨的。第二个维度没问题,因为数字 1 与任何内容都不冲突。
For more information on broadcasting rules: http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html
有关广播规则的更多信息: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.)
(请注意,如果X
和y
是 类型numpy.matrix
,那么星号可以用作矩阵乘法。我的建议是远离numpy.matrix
,它往往会使事情复杂化而不是简化。)
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:
你的数组应该没问题numpy.dot
;如果您在 上遇到错误numpy.dot
,则您一定有其他错误。如果 的形状错误numpy.dot
,则会出现不同的异常:
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)
回答by gobrewers14
Per numpy docs:
每个numpy 文档:
When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when:
- they are equal, or
- one of them is 1
在对两个数组进行操作时,NumPy 按元素比较它们的形状。它从尾随维度开始,然后继续前进。两个维度在以下情况下兼容:
- 他们是平等的,或者
- 其中之一是 1
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
.
换句话说,如果您尝试将两个矩阵相乘(在线性代数意义上)那么您想要,X.dot(y)
但是如果您尝试将标量从矩阵广播y
到X
那么您需要执行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]])
回答by Kenan
It's possible that the error didn't occur in the dot product, 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 (12x1 X 1x5 = 12x5 which cannot element-wise multiply 5x12) but numpy will give you
np.dot(a,b) 没问题;但是 np.dot(a, b) * c 显然是错误的(12x1 X 1x5 = 12x5 不能逐元素乘以 5x12)但是 numpy 会给你
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.
该错误具有误导性;但是那条线上有一个问题。
回答by user3101695
Use np.mat(x) * np.mat(y)
, that'll work.
使用np.mat(x) * np.mat(y)
,那会起作用。
回答by iamanigeeit
You are looking for np.matmul(X, y)
. In Python 3.5+ you can use X @ y
.
您正在寻找np.matmul(X, y)
. 在 Python 3.5+ 中,您可以使用X @ y
.
回答by chia yongkang
We might confuse ourselves that a * b is a dot product.
我们可能会混淆 a * b 是点积。
But in fact, it is broadcast.
但实际上,它是广播。
Dot Product :a.dot(b)
点积:a.dot(b)
Broadcast:
播送:
The term broadcasting refers to how numpy treats arrays with different dimensions during arithmetic operations which lead to certain constraints, the smaller array is broadcast across the larger array so that they have compatible shapes.
术语广播是指 numpy 在算术运算期间如何处理不同维度的数组,这会导致某些约束,较小的数组在较大的数组中广播,以便它们具有兼容的形状。
(m,n) +-/* (1,n) → (m,n) : the operation will be applied to m rows
(m,n) +-/* (1,n) → (m,n) :操作将应用于 m 行