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)更正此问题,但原始问题仍然存在。
3 个解决方案
#1
40
dot
is matrix multiplication, but *
does something else.
dot是矩阵乘法,但*做其他事情。
We have two arrays:
我们有两个数组:
-
X
, shape (97,2) - X,形状(97,2)
-
y
, shape (2,1) - 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)
#2
19
Per Wes McKinney's Python for Data Analysis
Per Wes McKinney的Python数据分析
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.
广播规则:如果对于每个尾随维度(即,从结尾开始),轴长度匹配或者如果任一长度为1,则两个阵列可用于广播。然后在缺失和/或长度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]])
#3
3
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.
这个错误具有误导性;但是那条线上有一个问题。
#1
40
dot
is matrix multiplication, but *
does something else.
dot是矩阵乘法,但*做其他事情。
We have two arrays:
我们有两个数组:
-
X
, shape (97,2) - X,形状(97,2)
-
y
, shape (2,1) - 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)
#2
19
Per Wes McKinney's Python for Data Analysis
Per Wes McKinney的Python数据分析
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.
广播规则:如果对于每个尾随维度(即,从结尾开始),轴长度匹配或者如果任一长度为1,则两个阵列可用于广播。然后在缺失和/或长度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]])
#3
3
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.
这个错误具有误导性;但是那条线上有一个问题。