Broadcasting in NumPy
What is broadcasting?
Broadcasting is NumPyβs ability to perform operations on arrays with different shapes by automatically expanding (virtually) smaller arrays.
This is a major reason NumPy code is concise and fast.
Simple example: add scalar
import numpy as np
arr = np.array([1, 2, 3])
print(arr + 10) # [11 12 13]import numpy as np
arr = np.array([1, 2, 3])
print(arr + 10) # [11 12 13]Here, the scalar 1010 is broadcast to match the shape (3,)(3,).
Example: add vector to matrix
import numpy as np
mat = np.array([
[1, 2, 3],
[4, 5, 6]
])
vec = np.array([10, 20, 30])
print(mat + vec)import numpy as np
mat = np.array([
[1, 2, 3],
[4, 5, 6]
])
vec = np.array([10, 20, 30])
print(mat + vec)vecvec (shape (3,)(3,)) broadcasts across each row.
Broadcasting rules (must know)
When operating on two arrays, NumPy compares shapes from the trailing dimension.
Two dimensions are compatible when:
- They are equal, OR
- One of them is
11
If dimensions are incompatible β broadcasting error.
Example: column vector + matrix
import numpy as np
mat = np.array([
[1, 2, 3],
[4, 5, 6]
])
col = np.array([100, 200]).reshape(2, 1)
print(mat + col)import numpy as np
mat = np.array([
[1, 2, 3],
[4, 5, 6]
])
col = np.array([100, 200]).reshape(2, 1)
print(mat + col)colcol has shape (2, 1)(2, 1) and broadcasts across columns.
Common broadcasting error
import numpy as np
mat = np.zeros((2, 3))
vec = np.array([1, 2])
# mat + vec -> ValueError (shapes (2,3) and (2,) not compatible)import numpy as np
mat = np.zeros((2, 3))
vec = np.array([1, 2])
# mat + vec -> ValueError (shapes (2,3) and (2,) not compatible)Fix by reshaping vecvec to a column vector if thatβs what you intend:
vec = vec.reshape(2, 1)
print(mat + vec)vec = vec.reshape(2, 1)
print(mat + vec)Why broadcasting is useful in analytics
- Normalize columns:
X / X.max(axis=0)X / X.max(axis=0) - Center data:
X - X.mean(axis=0)X - X.mean(axis=0) - Apply weights:
X * weightsX * weights
Next
Continue to: NumPy Arithmetic Operations for element-wise math and matrix operations.
π§ͺ Try It Yourself
Exercise 1 β Create a NumPy Array
Exercise 2 β Array Shape and Reshape
Exercise 3 β Array Arithmetic
If this helped you, consider buying me a coffee β
Buy me a coffeeWas this page helpful?
Let us know how we did
