Python / NumPy · How axes stretch
Broadcasting shapes
Pick two ndarray shapes and see whether NumPy can align them — and what the result looks like.
Result shape
(3, 4)
Align shapes on the right. A 1 stretches; a missing leading axis is a 1. Two disagreeing non-1 axes refuse. That is the whole rule.
Right-aligned axes
| axis | A | B | out |
|---|---|---|---|
| 0 | 3 | 1 | 3 |
| 1 | 4 | 4 | 4 |
A trailing axis
B trailing axis