Numpy Broadcasting

In NumPy, we can perform mathematical operations on arrays of different shapes. An array with a smaller shape is expanded to match the shape of a larger one. This is called broadcasting.

Let's see an example.

array1 = [1, 2, 3]
array2 = [[1], [2], [3]]

array1 is a 1-D array and array2 is a 2-D array. Let's perform addition between these two arrays of different shapes.

result = array1 + array2

Here, NumPy automatically broadcasts the size of a 1-D array array1 to perform element-wise addition with a 2-D array array2.


Example: NumPy Broadcasting

import numpy as np

# create 1-D array
array1 = np.array([1, 2, 3])

# create 2-D array
array2 = np.array([[1], [2], [3]])

# add arrays of different dimension
# size of array1 expands to match with array2
sum = array1 + array2

print(sum)

Output

[[2 3 4]
 [3 4 5]
 [4 5 6]]

In the example, we added two arrays with different dimensions. Numpy automatically expands the size of 1-D array array1 to match with the size of 2-D array array2.

Then, the element-wise addition is performed between two 2-D arrays.


Compatibility Rules for Broadcasting

Broadcasting only works with compatible arrays. NumPy compares a set of array dimensions from right to left.

Every set of dimensions must be compatible with the arrays to be broadcastable. A set of dimension lengths is compatible when

  • one of them has a length of 1 or
  • they are equal

Let's see an example.

array1 = shape(6, 7)
array2 = shape(6, 1)

Here, array1 and array2 are arrays with different dimensions (6,7) and (6,1) respectively.

The dimension length 7 and 1 are compatible because one of them is 1.

Similarly, 6 and 6 are compatible since they are the same.

As both sets of dimensions are compatible, the arrays are broadcastable.


Examples of Broadcastable Shapes

Now, we'll see the list of broadcastable and non-broadcastable shapes.

Broadcastable Shapes

  • (6, 7) and (6, 7)
  • (6, 7) and (6, 1)
  • (6, 7) and (7, )

Two arrays need not have the same number of dimensions to be broadcastable.

The last set of shapes is broadcastable because the right-most dimensions are both 7.

Non-Broadcastable Shapes

  • (6, 7) and (7, 6)
  • (6, 7) and (6, )

The last set of shapes is not broadcastable because the right-most dimensions are not the same.


Broadcasting with Scalars

We can also perform mathematical operations between arrays and scalars (single values). For example,

import numpy as np

# 1-D array
array1 = np.array([1, 2, 3])

# scalar
number = 5

# add scalar and 1-D array
sum = array1 + number

print(sum)

Output

[6 7 8]

In this example, NumPy automatically expands the scalar number to an 1-D array and then performs the element-wise addition.