Broadcasting
Last updated
Before training, almost every model's data is centred and scaled: each feature has its mean taken away and is divided by its spread. That means combining a table of shape (n, d) with a row of d numbers. NumPy does it with broadcasting: a rule for doing maths between arrays of different shapes. In this lesson you'll learn the rule, use it to standardise data, and fix the error it gives when shapes don't fit.
By the end of this lesson you can
- Check whether two shapes broadcast, comparing them from the right
- Centre and standardise each feature of a data matrix in one line
- Make a grid from a column and a row
- Fix a shape mismatch with
keepdims=True
The rule, step by step
In lesson 0.4, maths between two arrays worked element by element, when both had the same shape. Broadcasting lets it work when they don't, as long as the shapes are compatible. Step through it:
kilos - means. But the shapes differ: (3, 4) and (4,).The rule, in full:
- Write the two shapes one above the other, lined up on the right.
- Where one shape is shorter, treat its missing lengths as 1.
- Compare each pair of lengths. They must be equal, or one of them must be 1.
- A length of 1 stretches to match the other. Nothing is really copied; NumPy just reuses the same numbers.
So (5, 3) and (3,) work: on the right, 3 and 3 are equal; on the left, 5 and a missing length (counted as 1) work too. The result is (5, 3).
Centring and scaling features
Here's the use you'll make of it most. Features come in different units: bill depth around 17 mm, flipper length around 200 mm, body mass in the thousands of grams. Many models train badly when one feature's numbers are a hundred times bigger than another's (Stage 3 shows why). The fix is to put every feature on the same scale. Run each cell:
X - meanstakes each feature's mean away from that feature's column.(5, 3)minus(3,): the row of means broadcasts down all five rows. This is centring.- Every column of
centrednow has mean 0 (the third cell checks it;round(10)hides tiny rounding errors like1e-15). - Dividing by
X.std(axis=0), each feature's standard deviation (its typical distance from the mean), makes each column's spread 1 as well. This is standardising.
In symbols, for the value in row , column :
In words: take feature 's mean (the Greek letter mu) away from the value, then divide by feature 's standard deviation (sigma). In NumPy, for every and at once:
Z = (X - X.mean(axis=0)) / X.std(axis=0) is X.mean(axis=0)[j] and is X.std(axis=0)[j]: two arrays of shape (3,), both broadcast down the rows. A standardised value of 1.66 means "1.66 standard deviations above this feature's mean", whatever the feature's units were.
A column plus a row makes a grid
When both arrays have a length of 1 somewhere, both stretch. A column of shape (3, 1) and a row of shape (4,) make a (3, 4) grid where every row value meets every column value:
(3, 1), and a row of markups, shape (4,).Lined up on the right: (3, 1) and (4,). Last pair: 1 and 4, so the column stretches across. First pair: 3 and a missing length, so the row stretches down. Result (3, 4).
You'll use this to compare every example with every other (the distances behind nearest neighbours in Stage 2), and in Stage 10, where attention scores every word in a sentence against every other word.
Wrong way: shapes that don't fit
Two shapes that break the rule. Run each cell:
(3,)and(4,): on the right, 3 and 4 are neither equal nor 1. NumPy stops with aValueError: operands could not be broadcast together with shapes (3,) (4,).- The second error is the one you'll really meet. You want to centre each penguin (each row) instead of each feature, so you take
X.mean(axis=1), one mean per row, shape(5,). But lined up on the right,(5, 3)and(5,)compare 3 with 5. NumPy tries to match the means to the columns, and fails.
The fix is keepdims=True. It keeps the axis you averaged over, with length 1, so the shape is (5, 1): a column. Lined up on the right, 3 and 1 work, and 5 and 5 work. The last cell runs.
The error is the good outcome here. Worse is when the shapes happen to fit the wrong way: a (3, 3) array minus three row means would broadcast across the columns with no error and give wrong numbers. When you broadcast, say the shapes out loud, and use keepdims=True whenever you summarise along axis=1 to combine the result with the original.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- NumPy: Broadcasting: the general broadcasting rule, compared from the trailing dimension
- NumPy: numpy.std: the standard deviation, and the keepdims argument
- Goodfellow, Bengio and Courville, Deep Learning, chapter 2: Linear Algebra: adding a vector to every row of a matrix, which the book also calls broadcasting