Numpy 简明教程
NumPy - Advanced Indexing
可以从 ndarray 中进行选择,ndarray 是非元组序列、整数或布尔数据类型的 ndarray 对象,或元组,其中至少有一项是序列对象。高级索引始终返回数据的副本。与此相反,切片只呈现视图。
It is possible to make a selection from ndarray that is a non-tuple sequence, ndarray object of integer or Boolean data type, or a tuple with at least one item being a sequence object. Advanced indexing always returns a copy of the data. As against this, the slicing only presents a view.
有两种高级索引类型: Integer 和 Boolean 。
There are two types of advanced indexing − Integer and Boolean.
Integer Indexing
此机制有助于根据其 N 维度索引选择数组中的任何任意项目。每个整数数组表示该维度中索引的数量。当索引包含的整数数组数与目标 ndarray 的维度一样多时,它就会变得简单。
This mechanism helps in selecting any arbitrary item in an array based on its Ndimensional index. Each integer array represents the number of indexes into that dimension. When the index consists of as many integer arrays as the dimensions of the target ndarray, it becomes straightforward.
在以下示例中,从 ndarray 对象的每行中选择指定列的一个元素。因此,行索引包含所有行号,并且列索引指定要选择的元素。
In the following example, one element of specified column from each row of ndarray object is selected. Hence, the row index contains all row numbers, and the column index specifies the element to be selected.
Example 1
import numpy as np
x = np.array([[1, 2], [3, 4], [5, 6]])
y = x[[0,1,2], [0,1,0]]
print y
其输出如下所示 −
Its output would be as follows −
[1 4 5]
从第一个数组中选择了 (0,0)、(1,1) 和 (2,0) 处的元素。
The selection includes elements at (0,0), (1,1) and (2,0) from the first array.
在以下示例中,选择了放置在 4X3 数组边角处的元素。选择的行索引是 [0, 0] 和 [3,3],而列索引是 [0,2] 和 [0,2]。
In the following example, elements placed at corners of a 4X3 array are selected. The row indices of selection are [0, 0] and [3,3] whereas the column indices are [0,2] and [0,2].
Example 2
import numpy as np
x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]])
print 'Our array is:'
print x
print '\n'
rows = np.array([[0,0],[3,3]])
cols = np.array([[0,2],[0,2]])
y = x[rows,cols]
print 'The corner elements of this array are:'
print y
此程序的输出如下 -
The output of this program is as follows −
Our array is:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
The corner elements of this array are:
[[ 0 2]
[ 9 11]]
结果选择是包含边角元素的 ndarray 对象。
The resultant selection is an ndarray object containing corner elements.
可以通过将一片 (:) 或省略号 (…) 与索引数组一起使用,将高级和基本索引结合起来。以下示例对行使用切片,对列使用高级索引。当对两者使用切片时,结果相同。但是,高级索引会导致副本,并且可能有不同的内存布局。
Advanced and basic indexing can be combined by using one slice (:) or ellipsis (…) with an index array. The following example uses slice for row and advanced index for column. The result is the same when slice is used for both. But advanced index results in copy and may have different memory layout.
Example 3
import numpy as np
x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]])
print 'Our array is:'
print x
print '\n'
# slicing
z = x[1:4,1:3]
print 'After slicing, our array becomes:'
print z
print '\n'
# using advanced index for column
y = x[1:4,[1,2]]
print 'Slicing using advanced index for column:'
print y
此程序的输出如下:
The output of this program would be as follows −
Our array is:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
After slicing, our array becomes:
[[ 4 5]
[ 7 8]
[10 11]]
Slicing using advanced index for column:
[[ 4 5]
[ 7 8]
[10 11]]
Boolean Array Indexing
此类高级索引在结果对象应该是布尔运算结果(例如比较运算符)时使用。
This type of advanced indexing is used when the resultant object is meant to be the result of Boolean operations, such as comparison operators.
Example 1
在此示例中,大于 5 的项目作为布尔索引的结果返回。
In this example, items greater than 5 are returned as a result of Boolean indexing.
import numpy as np
x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]])
print 'Our array is:'
print x
print '\n'
# Now we will print the items greater than 5
print 'The items greater than 5 are:'
print x[x > 5]
此程序的输出如下:
The output of this program would be −
Our array is:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
The items greater than 5 are:
[ 6 7 8 9 10 11]