Broadcasting

How NumPy combines arrays of different shapes — the rule that makes vectorization actually work.

⏱ 9 min read Intermediate NumPy Updated May 2026

What you'll learn

  • The broadcasting rule, stated precisely
  • When a shape mismatch is broadcastable vs an error
  • Common broadcasting patterns — centering, scaling, outer-products
Prerequisites: numpy/why-numpy , numpy/ndarray

Broadcasting is how NumPy lets you do operations on arrays of different shapes — like adding a vector to every row of a matrix — without writing an explicit loop. Once it clicks, it changes how you write array code.

The rule

NumPy aligns the shapes right-to-left. For each axis:

  • if the sizes are equal → fine
  • if one of them is 1 → it stretches to match the other
  • otherwise → error

That’s the whole rule.

A:    (5, 4, 3)
B:       (4, 3)    → aligns as (1, 4, 3) → broadcasts to (5, 4, 3) ✓

A:    (5, 4)
B:    (5,)         → aligns as (1, 5)   → can NOT match (5, 4) ✗
                                              (the trailing axis differs)

The 3 patterns you’ll use 90% of the time

1. Subtract a row from every row (centering)

Python · NumPy
Ready
Output
(click Run)

The mean has shape (3,), X has shape (4, 3). NumPy lines them up as (1, 3) vs (4, 3) — the 1 broadcasts, and the subtraction runs on every row.

This is exactly how you “center” your features before PCA or linear regression. No loop required.

2. Scale each column independently

Python · NumPy
Ready
Output
(click Run)

3. Outer product — column × row

Python · NumPy
Ready
Output
(click Run)

This is the outer product pattern. You see it in attention mechanisms, in computing pairwise distances, and in countless physics simulations.

When broadcasting fails

Python · NumPy
Ready
Output
(click Run)

v[:, np.newaxis] (or equivalently v.reshape(-1, 1)) is how you tell NumPy “treat this 1D array as a column.” This little trick fixes 80% of “could not broadcast” errors.

A non-obvious use: distance matrix

Compute pairwise squared distances between every pair of points — without writing a loop:

Python · NumPy
Ready
Output
(click Run)

What you just wrote is the inner loop of k-nearest-neighbors. The “broadcast a pair of new-axis insertions” pattern is the cleanest way to do all-pairs operations in NumPy.

Quick check

Quick check

0/2 answered
Q1.Two arrays have shapes (5, 1, 3) and (4, 3). Are they broadcastable?
Q2.You want to subtract the per-row mean from a (n, d) matrix X. Which one works?

Finished the lesson?

Mark it complete to track your progress and keep your streak alive. +20 XP

Skip to content