Reputation: 24151
If I run:
x = np.zeros(6)
y = np.zeros([7, 6])
z = y * x
Then everything is fine, and there are no Python errors.
However, I am using a Python module (call if foo) containing a function (call if bar), which returns a 7x6 NumPy array. It has the same shape as y
above, and the same data type (float64). But when I run the following:
x = np.zeros(6)
y = foo.bar()
z = y * x
I get the following error:
ValueError: shapes (7,6) and (1,6) not aligned: 6 (dim 1) != 1 (dim 0)
But as far as I can tell, y
is exactly the same format in these two examples, with the same shape and data type. What's causing this error, and why is it not caused in the first example?
Upvotes: 0
Views: 529
Reputation: 231665
In [446]: x = np.zeros(6)
...: y = np.zeros([7, 6])
...: z = y * x
In [447]: z.shape
Out[447]: (7, 6)
Here we are doing element-wise multiplication, a (7,6) with a (6,). By broadcasting the (6,) becomes (1,6) and then (7,6) to match y
.
Evidently in the foo.bar
case, y
is np.matrix
subclass:
In [454]: y1 = np.matrix(y)
In [455]: y1*x
---...
219 # This promotes 1-D vectors to row vectors
--> 220 return N.dot(self, asmatrix(other))
...
ValueError: shapes (7,6) and (1,6) not aligned: 6 (dim 1) != 1 (dim 0)
Note the different display for y1
:
In [456]: y1
Out[456]:
matrix([[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])
With np.matrix
*
is defined as np.dot
, the matrix product. The x
is also converted np.matrix
, producing a (1,6) matrix. The error message follows from the definition of matrix multiplication.
np.multiply
can be used force the element-wise multiplication. Note the class of the result:
In [458]: np.multiply(y1,x)
Out[458]:
matrix([[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])
Because of confusions like this np.matrix
is being discouraged.
Upvotes: 1
Reputation: 339
I don't know which version you're running, but I am running version 1.16.3 on Python 3.
It seems to me you're defining your x
differently than in your example snippet. You seem to be defining it as a 6x1 matrix instead of a "vector", which is considered on Numpy to only have one dimension. Try multiplying y
by np.zeros([6,1])
and you'll see an error.
Bottom line is:
.shape
property a lot when debugging, it's very useful when you're doing matrix multiplication.Upvotes: 1