Matrix Broadcasting
When performing operations on arrays with different shapes, numpy automatically expands one array to match the other. This is broadcasting.
Rules:
- Compare dimensions from right to left
- Dimensions must be equal, or one must be 1
- If either is 1, expand to match the other
Example:
arr = np.array([1, 2, 3, 4])
arr * 2 # [2, 4, 6, 8]
# Expands [2] to [2, 2, 2, 2]Error case:
a = np.array([1, 2, 3])
b = np.array([1, 2])
a + b # ValueError: shapes (3,) and (2,) not aligned