reshape, and -1
What it is
reshape(shape) reinterprets the same buffer with a new shape, in row-major order by default. One axis may be -1, meaning 'infer this from the total size'.
Why it matters
It is the shape-plumbing between a model and its data — flattening a batch of images, adding the feature axis a estimator expects — and it is free whenever the array is contiguous.
In the real world: Turning 8,760 hourly readings into a 365-days-by-24-hours grid to see the daily pattern of electricity use.
Visual example
reshapereshape re-reads the same flat buffer in row-major order — the values keep their order, only the folding changes.
The buffer never changes
flat: 0 1 2 3 4 5
(2, 3): rows of 3
(3, 2): rows of 2
Why this matters
Shape errors between data loaders, feature matrices and models are almost always solved by one reshape — and knowing it is free means you can use it freely.
Common misconception
Wrong idea: reshape rearranges the values to fit the new shape.
Correct idea: it changes nothing but the shape. The flat order 0 1 2 3 4 5 is read in rows of the new width. To reorder axes you need transpose.
x = np.arange(6)
x.reshape(2, 3) # [[0, 1, 2], [3, 4, 5]] — row-major fill
x.reshape(-1, 2) # (3, 2): the -1 is inferred
x.reshape(3, 2).shape
imgs.reshape(len(imgs), -1) # flatten each image, keep the batch axisReshaping to reorder axes. reshape never moves data, so turning (batch, h, w) into (h, w, batch) with reshape interleaves the images into nonsense. Axis reordering is transpose, which does move data when you later ask for it contiguous.
Practise it: Flatten a Batch of Images