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: 

  1. 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.
  2. 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.
  3. The arrays can be broadcast together if they are compatible with all dimensions.
  4. After broadcasting, each array behaves as if it had a shape equal to the element-wise maximum of shapes of the two input arrays.
  5. 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:

  1. The arrays have the same shapes
  2. The arrays have the same number of dimensions and the length of each dimension is either of common length or 1.
  3. 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.

Similar Reads

What is NumPy Array Broadcasting?

Broadcasting provides a means of vectorizing array operations, therefore eliminating the need for Python loops. This is because NumPy is implemented in C Programming, which is a very efficient language....

NumPy Broadcasting Arrays in Python

...

Array Broadcasting Algorithm

Let’s assume that we have a large data set, each data is a list of parameters. In Numpy, we have a 2-D array, where each row is data and the number of rows is the size of the data set. Suppose we want to apply some sort of scaling to all these data every parameter gets its scaling factor or say every parameter is multiplied by some factor....

Array Broadcasting Examples

...

Conclusion

The NumPy array broadcasting algorithm determines how NumPy will treat arrays with different shapes during arithmetic operations....

Contact Us