ADVERTISEMENT
ADVERTISEMENT

What is Broadcasting in Numpy Array?

NumPy array broadcasting is a powerful feature that allows arrays of different shapes to be combined and operated upon in a way that avoids the need for explicit loops. Broadcasting enables efficient element-wise operations between arrays of different sizes, making it easier to write concise and efficient code. Learn how broadcasting works in Numpy array with examples.

In NumPy, broadcasting follows a set of rules that determine how arrays with different shapes can be combined. The rules of broadcasting are as follows:

Rule 1: Arrays have different dimensions

  • If the arrays have different numbers of dimensions, the smaller array is padded with ones on its left side until the numbers of dimensions match.
  • This rule allows arrays with different shapes to be broadcasted and operated upon.
  • Example: If array A has shape (3,) and array B has shape (3, 1), then array A is implicitly broadcasted to (1, 3) to match the dimensions of B.

Rule 2: Arrays have at least one mismatched dimension (excluding dimensions with size 1)

  • If the shape of the arrays does not match in any dimension, the array with size equal to 1 in that dimension is stretched or repeated to match the shape of the other array.
  • This rule enables broadcasting between arrays of different sizes in specific dimensions.
  • Example: If array A has shape (2, 3) and array B has shape (3,), then array B is implicitly broadcasted to (2, 3) by repeating the values along the new axis.

Rule 3: Arrays have incompatible sizes

  • If the sizes of the arrays in a particular dimension are neither equal nor 1, an error is raised.
  • This rule specifies that broadcasting is not possible when the arrays have incompatible shapes in specific dimensions.
  • Example: If array A has shape (2, 3) and array B has shape (4, 5), which have different sizes in the second dimension, an error will be raised because broadcasting is not feasible.

Consider two arrays, A and B, with shapes A.shape and B.shape, respectively.

Rule Broadcasting Condition Example
Rule 1 Arrays have different dimensions

A.shape = (3, 1)

B.shape = (1, 4)

Rule 2 Arrays have at least one mismatched dimension (excluding dimensions with size 1)

A.shape = (2, 3)

B.shape = (3,)

Rule 3 Arrays have incompatible sizes

A.shape = (2, 3)

B.shape = (4, 5)

Let's go through each rule with the corresponding examples:

Rule 1: Arrays have different dimensions

If the arrays have different numbers of dimensions, the smaller array is padded with ones on its left side until the numbers of dimensions match.

Example:

import numpy as np

A = np.array([1, 2, 3])          # Shape: (3,)
B = np.array([[10], [20], [30]]) # Shape: (3, 1)

result = A + B
print(result)

#output
#[[11 12 13]
# [21 22 23]
# [31 32 33]]

In this example, array A has shape (3,) and array B has shape (3, 1). To match the dimensions, array A is implicitly broadcasted to (1, 3). The result is a shape (3, 3) array, obtained by element-wise addition.

Let's break down the example using Rule 1:

  1. Both arrays have different dimensions: Array A has 1 dimension, and array B has 2 dimensions.
  2. The smaller array, A, is padded with ones on its left side until the numbers of dimensions match. So, A becomes (1, 3).

After applying Rule 1, the arrays have the following shapes:

  • A: (1, 3)
  • B: (3, 1)

Now, broadcasting can be performed since the shapes are compatible. The broadcasting results in the following array shapes:

  • A: (3, 3)
  • B: (3, 3)

The original array A has shape (3,), and through broadcasting, it is implicitly transformed to shape (3, 3) by repeating the rows.

The original array B has shape (3, 1), and through broadcasting, it is implicitly transformed to shape (3, 3) by repeating the columns.

NumPy Array Broadcasting

Rule 2: Arrays have at least one mismatched dimension (excluding dimensions with size 1)

If the shape of the arrays does not match in any dimension, the array with size equal to 1 in that dimension is stretched or repeated to match the shape of the other array.

Example:

import numpy as np

A = np.array([[1, 2, 3], [4, 5, 6]])  # Shape: (2, 3)
B = np.array([10, 20, 30])             # Shape: (3,)

result = A + B
print(result)

#output
# [[11 22 33]
#  [14 25 36]]

In this example, array A has shape (2, 3) and array B has shape (3,). We can see that the arrays have different shapes, but the dimensions with size 1 are excluded from consideration.

To apply broadcasting according to Rule 2, the following steps are followed:

  1. Check if the shapes are compatible for broadcasting, starting from the trailing dimensions.

  2. Compare the sizes of the dimensions:

    • For dimensions with the same size or dimensions with size 1, they are considered compatible.

    • For dimensions with different sizes and neither size being 1, broadcasting is not possible, and an error is raised.

  3. If the sizes are compatible or a dimension has size 1, the array with size 1 in that dimension is stretched or repeated to match the size of the other array.

In our example, let's analyze the broadcasting process step by step:

Step 1:

  • Array A has shape (2, 3).
  • Array B has shape (3,).

Step 2:

  • The trailing dimensions are considered first.(The term "trailing dimension" refers to the last dimension(s) of an array shape)
  • The trailing dimensions of A and B are (3,) and (3,), respectively.
  • The sizes match, so they are compatible.

Step 3:

  • As the dimensions match, no stretching or repeating is necessary.

Rule 3: Arrays have incompatible sizes

If the sizes of the arrays in a particular dimension are neither equal nor 1(one of the sizes should be 1 ), an error is raised.

Example:

import numpy as np

A = np.array([[1, 2, 3], [4, 5, 6]])  # Shape: (2, 3)
B = np.array([[10, 20], [30, 40]])     # Shape: (2, 2)

result = A + B  

# Raises ValueError: operands could not be broadcast together with shapes (2,3) (2,2)

In this example, array A has shape (2, 3) and array B has shape (2, 2). The sizes in the second dimension do not match, and they are not equal to 1 either. Therefore, broadcasting cannot be applied, and an error is raised.

These examples demonstrate how broadcasting rules work in different scenarios, allowing arrays with different shapes to be combined and operated


ADVERTISEMENT

ADVERTISEMENT