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

  1. 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".
  2. 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.
  3. 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.
  4. 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].
  5. Leave out the later indexes and you get whole rows: A[1] is [4, 5, 6, 7].
  6. 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].
  7. 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

Common mistakes

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.