Python 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.

On this page, arr is a numpy array (np.ndarray), and the letters inside the diagrams are its values.

Colons (:) #

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

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

\[\def\cel#1{\kern{-5pt}\boxed{\hphantom{n}\mathclap{\vphantom{(}#1}\hphantom{n}}\kern{-5pt}} \begin{array}{ccccc} \cel{a} & \cel{\color{#3b82f6}{b}} & \cel{\color{#3b82f6}{c}} & \cel{\color{#3b82f6}{d}} & \cel{e}\\ \texttt{0} & \color{#3b82f6}{\texttt{1}} & \color{#3b82f6}{\texttt{2}} & \color{#3b82f6}{\texttt{3}} & \texttt{4} \\ \end{array} \quad = \quad \begin{bmatrix} \color{#3b82f6}{b} & \color{#3b82f6}{c} & \color{#3b82f6}{d} \end{bmatrix}\]

Remember that slicing stops at item #3 (=4-1) since Python slicing does not include the upper-bound.

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

\[\def\cel#1{\kern{-5pt}\boxed{\hphantom{n}\mathclap{\vphantom{(}#1}\hphantom{n}}\kern{-5pt}} \begin{array}{ccccc} \cel{\color{#3b82f6}{a}} & \cel{b} & \cel{\color{#3b82f6}{c}} & \cel{d} & \cel{\color{#3b82f6}{e}} \\ \color{#3b82f6}{\texttt{0}} & \texttt{1} & \color{#3b82f6}{\texttt{2}} & \texttt{3} & \color{#3b82f6}{\texttt{4}} \\ \end{array} \quad = \quad \begin{bmatrix} \color{#3b82f6}{a} & \color{#3b82f6}{c} & \color{#3b82f6}{e} \end{bmatrix}\]

Note that arr[:] and arr[::] 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.

arr[1,2] means arr[1][2].

\[\def\cel#1{\kern{-5pt}\boxed{\hphantom{n}\mathclap{\vphantom{(}#1}\hphantom{n}}\kern{-5pt}} \begin{array}{rccc} & \texttt{0} & \texttt{1} & \color{#c2620a}{\texttt{2}} \\ \texttt{0} & \cel{a} & \cel{b} & \cel{c} \\ \color{#c2620a}{\texttt{1}} & \cel{d} & \cel{e} & \cel{\color{#c2620a}{f}} \\ \texttt{2} & \cel{g} & \cel{h} & \cel{i} \\ \texttt{3} & \cel{j} & \cel{k} & \cel{l} \\ \texttt{4} & \cel{m} & \cel{n} & \cel{o} \\ \end{array} \quad = \quad \color{#c2620a}{f}\]

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

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

Mixing commas and colons #

Commas get confusing when mixed with colons.

arr[:,1] means all rows, column 1

\[\def\cel#1{\kern{-5pt}\boxed{\hphantom{n}\mathclap{\vphantom{(}#1}\hphantom{n}}\kern{-5pt}} \begin{array}{rccc} & \texttt{0} & \color{#c2620a}{\texttt{1}} & \texttt{2} \\ \color{#c2620a}{\texttt{0}} & \cel{a} & \cel{\color{#c2620a}{b}} & \cel{c} \\ \color{#c2620a}{\texttt{1}} & \cel{d} & \cel{\color{#c2620a}{e}} & \cel{f} \\ \color{#c2620a}{\texttt{2}} & \cel{g} & \cel{\color{#c2620a}{h}} & \cel{i} \\ \color{#c2620a}{\texttt{3}} & \cel{j} & \cel{\color{#c2620a}{k}} & \cel{l} \\ \color{#c2620a}{\texttt{4}} & \cel{m} & \cel{\color{#c2620a}{n}} & \cel{o} \\ \end{array} \quad = \quad \begin{bmatrix} \color{#c2620a}{b} & \color{#c2620a}{e} & \color{#c2620a}{h} & \color{#c2620a}{k} & \color{#c2620a}{n} \end{bmatrix}\]

Note: this is not the same as arr[:][1]. arr[:] selects every row, then [1] picks row 1, giving [d, e, f]. The arr[1,2] = arr[1][2] rule only holds when every index is a single number, not a slice.

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

arr[0:4,1] means rows 0 to 3, column 1

\[\def\cel#1{\kern{-5pt}\boxed{\hphantom{n}\mathclap{\vphantom{(}#1}\hphantom{n}}\kern{-5pt}} \begin{array}{rccc} & \texttt{0} & \color{#c2620a}{\texttt{1}} & \texttt{2} \\ \color{#c2620a}{\texttt{0}} & \cel{a} & \cel{\color{#c2620a}{b}} & \cel{c} \\ \color{#c2620a}{\texttt{1}} & \cel{d} & \cel{\color{#c2620a}{e}} & \cel{f} \\ \color{#c2620a}{\texttt{2}} & \cel{g} & \cel{\color{#c2620a}{h}} & \cel{i} \\ \color{#c2620a}{\texttt{3}} & \cel{j} & \cel{\color{#c2620a}{k}} & \cel{l} \\ \texttt{4} & \cel{m} & \cel{n} & \cel{o} \\ \end{array} \quad = \quad \begin{bmatrix} \color{#c2620a}{b} & \color{#c2620a}{e} & \color{#c2620a}{h} & \color{#c2620a}{k} \end{bmatrix}\]

arr[0:4:2,1] means every 2nd row from 0 to 3, column 1

\[\def\cel#1{\kern{-5pt}\boxed{\hphantom{n}\mathclap{\vphantom{(}#1}\hphantom{n}}\kern{-5pt}} \begin{array}{rccc} & \texttt{0} & \color{#c2620a}{\texttt{1}} & \texttt{2} \\ \color{#c2620a}{\texttt{0}} & \cel{a} & \cel{\color{#c2620a}{b}} & \cel{c} \\ \texttt{1} & \cel{d} & \cel{e} & \cel{f} \\ \color{#c2620a}{\texttt{2}} & \cel{g} & \cel{\color{#c2620a}{h}} & \cel{i} \\ \texttt{3} & \cel{j} & \cel{k} & \cel{l} \\ \texttt{4} & \cel{m} & \cel{n} & \cel{o} \\ \end{array} \quad = \quad \begin{bmatrix} \color{#c2620a}{b} & \color{#c2620a}{h} \end{bmatrix}\]

arr[::2,1] means every 2nd row, column 1

\[\def\cel#1{\kern{-5pt}\boxed{\hphantom{n}\mathclap{\vphantom{(}#1}\hphantom{n}}\kern{-5pt}} \begin{array}{rccc} & \texttt{0} & \color{#c2620a}{\texttt{1}} & \texttt{2} \\ \color{#c2620a}{\texttt{0}} & \cel{a} & \cel{\color{#c2620a}{b}} & \cel{c} \\ \texttt{1} & \cel{d} & \cel{e} & \cel{f} \\ \color{#c2620a}{\texttt{2}} & \cel{g} & \cel{\color{#c2620a}{h}} & \cel{i} \\ \texttt{3} & \cel{j} & \cel{k} & \cel{l} \\ \color{#c2620a}{\texttt{4}} & \cel{m} & \cel{\color{#c2620a}{n}} & \cel{o} \\ \end{array} \quad = \quad \begin{bmatrix} \color{#c2620a}{b} & \color{#c2620a}{h} & \color{#c2620a}{n} \end{bmatrix}\]

Ellipsis (...) #

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

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

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

arr[..., 3] is the same as arr[:, :, :, 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 arr has shape (2, 3, 1)

arr[:,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
  • arr[:,1] is not the same as arr[:][1]. arr[:]. The arr[1,2] = arr[1][2] rule only holds when every index is a single number, not a slice.

August 2, 2020