The Broadcasting Rules
The exact step-by-step algorithm NumPy uses to decide whether two shapes are compatible, how to compute the result shape, and a full compatibility table.
Watch the lesson
1 video on CodeWithMunnaX
Broadcasting is not magic and not guesswork — NumPy follows a short, strict algorithm. Once you can run this algorithm in your head, you can predict every broadcasting result (and every error) before you run the code.
Definition
Two shapes are broadcast-compatible when, comparing their dimensions from right to left, every pair of dimensions is either equal or one of them is 1. Missing dimensions on the left are treated as 1. The result shape takes the larger size in every position.
- Right to left — NumPy lines shapes up at their last (trailing) dimension, not the first.
- Equal or 1 — the only two ways a pair can be compatible.
5vs5is fine,1vs14is fine,5vs14is an error. - Missing dimensions = 1 — a
(14,)array next to a(5, 14)array is treated as(1, 14). - Larger size wins — the size-1 side is stretched to the other size.
The rules step by step
text
Step 1 Write the two shapes right-aligned, one under the other.
Step 2 If one shape is shorter, pad it on the LEFT with 1s.
Step 3 Compare each column (right to left):
equal -> OK, keep that size
one of them 1 -> OK, use the bigger size (stretch the 1)
anything else -> ValueError: cannot broadcast
Step 4 The result shape is the bigger size in every column.Worked example — (5, 14) with (14,):
text
A: 5 14
B: 14 -> pad left -> 1 14
compare: 5 vs 1 -> OK (stretch to 5)
14 vs 14 -> OK (equal)
Result: (5, 14)Worked example — (5, 14) with (5,) (fails):
text
A: 5 14
B: 5 -> pad left -> 1 5
compare: 14 vs 5 -> neither equal nor 1 -> ERRORWorked example — 3D (2, 1, 3) with (1, 4, 3):
text
A: 2 1 3
B: 1 4 3
2 4 3 <- 3 vs 3 equal, 1 vs 4 stretch, 2 vs 1 stretch
Result: (2, 4, 3)Note: Both arrays can be stretched at the same time. In
(3, 1)+(1, 3)the first stretches across columns and the second down rows, giving(3, 3).
Syntax — check shapes without doing the math
python
np.broadcast_shapes(shape_a, shape_b, ...) # returns result shape tuple| Parameter | Meaning | Default |
|---|---|---|
*shapes | two or more shape tuples (use () for a scalar) | required |
| returns | the broadcast result shape, or raises ValueError | — |
Example — let NumPy apply the rules
python
import numpy as np
print(np.broadcast_shapes((5, 14), (14,)))
print(np.broadcast_shapes((5, 14), (5, 1)))
print(np.broadcast_shapes((3, 1), (1, 3)))
print(np.broadcast_shapes((2, 1, 3), (1, 4, 3)))
print(np.broadcast_shapes((2, 3), ()))text
(5, 14)
(5, 14)
(3, 3)
(2, 4, 3)
(2, 3)python
np.broadcast_shapes((5, 14), (5,))text
ValueError: shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between arg 0 with shape (5, 14) and arg 1 with shape (5,).How it works: np.broadcast_shapes runs exactly the four steps above, without creating any arrays. It is the fastest way to answer "will this broadcast?" during debugging.
Compatibility table
Every row below was checked with np.broadcast_shapes:
| Shape A | Shape B | Possible? | Result shape | Reason |
|---|---|---|---|---|
(3,) | (3,) | ✅ Yes | (3,) | Same shape |
(3,) | (1,) | ✅ Yes | (3,) | 1 expands to 3 |
(2, 3) | (3,) | ✅ Yes | (2, 3) | Last dimension matches; B becomes (1, 3) |
(2, 3) | (1, 3) | ✅ Yes | (2, 3) | First dimension expands |
(2, 3) | (2, 1) | ✅ Yes | (2, 3) | Second dimension expands |
(3, 1) | (1, 3) | ✅ Yes | (3, 3) | Both arrays expand |
(4, 1) | (4,) | ✅ Yes | (4, 4) | (4,) → (1, 4), then both expand |
(2, 1, 3) | (1, 4, 3) | ✅ Yes | (2, 4, 3) | First and middle dimensions expand |
(2, 3, 4) | (3, 4) | ✅ Yes | (2, 3, 4) | Missing dimension treated as 1 |
(5, 2, 3) | (2, 3) | ✅ Yes | (5, 2, 3) | Leading dimension expands |
(2, 3) | () scalar | ✅ Yes | (2, 3) | Scalar broadcasts everywhere |
(2, 3) | (2, 2) | ❌ No | Error | 3 ≠ 2 and neither is 1 |
(3,) | (4,) | ❌ No | Error | 3 ≠ 4 |
(2, 3) | (4,) | ❌ No | Error | Last dimension 3 ≠ 4 |
(5, 14) | (5,) | ❌ No | Error | 14 ≠ 5 — needs (5, 1) |
Tip: Notice the row
(4, 1)with(4,)→(4, 4). This is a famous silent surprise: no error, but the result is a 4×4 grid, not 4 values. Always check.shapeof the result.
Common mistakes
- ❌ Aligning shapes from the left (
5with5in(5, 14)vs(5,)). ✅ Always align from the right. - ❌ Thinking "any small number stretches". ✅ Only a 1 (or a missing dimension) stretches. A 2 never stretches to 4.
- ❌ Assuming the result shape equals the first operand's shape. ✅ The result takes the max of each column — it can be bigger than both inputs, e.g.
(3, 1)+(1, 3)=(3, 3).
Interview tip
Q: Will (8, 1, 6, 1) broadcast with (7, 1, 5)? What is the result?
A: Pad B to (1, 7, 1, 5). Compare right to left: 1 vs 5 → 5, 6 vs 1 → 6, 1 vs 7 → 7, 8 vs 1 → 8. Result (8, 7, 6, 5).
Key points to remember
- Shapes are compared from the rightmost (trailing) dimension moving left.
- A pair of dimensions is compatible only if they are equal or one of them is 1.
- A shorter shape is padded with 1s on the LEFT: (14,) next to (5, 14) becomes (1, 14).
- The result shape takes the larger size in every position — it can be bigger than both inputs.
- Both operands can stretch at once: (3, 1) with (1, 3) gives (3, 3).
- np.broadcast_shapes() checks compatibility and gives the result shape without doing any math.
Try it yourself
Predict the result shape with np.broadcast_shapes
Both arrays stretch: (3, 1) + (1, 3)
Practice: run the rules yourself
Revision Flashcards
Tap a card to flip it and see the answer.
