Latticework

Command Palette

Search for a command to run...

NumPy

Broadcasting

16 min

Explanation

Broadcasting is what lets arr + 5 work even though arr is an array and 5 is a single number — NumPy conceptually "stretches" the smaller shape to match the larger one, without actually copying any data.

import numpy as np

prices = np.array([100, 105, 98])
print(prices + 10)     # [110, 115, 108] — 10 is broadcast to every element
print(prices * 1.1)     # a 10% increase applied to every element at once
Try it

A shape-(3,) array added to a shape-(2,3) array gets broadcast across every row — this is the single most useful broadcasting pattern you'll use in practice.

Loading editor…
Explanation

The actual rule: NumPy compares shapes element-wise from the right. Two dimensions are compatible if they're equal, or if one of them is 1 (or missing — treated as 1). (2, 3) and (3,) are compatible because the trailing 3 matches and the missing leading dimension is implicitly 1, which broadcasts to 2.

(2, 3) + (3,)    # OK — (3,) broadcasts to (2, 3)
(2, 3) + (2, 1)   # OK — the 1 broadcasts across the 3 columns
(2, 3) + (2,)     # ERROR — 3 and 2 don't match, and neither is 1

Getting a ValueError: operands could not be broadcast together is almost always a sign your shapes don't line up the way you think — check .shape on both arrays first.

Exercise

Write `add_scalar(arr, scalar)`, returning `arr` with `scalar` added to every element, using broadcasting (`arr + scalar`) — not a loop.

Exercise

Write `subtract_col_means(matrix)`: given a 2D list, compute the mean of each column (`arr.mean(axis=0)`), subtract it from every row via broadcasting, and return `np.round(result, 2)`.

Quiz

What does NumPy do when you add a 1D array of shape (3,) to a 2D array of shape (4, 3)?

Checkpoint

You can predict when two array shapes will broadcast together, and use broadcasting to apply a scalar or a row/column vector across a whole array.