← All questions
Python & DataMedium· NumPy Essentials

What is broadcasting in NumPy, and where does it actual...

Broadcasting is NumPy’s rule for combining arrays that don’t have the same shape: it pads the smaller array’s shape with 1s on the left, stretches any dimension of size 1 to match the other array, and raises an error only when two dimensions disagree and neither is 1. Crucially, no data actually gets copied to do the stretching — it’s a virtual expansion.

This shows up constantly in ML code. Normalizing a feature matrix means subtracting a per-column mean (shape (n_features,)) from a full data matrix (shape (n_rows, n_features)) — broadcasting applies that one mean vector to every row automatically. Adding a bias vector across a neural network layer’s output works the same way, and so does comparing an array of prediction scores against a single scalar threshold to get a boolean mask. The habit worth building is tracing shapes explicitly — (3,3) - (3,) broadcasts fine, (3,3) - (2,) doesn’t — rather than guessing.

Code playground (Python)