Array Broadcasting Algorithm
The NumPy array broadcasting algorithm determines how NumPy will treat arrays with different shapes during arithmetic operations.
Input: Array ‘A’ with ‘m’ dimensions and array ‘B’ with ‘n’ dimensions
p = max(m, n) if m < p: left-pad A's shape with 1s until it also has p dimensions else if n < p: left-pad B's shape with 1s until it also has p dimensions result_dims = new list with p elements for i in p-1 ... 0: A_dim_i = A.shape[i] B_dim_i = B.shape[i] if A_dim_i != 1 and B_dim_i != 1 and A_dim_i != B_dim_i: raise ValueError("could not broadcast") else: # Pick the Array which is having maximum Dimension result_dims[i] = max(A_dim_i, B_dim_i)
Rules for Broadcasting
Broadcasting arrays is possible if the following rules are met:
- If the arrays don’t have the same rank then prepend the shape of the lower rank array with 1s until both shapes have the same length.
- The two arrays are compatible in a dimension if they have the same size in the dimension or if one of the arrays has size 1 in that dimension.
- The arrays can be broadcast together if they are compatible with all dimensions.
- After broadcasting, each array behaves as if it had a shape equal to the element-wise maximum of shapes of the two input arrays.
- In any dimension where one array had size 1 and the other array had size greater than 1, the first array behaves as if it were copied along that dimension.
What makes an array Broadcastable?
An array can be considered broadcastable if it satisfies the following rules:
- The arrays have the same shapes
- The arrays have the same number of dimensions and the length of each dimension is either of common length or 1.
- If an array has a different number of dimensions then the array with fewer dimensions can have its shape prepended.
NumPy Array Broadcasting
The term broadcasting refers to the ability of NumPy to treat arrays with different dimensions during arithmetic operations. This process involves certain rules that allow the smaller array to be ‘broadcast’ across the larger one, ensuring that they have compatible shapes for these operations.
Broadcasting is not limited to two arrays; it can be applied over multiple arrays as well.
In this article, we will learn about broadcast over multiple arrays in the NumPy extension in Python.
Contact Us