NumPy Advanced Indexing
NumPy provides more indexing methods than a typical Python sequence.
In addition to the integer and slice indexing seen earlier, arrays can be indexed by integer arrays, boolean indexing, and fancy indexing.
Advanced indexing in NumPy refers to using integer arrays, boolean arrays, or other sequences to access array elements. Compared with basic indexing, advanced indexing can access any element in an array and can be used to perform complex operations and modifications on arrays.
Integer Array Indexing
Integer array indexing refers to using one array to access elements of another array. Each element in this array is an index value along a certain dimension of the target array.
The following example gets the element in the array(0,0),(1,1)and(2,0)at the given position.
Example
The output result is:
[1 4 5]
The following example gets the elements of the four corners of a 4X3 array. The row indices are [0,0] and [3,3], and the column indices are [0,2] and [0,2].
Example
The output result is:
我们的数组是: [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] 这个数组的四个角元素是: [[ 0 2] [ 9 11]]
The returned result is an ndarray object containing each corner element.
You can use slicing:or…combined with index arrays. As in the following example:
Example
The output result is:
[[5 6] [8 9]] [[5 6] [8 9]] [[2 3] [5 6] [8 9]]
Boolean Indexing
We can index the target array with a boolean array.
Boolean indexing uses boolean operations (such as comparison operators) to obtain an array of elements that meet the specified condition.
The following example gets elements greater than 5:
Example
The output result is:
我们的数组是: [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] 大于 5 的元素是: [ 6 7 8 9 10 11]
The following example uses~(the complement operator) to filter NaN.
Example
The output result is:
[ 1. 2. 3. 4. 5.]
The following example demonstrates how to filter out non-complex elements from an array.
Example
The output is as follows:
[2.0+6.j 3.5+5.j]
Fancy Indexing
Fancy indexing refers to indexing using integer arrays.
Fancy indexing takes values according to the values of the index array as subscripts on a certain axis of the target array.
When using a one-dimensional integer array as an index, if the target is a one-dimensional array, the indexing result is the element at the corresponding position; if the target is a two-dimensional array, it is the row corresponding to the subscript.
Unlike slicing, fancy indexing always copies the data into a new array.
One-dimensional array
A one-dimensional array has only one axisaxis = 0, so a one-dimensional array takes values onaxis = 0this axis:
Example
x = np.arange(9)
print(x)
# Read the element corresponding to the specified index from a one-dimensional array
print("-------Read the element corresponding to the index-------")
x2 = x[[0, 6]] # Using fancy indexing
print(x2)
print(x2[0])
print(x2[1])
The output result is:
[0 1 2 3 4 5 6 7 8] -------读取下标对应的元素------- [0 6] 0 6
Two-dimensional array
1. Pass in a sequential index array
Example
print (x[[4,2,1,7]])Output the rows corresponding to the indices4, 2, 1, 7and the output result is:
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15] [16 17 18 19] [20 21 22 23] [24 25 26 27] [28 29 30 31]] -------读取下标对应的行------- [[16 17 18 19] [ 8 9 10 11] [ 4 5 6 7] [28 29 30 31]]
2. Pass in a reverse-order index array
Example
The output result is:
[[16 17 18 19] [24 25 26 27] [28 29 30 31] [ 4 5 6 7]]
3. Pass in multiple index arrays (use np.ix_)
The np.ix_ function takes two arrays and produces a mapping relationship of their Cartesian product.
The Cartesian product in mathematics refers to the Cartesian product (Cartesian product) of two sets X and Y, also known as the direct product, denoted asX×Y, where the first object is a member of X and the second object is a member of Y, forming all possible ordered pairs.
For exampleA={a,b}, B={0,1,2}, then:
A×B={(a, 0), (a, 1), (a, 2), (b, 0), (b, 1), (b, 2)}
B×A={(0, a), (0, b), (1, a), (1, b), (2, a), (2, b)}
Example
The output result is:
[[ 4 7 5 6] [20 23 21 22] [28 31 29 30] [ 8 11 9 10]]Other extensions