简体   繁体   中英

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.

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]])

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