Index Ai Academy
Phase 0 · ToolsWorking with data

NumPy in practice: shapes, types and picking values

About 35 minLooking ahead: some prerequisites aren't done yet

Why this matters

Every input to a model is an array with a shape, and most first-week bugs are shape or type bugs: an axis that vanished, a uint8 that wrapped around, a reshape that scrambled pixels, a slice that changed its parent. This lesson names the handful of NumPy moves that the image project, pandas and PyTorch all lean on, so you meet them once, slowly, before they appear mixed into larger code.

  • Read and change an array's shape with reshape (including -1), .T and a new axis
  • Choose between a view and a copy, and say which a slice, a reshape and .copy() give
  • Pick values with np.where, np.clip, a mask and argsort, and join arrays with concatenate and stack
  • Turn a batch of images into the (N, C, H, W) float tensor a PyTorch model expects
Quick check
A guess before you start: how many numbers does an array of shape (3, 4) hold?

The idea: one block of numbers, a shape and a dtype

This lesson rests on NumPy arrays and the Foundation's Tensors and broadcasting. An array holds its numbers in one block of memory. Two small labels say how to read the block: its shape (how the numbers are laid out in rows and columns) and its dtype (what kind of number each cell holds).

The same twelve numbers can be read as a list of 12, a grid of 3 rows and 4 columns, or two grids of 2 by 3. Changing the shape changes the labels, not the numbers:

import numpy as np

a = np.arange(12)
print(a.shape)
print(a.reshape(3, 4).shape)
print(a.reshape(2, 2, 3).shape)
print(a.reshape(2, -1).shape)

The -1 means "work this one out": 12 numbers in 2 rows leaves 6 columns. Every model input is an array with a shape like this, so most AI bugs are shape bugs.

The dtype matters just as much. Whole numbers are int64 by default on most computers (int32 here in the browser, as the first line below prints), numbers with a decimal point are float64, and pixels are uint8: whole numbers from 0 to 255 in one byte each. PyTorch's default is float32, half the size of float64, which is why a model's weights fit in memory. A mismatch between the two is a common error message: the cure is .astype in NumPy and .float() in PyTorch.

import numpy as np

print(np.array([1, 2]).dtype, np.array([1, 2.5]).dtype)
print(np.array([1, 2.5], dtype=np.float32).dtype)
small = np.array([250, 5], dtype=np.uint8)
print(small.dtype, (small + 10).tolist())

A decimal anywhere in a list makes the whole array float. A uint8 can't go above 255, so a sum that does wraps around instead of growing.