[英]Numpy Broadcasting
What happens when i make this operation in Numpy? 当我在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. Numpy假定一维数组为行向量,因此您的求和确实在形状(500,1)和(1,5000)之间,这导致矩阵求和。
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]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.