Broadcasting
Last updated
So far, arrays in a sum have had the same shape, or one of them was a single number. Broadcasting is NumPy's rule for everything in between: a table minus a row, a column plus a row. In this lesson you'll learn the rule, use it to compare each store with the day's average, and see the mistake it makes easy.
By the end of this lesson you can
- Apply the broadcasting rule to two shapes, from the right
- Subtract each column's mean from a table, and standardise it
- Turn a row into a column with
np.newaxisto build a grid - Spot a
(n,)and(n, 1)mix-up that makes an(n, n)grid by mistake
The rule, step by step
Step through it. The pale boxes are copies NumPy acts as if it made.
kilos - means. But the shapes differ: (3, 4) and (4,).The whole rule: line the two shapes up from the right. Going left, each pair of lengths must be equal, or one of them must be 1 (a missing length counts as 1). A length of 1 stretches to match the other. Some examples:
| shapes | going from the right | result |
|---|---|---|
(3, 4) and (4,) | 4 and 4 equal; 3 and missing | (3, 4) |
(3, 4) and (3, 1) | 4 and 1 stretch; 3 and 3 equal | (3, 4) |
(3, 1) and (4,) | 1 and 4 stretch; 3 and missing | (3, 4) |
(3, 4) and (3,) | 4 and 3: neither equal nor 1 | ValueError |
Compared with the day’s average
Run the first two cells.
kilos - means is a (3, 4) table: how far each store was above or below the day's average. Monday's mean was 35, so Batangas, with 42, was 7 above.
Kilos aren't always the best way to compare. A store that varies a lot from day to day being 7 above is less unusual than a steady one being 7 above. Standardising measures the distance in standard deviations instead, column by column. For each value in a column with mean and standard deviation (lesson 2.8),
The fourth cell does it for the whole table in one line: a (3, 4) minus a (4,), divided by a (4,). A of 1.22 means "1.22 standard deviations above that day's mean". You'll meet again in Stage 8, and machine learning uses it constantly.
The last cell fails: (3, 4) and (3,). You may have meant "one number per store", but NumPy matches from the right, against the 4 days. Step 4 shows how to say "per row".
Row plus column makes a grid
When both arrays stretch, a short row and a short column make a whole table.
(3, 1), and a row of markups, shape (4,).np.newaxis adds an axis of length 1. prices[:, np.newaxis] turns the (3,) row into a (3, 1) column, and then the rule does the rest: (3, 1) and (4,) make (3, 4). That's the third cell in the notebook above: three base prices with four markups, a whole price list.
You can also write prices.reshape(-1, 1), which does the same.
Wrong way: a column where a row was meant
Three stores sold 42, 35 and 28 kg. Their targets were 45, 35 and 30, and someone typed the targets as a column. You want each store's gap from its target. Run it.
No error, and a (3, 3) answer. (3,) and (3, 1) broadcast to (3, 3): every store's sales minus every store's target, nine numbers where you wanted three. The right answers are on the diagonal (−3, 0, −2), lost among six that mean nothing.
This is the most common broadcasting bug, because nothing fails. A total worked out from gap would be wrong, and you might never look at its shape. Two habits catch it:
- Check the shape of any result you didn't expect to look at:
gap.shape. - Make the shapes match on purpose. The last cell flattens
targetwith.ravel(), so both are(3,), and the answer is three numbers.
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 rule, with pictures
- NumPy: Constants, numpy.newaxis: adding an axis of length 1
- Harris et al., Array programming with NumPy, Nature 585, 2020: broadcasting as one of the ideas NumPy is built on