操作数不能与形状 (5,2) (1,5) 一起广播

operands could not be broadcast together with shapes (5,2) (1,5)

我想将这两个 np.array 相乘,我相信它应该 return a (5,2) np.array,但它会引发错误。

a = np.array([1, 2, 3, 4, 5])
b = np.arange(10)
b = b.reshape((5,2))
print(a.shape, b.shape)
print(b * a)

如果你想在每一列上按元素相乘 a 和 b,你需要添加

a = a.reshape((5,1))