Array indexing

Elements from NumPy arrays can be selected using four methods: scalar selection, slicing, numerical indexing, and logical (or Boolean) indexing. Scalar selection and slicing are the basic methods to access elements in an array, which has already been discussed here. Numerical indexing and logical indexing are closely related and allows more flexible selection. Numerical indexing uses lists or arrays of locations to select elements, whereas logical indexing uses arrays that contain Boolean values to select elements.

Numerical indexing

Numerical indexing is an alternative to slice notation. The idea in numerical indexing is to use coordinates to select elements. This is similar to slicing. Arrays created using numerical indexing create copies of data, whereas slices are only views of data, and not copies. For performance sake, slicing should be used. Slices are similar to one-dimensional arrays, but the shape of the slice is determined by the slice inputs.

Numerical indexing in one-dimensional arrays uses the numerical index values as locations in the array (0-based indexing) and returns an array with the same dimensions as the numerical index.

Note that the numerical index can be either a list or a NumPy array and must contain integer data, as shown in the following code:

a = 10 * arange(4.0)
array([0.,10.,20.,30.])

a[[1]]  # arrays index is list with first element 
array([ 10.])

a[[0,3,2]] # arrays index are 0-th, 3-rd and 2-nd
array([  0.,  30.,  20.])

sel = array([3,1,4,2,3,3])  # array with repetition
a[sel]
array([ 30.  10.   0.  20.  30.  30.])

sel = array([4,1],[3,2]])
a[sel]
array([[ 30.,10.], [ 0.,20.]])

These examples show that the numerical indices determine the element location, and the shape of the numerical index array determines the shape of the output.

Similar to slicing, numerical indexing can be combined using the flat function to select elements from an array using the row-major ordering of the array. The behavior of numerical indexing with flat is identical to that of using numerical indexing on a flattened version of the underlying array. A few examples are shown here:

a = 10 * arange(8.0)
array([  0.,  10.,  20.,  30.,  40., 50., 60., 70.])

a.flat[[3,4,1]]
array([ 30., 40., 10.])

a.flat[[[3,4,7],[1,5,3]]]
array([[ 30., 40., 70.], [ 10., 50., 30.]])

Logical indexing

Logical indexing is different from slicing and numeric indexing; it rather uses logical indices to select elements, rows, or columns. Logical indices act as light switches and are either true or false. Pure logical indexing uses a logical indexing array with the same size as the array being used for selection and always returns a one-dimensional array, as shown in the following code:

x = arange(-4,5)

x < 0 
array([True, True, True, True, False, False, False, False, False], dtype=bool)

x[x>0]
array([1, 2, 3, 4])

x[abs(x) >= 2]  
array([-4, -3, -2,  2,  3,  4])

#Even for 2-dimension it still does the same
x = reshape(arange(-8, 8), (4,4))
x
array([[-8, -7, -6, -5], [-4, -3, -2, -1], [ 0,  1,  2,  3], [ 4,  5,  6,  7]])

x[x<0]
array([-8, -7, -6, -5, -4, -3, -2, -1])

Here is another example to demonstrate logical indexing:

from math import isnan
a = [[3, 4, float('NaN')], [5, 9, 8], [3, 3, 2], [9, -1, float('NaN')]]

list2 = [3, 4, 5, 6]
list1_valid = [elem for elem in list1 if not any([isnan(element) for element in elem])]

list1_valid
[[3, 7, 8], [1, 1, 1]] 

list2_valid = [list2[index] for index, elem in enumerate(list1) if not any([isnan(element) for element in elem])]

list2_valid
 [4, 5]
..................Content has been hidden....................

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