Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 49 ยท Array Math

Broadcasting

Understand NumPy broadcasting step by step: the shape rules, scalar, row and column cases, np.newaxis, multiplication tables and how to fix shape mismatch errors.

In Vectorized Operations and ufuncs both operands had the same shape. Real work rarely looks like that. You want to subtract one average from a whole table, add a bias vector to every row, or build a multiplication table from two short lists. Broadcasting is the set of rules NumPy uses to combine arrays of different shapes without writing loops and without actually copying data.

Broadcasting is simple once you know the rules, and confusing until you do. This lesson states the rules, walks through scalar, row and column cases, shows how np.newaxis lines axes up, builds a multiplication table, and ends with the shape-mismatch error you will meet sooner or later.

The rules

When NumPy combines two arrays it compares their shapes from the last axis backwards:

  1. If the arrays have different numbers of dimensions, the shorter shape is padded with 1s on the left.
  2. Two sizes along an axis are compatible if they are equal, or if one of them is 1.
  3. An axis of size 1 is stretched to match the other size. The result has, on each axis, the larger of the two sizes.
  4. If any pair is incompatible (different and neither is 1), NumPy raises an error.
flowchart TB
    A["A: shape (3, 4)"] --> C{"compare from<br/>the right"}
    B["B: shape (4,)"] --> P["pad left: (1, 4)"]
    P --> C
    C --> D["4 vs 4: equal"]
    C --> E["3 vs 1: stretch the 1"]
    D --> R["result shape (3, 4)"]
    E --> R

Shapes are aligned on the right, padded with 1s on the left, and size-1 axes stretch to match.

NumPy does not literally copy the smaller array. It reuses the same numbers while stepping through memory, which is why broadcasting is both fast and cheap on memory.

Scalars

The simplest case: a scalar behaves like an array of shape (), padded to match anything.

Python Scalar broadcasting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A row against a table

A 1D array of length 3 combined with a table of shape (2, 3): the vector is treated as shape (1, 3), then stretched down both rows.

Python Adding a row to every row

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The row [1, 2, 3] is added to the first row and to the second row. Centering data by column is the classic use: table.mean(axis=0) has one number per column, which lines up with the last axis.

A column against a table

What if you want to subtract a value per row instead? A 1D array of length 2 will not line up with a (2, 3) table: the alignment from the right compares 2 with 3 and fails. You need a column of shape (2, 1).

Python Row means need a column shape

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The keepdims=True option, which you will meet again in Aggregations and Axes, keeps the reduced axis with size 1 and is the neatest way to get a broadcast-ready shape.

Adding axes with np.newaxis

np.newaxis (an alias of None) inside an index inserts a new axis of size 1. It converts a flat vector into a row or a column without changing its numbers.

Python Row and column from one vector

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When a row and a column meet, both stretch: the column spreads across the columns and the row spreads down the rows, giving a full grid. This is the key to table-style calculations.

flowchart LR
    C["col (3, 1)<br/>1<br/>2<br/>3"] --> G["result (3, 3)"]
    R["row (1, 3)<br/>10 20 30"] --> G

A (3, 1) column and a (1, 3) row broadcast to a (3, 3) grid.

Outer tables

Because a column and a row produce every pairing, you can build tables with one expression:

Python Multiplication table without loops

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

None is simply another spelling of np.newaxis, and you will see x[:, None] everywhere in NumPy code. The distance-matrix pattern generalizes to points in 2D and 3D, and powers the K-means project later in the tutorial.

Checking the result shape

You can predict a broadcast result yourself, or ask NumPy: np.broadcast_shapes(*shapes) returns the result shape or raises an error.

Python Predicting shapes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Work the third one through: (5, 1, 3) and (4, 1) pad to (5, 1, 3) and (1, 4, 1). Axis by axis you get 5 vs 1, 1 vs 4 and 3 vs 1, so the answer is (5, 4, 3).

Reading the shape-mismatch error

When the rules fail you get a message like "operands could not be broadcast together with shapes (3,4) (3,)". Read the two shapes, align them on the right and find the first pair that is different with neither equal to 1. The usual fix is one of these:

  • Give the smaller array a trailing axis of size 1: v[:, None] or v.reshape(-1, 1).
  • Use keepdims=True on the reduction that produced it.
  • Transpose one of the arrays if the axes are simply swapped.

Warning: Broadcasting can also hide mistakes. Adding a vector of shape (n,) to a column of shape (n, 1) does not fail; it silently makes an (n, n) grid. If a result has a surprising shape, print the .shape of both inputs.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the result shape of adding arrays of shape (3, 1) and (1, 4)?

    Each axis has a 1 on one side, so both stretch: 3 vs 1 gives 3 and 1 vs 4 gives 4.
  2. 2. Which pair of shapes cannot be broadcast together?

    Aligned on the right, (5,) is compared with the last axis of size 3 and 5 does not equal 3. A column (5, 1) would work.
  3. 3. How do you subtract each row's mean from a 2D array a?

    Row means have one value per row. keepdims=True keeps them as a column of shape (rows, 1) so they broadcast across each row.
  4. 4. What does v[:, np.newaxis] do to a vector of shape (4,)?

    np.newaxis inserts an axis of size 1 at that position; here it follows the first axis, giving a column.

Summary

  • Broadcasting aligns shapes from the right, pads missing axes with 1s on the left, and stretches axes of size 1.
  • Two sizes are compatible when they are equal or one of them is 1; otherwise NumPy raises a ValueError.
  • A row (n,) or (1, n) is applied to every row of a table; a per-row value needs a column (m, 1).
  • np.newaxis (or None) and keepdims=True create the size-1 axes you need.
  • A column combined with a row gives an outer grid, ideal for tables and pairwise distances.
  • np.broadcast_shapes predicts the result; print .shape when a result looks wrong.

Next: reduce arrays along axes in Aggregations and Axes.