Skip to content

NumPy

Thinking in Shapes

9 min+80 XPStep 1/10

Shape is the contract

.shape is the first thing experienced practitioners print. It tells you what the tensor is:

  • (1000, 20) — 1000 examples × 20 features: a dataset
  • (224, 224, 3) — height × width × RGB channels: one image
  • (32, 224, 224, 3) — a batch of 32 images
  • (128, 50, 768) — 128 sequences of 50 tokens, each a 768-dim vector: transformer activations

Convention runs deep: rows are examples, columns are features. When a model says it expects (batch, features), it's describing a shape contract — and most shape bugs are delivering the columns where the rows belong.

insight

Reading `(n_samples, n_features)` is reading the data's filing system. Nearly every error message in ML is, at heart, a shape disagreement.