Sanj's Notes

Numpy Indexing

Numpy allows basic and advanced indexing techniques to help slice multi-dimensional arrays. These techniques are not present in basic Python and are only unique to numpy.

Colons (:) #

Colons work the same way as the standard Python indexing format (start:stop:step).

a[1:4] means all items between index 1 and 3 (inclusive)1

a[::2] means every 2nd item from the array

Note that a[:] and a[::] are equivalent (empty start, stop and step values).


Commas (,) #

Commas separate indexing performed at each dimension.

If you see a comma in your numpy array index, think of it like moving to the next dimension of the array.

a[1,2] means a[1][2].

a[0,2,3] means a[0][2][3].

You can’t use more commas than the number of dimensions minus 1.

Commas (,) get confusing when mixed with colons (:). The easiest way to think about is that the commas separate the indexing performed at each dimension.

a[:,1] means a[:][1]

If you supply start, stop and step values, you can filter the items being selected.

a[0:4,1] means a[0:4][1]

a[0:4:2,1] means a[0:4:2][1]

a[::2,1] means a[::2][1]


Ellipsis (...) #

Ellipsis are a shorthand for 0 or more colons (:).

For example, if a is a 10 x 20 x 30 x 40 matrix,

a[3:, :, :, 4] is the same as a[3:, ..., 4]

a[..., 3] is the same as a[:, :, :, 3].

There can only ever be one ellipsis (...) inside of an index.


New axis (np.newaxis) #

np.newaxis inserts a 1 unit-length dimension when indexing an array.

If a has shape (2, 3, 1)

a[:,np.newaxis,:,:].shape will be (2, 1, 3, 1)


Trivia #

  • Slicing can’t expand the size of the array.
  • Basic indexing returns a view of the data, whilst triggering advanced indexing returns a copy of the data

  1. Slicing stops at item #3 (=4-1) since Python slicing is upper-bound exlusive. Read more. ↩︎


August 2, 2020