NumPy Advanced Indexing
NumPy provides more indexing methods than general Python sequences.
In addition to the integer and slice indexing seen before, 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 elements of an array. Compared to 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 some dimension of the target array.
The following example gets the elements at (0,0),(1,1) and (2,0) positions in the array.
Example
import
numpy
as
np
x
=
np
.
array
(
[
[
1
,
2
]
,
[
3
,
4
]
,
[
5
,
6
]
]
)
y
=
x
[
[
0
,
1
,
2
]
,
[
0
,
1
,
0
]
]
(
y
)
The output is:
[1 4 5]
The following example gets the four corner elements of a 4X3 array. The row indices are [0,0] and [3,3], and the column indices are [0,2] and [0,2].
Example
import
numpy
as
np
x
=
np
.
array
(
[
[
0
,
1
,
2
]
,
[
3
,
4
,
5
]
,
[
6
,
7
,
8
]
,
[
9
,
10
,
11
]
]
)
(
'
Our array is:
'
)
(
x
)
(
'
\n
'
)
rows
=
np
.
array
(
[
[
0
,
0
]
,
[
3
,
3
]
]
)
cols
=
np
.
array
(
[
[
0
,
2
]
,
[
0
,
2
]
]
)
y
=
x
[
rows
,
cols
]
(
'
The four corner elements of this array are:
'
)
(
y
)
The output is:
我们的数组是:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
The four corner elements of this array are:
[[ 0 2]
[ 9 11]]
The returned result is an ndarray object containing each corner element.
You can combine slices : or … with index arrays, as in the following example:
Example
import
numpy
as
np
a
=
np
.
array
(
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
,
[
7
,
8
,
9
]
]
)
b
=
a
[
1
:
3
,
1
:
3
]
c
=
a
[
1
:
3
,
[
1
,
2
]
]
d
=
a
[
...,
1
:
]
(
b
)
(
c
)
(
d
)
The output is:
[[5 6]
[8 9]]
[[5 6]
[8 9]]
[[2 3]
[5 6]
[8 9]]
Boolean Indexing
We can index a target array with a boolean array.
Boolean indexing uses boolean operations (such as comparison operators) to obtain an array of elements that meet specified conditions.
The following example gets elements greater than 5:
Example
import
numpy
as
np
x
=
np
.
array
(
[
[
0
,
1
,
2
]
,
[
3
,
4
,
5
]
,
[
6
,
7
,
8
]
,
[
9
,
10
,
11
]
]
)
(
'
Our array is:
'
)
(
x
)
(
'
\n
'
)
Now we will print the elements greater than 5
(
'
The elements greater than 5 are:
'
)
(
x
[
x
5
]
)
The output is:
我们的数组是:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
Elements greater than 5 are:
[ 6 7 8 9 10 11]
The following example uses ~ (complement operator) to filter NaN.
Example
import
numpy
as
np
a
=
np
.
array
(
[
np
.
nan
,
1
,
2
,
np
.
nan
,
3
,
4
,
5
]
)
(
a
[
~
np
.
isnan
(
a
)
]
)
The output is:
[ 1. 2. 3. 4. 5.]
The following example demonstrates how to filter out non-complex elements from an array.
Example
import
numpy
as
np
a
=
np
.
array
(
[
1
,
2
6j
,
5
,
3.5
5j
]
)
(
a
[
np
.
iscomplex
(
a
)
]
)
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 using the values of the index array as subscripts along an 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 at the corresponding subscript.
Unlike slicing, fancy indexing always copies data into a new array.
One-dimensional array
A one-dimensional array has only one axis axis = 0, so a one-dimensional array takes values along axis = 0 this axis:
Example
import
numpy
as
np
x
=
np.
arange
(
9
)
(
x
)
A one-dimensional array reads the element at the specified subscript
(
"-------Read the element at the subscript-------"
)
x2
=
x
[
[
0
,
6
]
]
Using Fancy Indexing
(
x2
)
(
x2
[
0
]
)
(
x2
[
1
]
)
The output is:
[0 1 2 3 4 5 6 7 8]
-------Read the element corresponding to the index-------
[0 6]
0
6
Two-dimensional array
1. Pass in an ordered index array
Example
import
numpy
as
np
x
=
np
.
arange
(
32
)
.
reshape
(
(
8
,
4
)
)
(
x
)
A two-dimensional array reads the row at the specified subscript
(
"
-------Read the row at the subscript-------
"
)
(
x
[
[
4
,
2
,
1
,
7
]
]
)
print (x[[4,2,1,7]]) outputs the rows with the following subscripts 4, 2, 1, 7 corresponding to the subscripts, and 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]]
-------Read the row corresponding to the index-------
[[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
import
numpy
as
np
x
=
np
.
arange
(
32
)
.
reshape
(
(
8
,
4
)
)
(
x
[
[
4
,-
2
,-
1
,-
7
]
]
)
The output 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 as input and produces a mapping of the Cartesian product.
The Cartesian product refers to, in mathematics, the Cartesian product of two sets X and Y, also called the direct product, denoted as X×Y, where the first object is a member of X and the second object is one of the members of all possible ordered pairs of Y.
For example A={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
import
numpy
as
np
x
=
np
.
arange
(
32
)
.
reshape
(
(
8
,
4
)
)
(
x
[
np
.
ix_
(
[
1
,
5
,
7
,
2
]
,
[
0
,
3
,
1
,
2
]
)
]
)
The output is:
[[ 4 7 5 6]
[20 23 21 22]
[28 31 29 30]
[ 8 11 9 10]]