Labs

Python / NumPy · How axes stretch

Broadcasting shapes

Pick two ndarray shapes and see whether NumPy can align them — and what the result looks like.

Array A
Array B

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

axisABout
0313
1444

A trailing axis

B trailing axis