NumPy Broadcasting

NumPy broadcasting is a powerful mechanism that allows NumPy to work with arrays of different shapes when performing arithmetic operations. Through broadcasting, a smaller array can be automatically "stretched" or "copied" to match the shape of a larger array, enabling element-wise operations between them.

Here is an example of broadcasting in action:

import numpy as np # We have a matrix A with shape (3, 4) # and a vector v with shape (4,) A = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) v = np.array([1, 0, 1, 0]) # We can add v to each row of A by simply # performing the element-wise addition print(A + v)

The output will be:

[[ 2 2 4 4] [ 6 6 8 8] [10 10 12 12]]

Notice how the vector v was "stretched" to match the shape of the matrix A, and the element-wise addition was performed as if v was a matrix with the same shape as A. This is possible because NumPy compares the shapes of the arrays and "broadcasts" v to match the shape of A.

Broadcasting follows a set of rules to determine whether two arrays are compatible for element-wise operations, which are as follows:

  • If the two arrays have the same shape, they are compatible for element-wise operations.
  • If the two arrays have the same number of dimensions, but one of them has a length of 1 in a particular dimension, the array with the length of 1 is "stretched" to match the shape of the other array.
  • If the two arrays have different numbers of dimensions, the array with fewer dimensions is "padded" with extra dimensions of length 1 to match the shape of the other array.

Here are some more examples to illustrate these rules:

# Example 1 # Compatible shapes A = np.array([[1, 2, 3], [4, 5, 6]]) B = np.array([[7, 8, 9], [10, 11, 12]]) print(A + B) # element-wise addition # Example 2 # Same number of dimensions, one array has a length of 1 in a particular dimension A = np.array([[1, 2, 3], [4, 5, 6]]) B = np.array([7, 8, 9]) print(A + B) # B is "stretched" to match the shape of A # Example 3 # Different numbers of dimensions A = np.array([[1, 2, 3], [4, 5, 6]]) B = np.array([7, 8]) print(A + B) # B is "padded" with an extra dimension of length 1

The output of the above code will be:

[[ 8 10 12] [14 16 18]] [[ 8 10 12] [11 13 15]] [[ 8 10 12] [11 13 15]]

As you can see, NumPy broadcasting allows us to perform element-wise operations on arrays with different shapes, making it a very useful tool for working with arrays of different shapes in a convenient and efficient manner.

No comments

Powered by Blogger.