NumPy

Broadcasting

Understanding broadcasting rules

This is a broadcasting tutorial.

Broadcasting is a powerful mechanism in NumPy that allows arrays of different shapes process arithmetic operations. It’s a way of treating arrays with different shapes during arithmetic operations. Understanding broadcasting rules is crucial for efficiently working with arrays of different shapes in NumPy, as it allows you to write more concise and readable code for array operations.

The Broadcasting rules of NumPy are quite intuitive and follow these steps:

  • Comparing of the dimensions of the arrays, starting from the trailing dimensions and then working its way forward.
  • Arrays with fewer dimensions are padded with ‘1’s on their leading (left) side.
  • Arrays are compatible in a dimension if they have the same size in that dimension, or if one of the arrays has size 1 in that dimension.
  • After steps 2 and 3, the sizes of the arrays along each dimension go through comparison. If they are not equal, and one of them is not equal to 1, then the operation raises an error.
  • Arrays that have a size of 1 along a particular dimension act as if they had the size of the array with the largest shape along that dimension.

Let’s look at a practical example.

import numpy as np

# Create two arrays of different shapes
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([1, 2, 3])

We can check the shape of the arrays with the NumPy shape method.

print(arr1.shape)
# Shape of arr1: (2, 3)
print(arr2.shape)
# Shape of arr2: (3,)

The following is the process occurring behind the scenes.

Broadcasting rule applies:

  1. arr2 is padded to have the shape (1, 3)
  2. Then it’s broadcasted to match the shape of arr1, becoming (2, 3)
result = arr1 + arr2
print(result)

# Output: [[2 4 6] [5 7 9]]

Broadcasting in practice

Here, we look at the different scenarios.

These examples demonstrate how the method simplifies the process of performing element-wise operations between arrays of different shapes. Therefore, making it a powerful tool for writing concise arrays and efficient code.

A scalar to a one-dimensional array

import numpy as np

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

result = arr + scalar
print(result)

# Output: [6 7 8]

A scalar to a two-dimensional array

import numpy as np

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

result = arr * scalar
print(result)

# Output: [[2 4 6] [8 10 12]]

Arrays with different shapes

import numpy as np

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

result = arr1 + arr2
print(result)

# Output: [[2 4 6] [5 7 9]]

Arrays with different shapes (including a singleton dimension)

import numpy as np

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

result = arr1 * arr2
print(result)

# Output: [[1 2 3] [8 10 12]]

Along multiple dimensions

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([[1], [2], [3]])

result = arr1 * arr2
print(result)

# Output: [[1 2 3] [2 4 6] [3 6 9]]

This is an original broadcasting educational material created by aicorr.com.

Next: Array Iteration

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.