花式索引
2025年5月11日
14:26
★Fancy indexing: https://jakevdp.github.io/PythonDataScienceHandbook/02.07-fancy-indexing.html
先以一维数组为例,
x = [51 92 14 71 60 20 82 86 74 74]
ind = [3, 7, 4]
print(x[ind])
array([71, 86, 60])
ind = np.array([[3, 7],
[4, 5]])
print(x[ind])
array([[71, 86],
[60, 20]])
可以看出,When using fancy indexing, the shape of the result reflects the shape of the index arrays rather than the shape of the array being indexed:
再以多维数组为例:
X = array(
[[ 0, 1, 2,
3],
[ 4,
5, 6, 7],
[ 8,
9, 10, 11]]
)
Like with standard indexing, the first index refers to the row, and the second to the column:
row = np.array([0, 1, 2])
col = np.array([2, 1, 3])
print(X[row, col])
array([ 2, 5, 11])
X有二维,dim=0和dim=1,row是对dim=0的索引,col是对dim=1的索引 ,row和col的组合就是对X的索引。
Notice that the first value in the result is X[0, 2], the second is X[1, 1], and the third is X[2, 3]. The pairing of indices in fancy indexing follows all the broadcasting rules that were mentioned in Computation on Arrays: Broadcasting. So, for example, if we combine a column vector and a row vector within the indices, we get a two-dimensional result:
print(X[row[:, np.newaxis], col])
array([[
2, 1,
3],
[ 6, 5,
7],
[10, 9, 11]])
总结:假设X是一个n维数组,即有n个dim,X的shape是(n_dim0, n_dim1,…,n_dimn),现在对X进行花式索引,X[index_dim0, index_dim1,…, index_dimn],其中 index_dim1到index_dimn可以省略任意个,但最多只能到index_dimn,如果有index_dim(n+1),就会报错。index_dim0到index_dimn的可以是任意维数的数组,且index_dim0到index_dimn的shape可以不一样(但是必须可以广播),现在假设index_dim0到index_dimn的shape都是(a,b,c),index_dim0中的每个元素表示对X第0维的索引,index_dimn中的每个元素表示对X第n维的索引,将index_dim0,…,Index_dimn的单个元素进行组合,就得到了一个对X中单个元素的索引,由于index_dim0到index_dimn的shape都是(a,b,c),因此一共得到了a*b*c个对X中单个元素的索引。
再总结:
X[index_dim0,index_dim1,index_dim2]的shape是(100,200,300,400),
X[index_dim0,index_dim1]的shape是(100,200,300,400,88),
X[index_dim0]的shape是(100,200,300,400,77,88),
已使用 OneNote 创建。