NumPy Broadcasting

Carson West

Libraries like NumPy

NumPy Broadcasting

NumPy broadcasting is a powerful mechanism that allows NumPy to perform operations on arrays of different shapes, under certain conditions. It avoids explicit looping and significantly speeds up computations.

Core Idea: Broadcasting allows binary operations (like +, -, *, /) to be applied between arrays of different shapes, provided that certain rules are met. The smaller array is implicitly “stretched” or “copied” to match the shape of the larger array before the operation is performed.

Rules for Broadcasting:

  1. Shape Compatibility: Two arrays are compatible if they have the same number of dimensions, or if one array has a dimension of size 1 where the other has a dimension of size greater than 1.

  2. Dimension Alignment: Arrays are aligned from right to left (i.e., least significant dimension first). If dimensions are not compatible, an error will occur.

Examples:

import numpy as np

# Example 1:  Adding a scalar to an array
a = np.array(1, 2, 3)
b = 5
c = a + b  # b is broadcasted to 5, 5, 5
print(c)  # Output: 6 7 8


# Example 2: Adding a 1D array to a 2D array
a = np.array(1, 2, 3, 4, 5, 6)
b = np.array([10, 20, 30]])
c = a + b # b is broadcasted to 10, 20, 30]], [10, 20, 30]]
print(c)  # Output: 11 22 33, [14 25 36]]

# Example 3: Incompatible shapes - Error
a = np.array(1, 2, 3, 4)
b = np.array(1, 2, 3)
c = a + b #This will raise a ValueError
#print(c)


#Example 4: Broadcasting with 1s
a = np.array(1,2,3,4,5,6)
b = np.array(1,2)
c = a + b
print(c) # Output: 2 3 4,6 [7 8](./../7-8/)

Advanced Cases: Broadcasting can involve more complex shape manipulations, including the use of None or np.newaxis to add dimensions. NumPy Newaxis

Benefits of Broadcasting:

Limitations:

Related Notes: