Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 919
Dude Avatar asked Mar 16 '23 10:03

Dude


1 Answers

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]])
like image 79
hpaulj Avatar answered Mar 25 '23 07:03

hpaulj