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.

Ask about this picture
Array A
Array B

Result shape of a + b

(3, 4)

Align shapes on the right. A 1 stretches to match; a missing leading axis counts as a 1. Two disagreeing non-1 axes refuse. That is the whole rule.

Right-aligned axes

axisABout
031 → 33
1444

— no axis here (counts as 1) · 1 → n the 1 is stretched (no copy in memory: stride 0) · ✕ clash.

Ask the studio

Broadcasting shapes

This chat can see the instrument. Questions here are about this picture.

Exclusive to this lab — pick one, or type your own about the controls.