Topics in this subject
NumPy 2 min read Updated 4 Aug 2026

7. Broadcasting

Broadcasting lets NumPy operate on arrays of different shapes without copying data, by virtually stretching smaller arrays.

Broadcasting lets NumPy operate on arrays of different shapes without copying data, by virtually stretching smaller arrays.

The rules (compare shapes from the right)#

  1. If dimensions differ in count, left-pad the smaller shape with 1s.
  2. Two dimensions are compatible if they are equal or one of them is 1.
  3. A size-1 dimension is stretched to match the other.
  4. If any dimension is incompatible → ValueError.
flowchart TD
    A["A · shape (3, 4)"] --> C{"align shapes from the right"}
    B["B · shape (4,) → padded to (1, 4)"] --> C
    C -->|"last dim: 4 == 4 ✓ · next dim: 3 vs 1 → stretch"| D["B virtually stretched to (3, 4)"]
    D --> E["element-wise result · shape (3, 4)"]
A      (3, 4)
B         (4,)   -> treated as (1, 4) -> stretched to (3, 4)  ✅

A      (3, 1)
B      (1, 4)    -> both stretch -> (3, 4)  ✅

A      (3, 4)
B         (3,)   -> (1, 3) vs (3, 4): 3 != 4  ❌ ValueError

Examples#

a = np.array([[1], [2], [3]])   # shape (3, 1)
b = np.array([10, 20, 30])      # shape (3,)
a + b
# [[11 21 31]
#  [12 22 32]
#  [13 23 33]]                   # outer sum, shape (3, 3)

m = np.arange(12).reshape(3, 4)
col_mean = m.mean(axis=0)       # shape (4,)
centered = m - col_mean         # subtract column means (broadcast)

💡 Tip: To force a column vector for broadcasting: col = a.reshape(-1, 1) or a[:, None].

Interview Question: Explain broadcasting. NumPy aligns shapes from the right, stretches size-1 dimensions, and applies the op element-wise without materializing the stretched copies — saving memory and time.

Real-world use case: Normalizing an image: img - img.mean(axis=(0,1)) broadcasts per-channel means across all pixels.