Broadcasting lets NumPy operate on arrays of different shapes by virtually expanding smaller arrays along length-1 axes—without copying full-size temp arrays when possible.
Rules (simplified)
- Align shapes from the trailing dimension
- Dimensions must be equal or one of them is 1
- Missing dimensions are treated as size 1
Examples
import numpy as np
a = np.array([[1], [2], [3]]) # (3, 1)
b = np.array([10, 20, 30]) # (3,)
print(a + b) # (3, 3) after broadcast
Common patterns
- Subtract column mean:
X - X.mean(axis=0) - Scale rows:
X * row_weights[:, np.newaxis] - Compare to scalar threshold:
arr > 0.5
Important interview questions and answers
- Q: Broadcasting always copy?
A: Often uses virtual stride tricks—no full materialization of expanded array. - Q: Incompatible shapes?
A: ValueError when neither side is 1 and sizes differ.
Self-check
- Add shape (3,1) array to shape (3,) array—result shape?
- Why does broadcasting matter for performance?
Pitfall: Draw trailing axes on paper before combining shapes.
Interview prep
- Rule?
Align trailing dims; equal or one is 1.
- Failure?
ValueError when dimensions incompatible and neither is 1.