Indexing and slicing arrays

There are two basic methods to access the data in a NumPy array; let's call that array for A. Both methods use the same syntax, A[obj], where obj is a Python object that performs the selection. We are already familiar with the first method of accessing a single element. The second method is the subject of this section, namely slicing. This concept is exactly what makes NumPy and SciPy so incredibly easy to manage.

The basic slice method is a Python object of the form slice(start,stop,step), or in a more compact notation, start:stop:step. Initially, the three variables, start, stop, and step are non-negative integer values, with start less than or equal to stop.

This represents the sequence of indices k = start + (i * step), where k runs from start to the largest integer k_max = start + step*int((stop-start)/step), or i from 0 to the largest integer equal to int((stop - start) / step). When a slice method is invoked on any of the dimensions of ndarray, it selects all elements in that dimension indexed by the corresponding sequence of indices. The simple example next illustrates this point:

>>> A=numpy.array([[1,2,3,4,5,6,7,8],[2,4,6,8,10,12,14,16]])
>>> print (A[0:2, 0:8:2])

The output is shown as follows:

[[ 1  3  5  7]
 [ 2  6 10 14]]

If start is greater than stop, a negative value of step is used to traverse the sequence backwards:

>>> print (A[0:2, 8:0:-2])

The output is shown as follows:

[[ 8,  6,  4,  2]
 [16, 12,  8,  4]]

Negative values of start and stop are interpreted as n-start and n-stop (respectively), where n is the size of the corresponding dimension. The A[0:2,-1:0:-2] command gives exactly the same output as the previous example.

The slice objects can be shortened by the absence of start (which implies a zero if step is positive, or the size of the dimension if step is negative), absence of stop (which implies the size of the corresponding dimension in case of positive step, or zero in case of negative step). Absence of step implies step is equal to 1. The :: object can be shortened simply as : for an easier syntax. The A[:,::-2] command then offers, yet again, the same output as the previous two.

The first nonbasic method of accessing data from an array is based on the idea of collecting several indices and requesting the elements in the array with those indices. For example, from our previous array A, we would like to construct a new array with the elements on locations (0, 0), (0, 3), (1, 2), and (1, 5). We do so by gathering the x and y values of the indices in respective lists, [0,0,1,1] and [0,3,2,5], and feeding these lists to A as an indexing object, as follows:

>>> print (A[ [0,0,1,1], [0,3,2,5] ])

The output is shown as follows:

[ 1  4  6 12]

Note how the result loses the dimension of the primitive array, and offers a one-dimensional array. If we desire to capture a subarray of A with indices in the Cartesian product of two sets of indices, respecting the row and column choice and creating a new array with the dimensions of the Cartesian product, we use the ix_ command. For instance, if in our previous array we would like to obtain the subarray of dimension 2 x 2 with indices in the Cartesian product of indices (0, 1) by (0,3) (these are the locations (0, 0), (0, 3), (1, 0), and (1, 3), we do so as follows:

>>> print (A[ numpy.ix_( [0,1], [0,3] )])

The output is shown as follows:

[[1 4]
 [2 8]]
..................Content has been hidden....................

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