NumPy 入门
Izzy Weber
Core Curriculum Manager, DataCamp




可广播的形状:
(10, 5) 与 (10, 1)(10, 5) 与 (5, )
不可广播的形状:
(10, 5) 与 (5, 10)(10, 5) 与 (10, )
array = np.arange(10).reshape((2, 5))
array + np.array([0, 1, 2, 3, 4])
array([[ 0, 2, 4, 6, 8],
[ 5, 7, 9, 11, 13]])


array = np.arange(10).reshape((2, 5))
array + np.array([0, 1])
ValueError: operands could not be broadcast together with shapes (2,5) (2,)

array = np.arange(10).reshape((2, 5))
array + np.array([0, 1]).reshape((2, 1))
array([[ 0, 1, 2, 3, 4],
[ 6, 7, 8, 9, 10]])



NumPy 入门