1.4.5 · D1Python & Scientific Computing

Foundations — NumPy broadcasting rules

1,934 words9 min readBack to topic

Before you can read a single broadcasting rule, you need a vocabulary. The parent note throws around words like shape, dimension, axis, trailing, singleton, 0-D scalar, and symbols like (2, 3), [:, np.newaxis], (). This page builds every one of them from nothing, in an order where each idea rests on the one before it.


1. What is an "array"?

The word "array" only means "orderly arrangement". The pictures below are the whole idea.

Figure — NumPy broadcasting rules

Look at the four panels:

  • Panel 1 — a lone number. You cannot walk anywhere; there are zero directions to move.
  • Panel 2 — a row. There is exactly one direction to walk: left↔right.
  • Panel 3 — a table. There are two directions: down↔up (rows) and left↔right (columns).
  • Panel 4 — a stack of tables. Three directions: which sheet, which row, which column.

That "number of directions you can walk" is the single most important idea on the whole page, so it gets its own name next.


2. Dimension, axis, rank

The axes are numbered starting from the left, beginning at 0:

Figure — NumPy broadcasting rules

For a table (rank 2):

  • axis 0 points down the rows (the vertical direction).
  • axis 1 points across the columns (the horizontal direction).

3. Shape and the tuple notation (...)

Now we can decode the symbol you saw everywhere on the parent page: (2, 3).

Special cases of shape, all of which appear on the parent page:

Written shape Rank Picture Note
() 0 one lone number empty tuple — no axes at all
(3,) 1 a row of 3 the lonely comma means "one axis"
(3, 1) 2 a column of 3 3 rows, 1 column
(1, 3) 2 a row of 3, but 2-D 1 row, 3 columns
(2, 3) 2 a 2×3 table

Why the trailing comma in (3,)? Because (3) in Python is just the number 3 in brackets; the comma is what tells Python "this is a one-item tuple, i.e. a shape".


4. The singleton: a dimension of size 1

Figure — NumPy broadcasting rules

The figure shows a (3, 1) column (one slice along axis 1) being virtually stretched into (3, 3). Notice the arrows: no new memory is created — the same three numbers are re-read three times. That is why broadcasting is fast and memory-light.


5. Element-wise operations


6. Trailing / rightmost dimension


7. Right-alignment and left-padding

This is the mechanical step the parent page performs in every example. When two arrays have different ranks, NumPy lines their shapes up flush to the right, then fills the shorter one with 1s on the left until both have equal length.

Figure — NumPy broadcasting rules

8. np.newaxis — inserting a fresh axis


How the foundations feed the topic

Array = grid of numbers

Axis and rank

Shape tuple like 2 3

Singleton axis of size 1

Trailing rightmost dimension

Element-wise operation

Right-align and left-pad

np.newaxis inserts axis

Broadcasting rules

Read it top-to-bottom: you cannot understand right-alignment until you understand shape and singletons; you cannot understand np.newaxis until you understand axes; and every arrow eventually funnels into the broadcasting rules node. Broadcasting also feeds forward into practical uses like feature normalization and vectorization, and it depends on the memory-efficient patterns that make the "no-copy" trick possible.


Equipment checklist

Test yourself — cover the right side and answer before revealing.

What is the shape of a lone number (a scalar)?
() — the empty tuple, rank 0, no axes at all.
How many axes does an array of shape (3, 1) have?
Two axes (rank 2): 3 down axis 0, 1 across axis 1.
Which direction does axis 0 point in a 2-D table?
Down the rows (vertical), not across.
What is a singleton dimension and why does it matter?
An axis of size 1; it can be virtually stretched to any size (Rule 2 of broadcasting).
What is the trailing dimension of shape (2, 3, 4)?
4 — the rightmost, fastest-changing axis.
When two shapes have different ranks, where does NumPy add the padding 1s?
On the LEFT (outermost side), flush-aligning the shapes to the right.
What does a[:, np.newaxis] do to a (3,) array?
Turns it into shape (3, 1) by inserting a new singleton axis on the right.
Why does NumPy compare dimensions starting from the right?
Because arrays are stored row-major, so the rightmost index changes fastest in memory — right-to-left matching is cache-friendly.
Is A * B between two NumPy arrays a dot product?
No — it is element-wise multiplication, pairing same-position elements.