Introduction to NumPy
Izzy Weber
Core Curriculum Manager, DataCamp
Broadcastable shapes:
(10, 5)
and (10, 1)
(10, 5)
and (5, )
Shapes which are not broadcastable:
(10, 5)
and (5, 10)
(10, 5)
and (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]])
Introduction to NumPy