简体   繁体   中英

How is broadcasting applying in this example of numpy?

I am learning numpy and am a bit confused about broadcasting, here is my set up. I have two matrices

>>> y=np.array([1,2,3])
>>> v = np.array([1,2,3])
>>> r=np.reshape(v, (3, 1))

So r is (3*1) matrix while y is a rank 1 matrix with shape being(3,).

If I do y.dot(r), I get 14, assuming that numpy applies broadcasting on y, making it (1*3) and then it does the dot product with r(3*1) so resulting matrix will be 1*1.

However when I do r.dot(y), it throws an error. Why doesn't it do the same thing here? It should make y(1*3) and r being(3*1), it should give a 3*3 matrix. What is wrong with this reasoning?

The normal broadcasting does not apply in np.dot . It's docs say:

For N dimensions it is a sum product over the last axis of a and the second-to-last of b ::

y is (3,) ; r is (3,1) .

In y*r , broadcasting applies, y is reshaped to (1,3) , and the result is (3,3) .

In np.dot(y,r) , the last axis of y is 3 , 2nd last of r is also 3 , multiply and sum, the shape is (1,) . Note that if y starts as (1,3) , the result is 2d:

In [445]: np.dot(y.reshape(1,3),r).shape
Out[445]: (1, 1)

In np.dot(r,y) , last axis of r is 1 , 2nd to last (only) of y is 3 - hence the mismatch.

Expanding y does produce the (3,3) :

In [449]: np.dot(r,y.reshape(1,3))
Out[449]: 
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

It's just like matrix multiplication on paper where the two inner dimensions need to agree. So if you want a 3x3 not only does r need to be of shape (3,1) but y needs to be of shape (1,3) not (3,)

Like so:

In [60]: r.dot(y.reshape(1,-1))
Out[60]: 
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

I feel like numpy should infer this, but I guess "explicit is better than implicit"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM