Numpy Broadcasting

Broadcasting is a powerful feature in NumPy that allows you to perform arithmetic operations on arrays of different shapes. It essentially stretches the smaller array to match the dimensions of the larger array, enabling element-wise operations. This avoids the need for explicit looping and makes the code concise and efficient.

Let’s study it using some examples:

Example 1: Adding a scalar to a 2D array

Python

import numpy as np

scalar = 5
array = np.array([[1, 2, 3], [4, 5, 6]])

# Broadcasting adds the scalar to each element of the array
result = scalar + array
print(result)
Output:
[[6 7 8]
 [9 10 11]

In this example, the scalar 5 is added to each element of the 2D array array. Broadcasting automatically stretches the scalar to match the shape of the array, resulting in the output array.

Example 2: Multiplying a 1D array with a 2D array

vector = np.array([1, 2, 3])
array = np.array([[1, 2, 3], [4, 5, 6]])

# Broadcasting multiplies the vector element-wise with each row of the array
result = vector * array
print(result)
Output:
[[1 2 3]
 [4 10 18]]

In this example, the 1D array vector is element-wise multiplied with each row of the 2D array array. Broadcasting automatically stretches the 1D array to match the number of columns in the 2D array, resulting in the output array.

Example 3: Adding two arrays of different shapes

array1 = np.array([[1, 2, 3]])  # 1D array
array2 = np.array([[4], [5], [6]])  # 2D array

# Broadcasting stretches the 1D array to match the 2D array shape
result = array1 + array2
print(result)
Output:

[[5 6 7]
 [5 6 7]
 [5 6 7]]

In this example, the 1D array array1 is added to the 2D array array2. Broadcasting stretches the 1D array to match the shape of the 2D array, resulting in the output array.