Array Broadcasting in NumPy
What happens when you add a vector of shape (3,) to a matrix of shape (4, 3)? In regular math, this is undefined. In NumPy, broadcasting automatically stretches the smaller array along the missing dimensions so the operation makes sense. This eliminates entire categories of loops in AI code.
The Broadcasting Rules
NumPy compares the shapes of two arrays from right to left. Two dimensions are compatible when they are equal, or when one of them is 1. When a dimension is 1, it gets logically repeated to match the other.
Shapes are also prepended with 1s on the left to make them the same length before comparison.
Shape Compatibility Examples
Real-World Example: Adding a Bias Vector
One of the most common uses of broadcasting is adding a bias vector to an output matrix in a neural network layer. The weight output is a matrix, the bias is a 1D vector — broadcasting handles this cleanly.
Bias Addition in a Layer
Without broadcasting you'd need a for loop over all 5 rows. NumPy avoids that entirely.
Normalizing Data Across a Batch
Another common use is mean subtraction — centering a dataset by subtracting the column mean. The mean has shape (features,) and the dataset has shape (samples, features); broadcasting subtracts the right mean from every sample automatically.