Marcello S
Marcello S

Reputation: 331

Numpy Broadcasting

What happens when i make this operation in Numpy?

a = np.ones([500,1])
b = np.ones([5000,])/2
c = a + b 

# a.shape (500,1)
# b.shape (5000, )
# c.shape (500, 5000)

I'm having a hard time to figure out what is actually happening in this broadcast.

Upvotes: 1

Views: 689

Answers (1)

Daniel
Daniel

Reputation: 42748

Numpy assumes for 1 dimensional arrays row vectors, so your summation is indeed between shapes (500, 1) and (1, 5000), which leads to matrix summation.

Since this is not very clear, you should extend your dimensions explicitly:

>>> np.arange(5)[:, None] + np.arange(8)[None, :]
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 1,  2,  3,  4,  5,  6,  7,  8],
       [ 2,  3,  4,  5,  6,  7,  8,  9],
       [ 3,  4,  5,  6,  7,  8,  9, 10],
       [ 4,  5,  6,  7,  8,  9, 10, 11]])

Upvotes: 1

Related Questions