NumPy Broadcasting
Broadcasting is the method that NumPy uses to allow arithmetic operations between arrays of different shapes. The arithmetic operations on arrays are usually performed on corresponding elements.
If two arrays, a
and b
, have the same shape, i.e., a.shape == b.shape, then the result of a * b
is the element-wise multiplication of a
and b
. This requires the same number of dimensions and equal lengths in each dimension.
Example
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([10, 20, 30, 40])
c = a * b
print(c)
Output:
[ 10 40 90 160]
When the shapes of the two arrays in an operation are different, NumPy automatically triggers the broadcasting mechanism. For example:
Example
import numpy as np
a = np.array([[ 0, 0, 0],
[10, 10, 10],
[20, 20, 20],
[30, 30, 30]])
b = np.array([0, 1, 2])
print(a + b)
Output:
[[ 0 1 2]
[10 11 12]
[20 21 22]
[30 31 32]]
A 4x3 two-dimensional array added to a one-dimensional array of length 3 is equivalent to repeating array b
four times in two dimensions and then performing the operation:
Example
import numpy as np
a = np.array([[ 0, 0, 0],
[10, 10, 10],
[20, 20, 20],
[30, 30, 30]])
b = np.array([1, 2, 3])
bb = np.tile(b, (4, 1)) # Repeat b along each dimension
print(a + bb)
Output:
[[ 1 2 3]
[11 12 13]
[21 22 23]
[31 32 33]]
Broadcasting Rules:
- All input arrays are aligned with the array that has the longest shape by padding shorter shapes with ones at the front.
- The shape of the output array is the maximum value along each dimension of the input arrays.
- An array can be used in the calculation if its dimension matches the corresponding dimension of the output array or if its length is 1. Otherwise, an error occurs.
- When the length of a dimension of an input array is 1, the first set of values in that dimension is used throughout the calculation along that dimension.
Simple Understanding: For two arrays, compare each dimension (ignore if one array does not have the current dimension) and satisfy:
- The arrays have the same shape.
- The values of the current dimension are equal.
- The value of the current dimension is 1.
If these conditions are not met, a "ValueError: frames are not aligned" exception is thrown.