Slicing and indexing with NumPy arrays

Indexing allows us to take a view of a ndarray by pointing out either what slice of columns and rows to visualize, or an index:

  1. Let's define a working array:
In: import numpy as np
M = np.arange(10*10, dtype=int).reshape(10,10)
  1. Our array is a 10 x 10 two-dimensional array. We can initially start by slicing it into a single dimension. The notation for a single dimension is the same as that in Python lists:
[start_index_included:end_index_exclude:steps]
  1. Let's say that we want to extract even rows from 2 to 8:
In: M[2:9:2,:]   

Out: array([[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89]])
  1. After slicing the rows, we can slice the columns even further by taking only the columns from index 5:
In: M[2:9:2,5:]    

Out: array([[25, 26, 27, 28, 29],
[45, 46, 47, 48, 49],
[65, 66, 67, 68, 69],
[85, 86, 87, 88, 89]])
  1. As in lists, it is possible to use negative index values in order to start counting from the end. Moreover, a negative number for parameters, such as steps, reverses the order of the output array, like in the following example, where the counting starts from column index 5 but in the reverse order and goes toward index 0:
In: M[2:9:2,5::-1] 

Out: array([[25, 24, 23, 22, 21, 20],
[45, 44, 43, 42, 41, 40],
[65, 64, 63, 62, 61, 60],
[85, 84, 83, 82, 81, 80]])
  1. We can also create Boolean indexes that point out which rows and columns to select. Therefore, we can replicate the previous example by using a row_index and a col_index variable:
In: row_index = (M[:,0]>=20) & (M[:,0]<=80) 
col_index = M[0,:]>=5
M[row_index,:][:,col_index]

Out: array([[25, 26, 27, 28, 29],
[35, 36, 37, 38, 39],
[45, 46, 47, 48, 49],
[55, 56, 57, 58, 59],
[65, 66, 67, 68, 69],
[75, 76, 77, 78, 79],
[85, 86, 87, 88, 89]])

We cannot contextually use Boolean indexes on both columns and rows in the same square brackets, though we can apply the usual indexing to the other dimension using integer indexes. Consequently, we have to first operate a Boolean selection on rows and then reopen the square brackets and operate a second selection on the first, this time focusing on the columns.

  1. If we need a global selection of elements in the array, we can also use a mask of Boolean values, as follows:
In: mask = (M>=20) & (M<=90) & ((M / 10.) % 1 >= 0.5)
M[mask]

Out:
array([25, 26, 27, 28, 29, 35, 36, 37, 38, 39, 45, 46, 47, 48, 49,
55, 56, 57, 58, 59, 65, 66, 67, 68, 69, 75, 76, 77, 78, 79,
85, 86, 87, 88, 89])

This approach is particularly useful if you need to operate on the partition of the array selected by the mask (for example, M[mask]=0).

Another way to point out which elements need to be selected from your array is by providing a row or column index consisting of integers. Such indexes may be defined either by a np.where() function that transforms a Boolean condition on an array into indexes or by simply providing a sequence of integer indexes, where integers may be in a particular order or might even be repeated. Such an approach is called fancy indexing:

In: row_index = [1,1,2,7]
col_index = [0,2,4,8]

Having defined the indexes of your rows and columns, you have to apply them contextually to select elements whose coordinates are given by the tuple of values of both the indexes:

In: M[row_index,col_index]

Out: array([10, 12, 24, 78])

In this way, the selection will report the following points: (1,0),(1,2),(2,4), and (7,8). Otherwise, as seen previously, you just have to select the rows first and then the columns, which are separated by square brackets:

In: M[row_index,:][:,col_index]

Out: array([[10, 12, 14, 18],
[10, 12, 14, 18],
[20, 22, 24, 28],
[70, 72, 74, 78]])

Finally, please remember that slicing and indexing are just views of the data. If you need to create new data from such views, you have to use the .copy method on the slice and assign it to another variable. Otherwise, any modification to the original array will be reflected on your slice and vice versa. The copy method is shown here:

In: N = M[2:9:2,5:].copy()  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset