Day 3: NumPy fundamentals: arrays as the mental model
The array is the mental model for everything after
Every technology in this roadmap — a garment photo, a batch of training data, a model's weights, a paragraph's embedding — is, underneath, a NumPy-style array. Get this abstraction solid now and PyTorch tensors (Stage 1), OpenCV images (today onward), and pgvector embeddings (Stage 3) all become 'the same shape of idea, different label'.
Why arrays, not Python lists
A Python list can hold anything, anywhere in memory — each element is a separate object with its own type tag and pointer. A NumPy ndarray demands one fixed dtype for every element and stores them contiguously in a single memory block. That uniformity is exactly what lets NumPy hand a whole array to a tight, vectorized C loop instead of a slow, per-element Python loop.
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
a.shape # (2, 3) — 2 rows, 3 columns
a.dtype # dtype('int64') — every element is the SAME type
a.ndim # 2 — a "rank-2" array (a matrix)
a.size # 6 — total element count
# a garment photo, once loaded, is exactly this shape of thing:
# (height, width, channels) — a rank-3 array of uint8 (0-255)The spreadsheet analogy
Think of an ndarray as a spreadsheet with a hard rule: every cell must be the same data type, and the sheet's dimensions are fixed once created. A color image is a 3D 'spreadsheet' — rows, columns, and a third axis for the Red/Green/Blue channels.
Axes
Operations like sum, mean, and max take an axis argument telling NumPy which dimension to collapse. axis=0 collapses rows (down each column); axis=1 collapses columns (across each row). This trips up almost everyone once — memorize it now rather than during a debugging session in Stage 1.
a = np.array([[1, 2, 3], [4, 5, 6]])
a.sum(axis=0) # array([5, 7, 9]) — summed DOWN each column
a.sum(axis=1) # array([6, 15]) — summed ACROSS each rowKey terms
- ndarray
- NumPy's core data type: a fixed-dtype, fixed-shape, contiguous grid of numbers.
- dtype
- The single data type shared by every element in an array, e.g. float32, uint8, int64.
- shape
- A tuple giving the size of each dimension of an array, e.g. (height, width, channels).
- axis
- A specific dimension of an array that a reduction operation (sum, mean, max...) collapses along.
A color garment photo, loaded as a NumPy array, has shape (480, 640, 3). What does the "3" represent?