花式索引

2025511

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=0dim=1row是对dim=0的索引,col是对dim=1的索引 rowcol的组合就是对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维数组,即有ndimXshape(n_dim0, n_dim1,…,n_dimn),现在对X进行花式索引,X[index_dim0, index_dim1,…, index_dimn],其中 index_dim1index_dimn可以省略任意个,但最多只能到index_dimn,如果有index_dim(n+1),就会报错。index_dim0index_dimn的可以是任意维数的数组,且index_dim0index_dimnshape可以不一样(但是必须可以广播),现在假设index_dim0index_dimnshape都是(a,b,c)index_dim0中的每个元素表示对X0维的索引,index_dimn中的每个元素表示对Xn维的索引,将index_dim0,…,Index_dimn的单个元素进行组合,就得到了一个对X中单个元素的索引,由于index_dim0index_dimnshape都是(a,b,c),因此一共得到了a*b*c个对X中单个元素的索引。

 

再总结:

  • X是一个n维数组,对X进行花式索引,X[index_dim0, index_dim1,…, index_dimn],得到的结果的shapeXshape无关,与index_dim0,…,index_dimnshape有关,还与花式索引中有几个index_dim有关。
  • 假设X3维数组,shape(66,77,88)index_dim0index_dim1index_dim2shape都是(100,200,300,400,那么

X[index_dim0,index_dim1,index_dim2]shape是(100,200,300,400),

X[index_dim0,index_dim1]shape是(100,200,300,40088),

X[index_dim0]shape是(100,200,300,4007788),

 

已使用 OneNote 创建。