NumPy arrays, shapes and indexing
A NumPy array is a grid of numbers, all of the same type, with a shape that says how long it is in each direction.
Why it matters
ML is arithmetic on huge piles of numbers: a dataset is a table, an image is a grid of pixels, and a batch of images is a stack of grids. Python lists are slow for this and have no idea of rows and columns.
NumPy arrays keep the numbers packed together in memory, and run maths on all of them at once in fast compiled code. They also let you pick out any row, column or block with one short expression.
The idea
An array is a grid of numbers of the same type. Its shape tells you how many numbers there are along each direction. Shape (3, 4) means 3 rows and 4 columns.
A spreadsheet. The shape is the number of rows and columns. Indexing means picking one cell, a whole row or a whole column.
A spreadsheet has only two directions. An array can have three or more, like a stack of spreadsheets. A batch of colour images has shape (batch, height, width, 3).
How it works
- np.arange(12) makes the numbers 0 to 11 as a one-dimensional (1-D) array. Its shape is (12,): one direction, 12 long. The trailing comma just means "a tuple with one entry".
- reshape(3, 4) shows the same 12 numbers as 3 rows of 4. The total has to stay the same: 3 × 4 = 12. A.ndim is 2 (the number of directions, called axes) and A.size is 12.
- Index with one number per axis, counting from 0: A[1, 2] is row 1, column 2, which is 6. Negative numbers count from the end, so A[-1, -1] is 11.
- Slice with start:stop, where the stop is not included: A[0, 1:3] is [1, 2]. A colon on its own means "all of this axis", so A[:, 0] is the whole first column, [0, 4, 8].
- Leave out the later indexes and you get whole rows: A[1] is [4, 5, 6, 7].
- Keep only the values that pass a test with a boolean mask. A > 6 is a grid of True and False the same shape as A, and A[A > 6] is [7, 8, 9, 10, 11].
- Slices are views: B = A[0] shares memory with A, so changing B changes A as well. Call .copy() when you need a separate array. Masks always make a copy.
Key points
- shape is a tuple: (12,) is 1-D, (3, 4) is 2-D. The number of entries in the shape is ndim, the number of axes.
- Axis 0 runs down the rows; axis 1 runs across the columns. A.sum(axis=0) adds down each column and has shape (4,).
- Indexing counts from 0, and a slice leaves out its stop value: 1:3 means positions 1 and 2.
- Basic slices return views that share memory. Boolean masks and lists of indexes return copies.
- Every value has the same dtype, such as float64 or int64. Put one decimal into a list of whole numbers and the whole array becomes float64.
- Shapes to recognise in ML: a table of data is (n_samples, n_features); a batch of colour images is (batch, height, width, 3).
Common mistakes
- Confusing (3,) with (3, 1) or (1, 3). They hold the same three numbers but behave differently in maths and broadcasting.
- Off-by-one slices: A[0:2] gives two rows, not three.
- Changing a slice and being surprised that the original changed too. Slices are views.
- Looping over array elements in Python. One array operation is often 10 to 100 times faster.
Not true: Changing a slice of a NumPy array leaves the original array untouched.
In fact: A slice is a view: it shares memory with the original, so changing it changes the original too. Call .copy() when you need a separate array.
Not true: A[0:2] selects three rows: rows 0, 1 and 2.
In fact: The end of a slice is left out, so A[0:2] gives two rows: row 0 and row 1.
Part of Unit 0: Your toolkit in the free ML Foundations course.