其他分享
首页 > 其他分享> > numpy中的fancy indexing

numpy中的fancy indexing

作者:互联网

Boolean Array Indexing

a: ndarray
b: ndarray of bool
a.shape == b.shape
a[b]

a = np.arange(-2, 3)
print(a)
print(a[[True, False, True, False, False]])
print(a > 0)
print(a[a > 0])
[-2 -1  0  1  2]
[-2  0]
[False False False  True  True]
[1 2]

Integer Array Indexing

a: ndarray
l: tuple,可以去掉括号(语法糖)
a的维度 ≥ \ge ≥ l的“维度”
a[l]

1D数组

a = np.arange(5, 10)
print(a)
print(a[[0, 0, 2]])
[5 6 7 8 9]
[5 5 7]

nD数组

a = np.arange(16).reshape(4, 4)
print(a)
print(a[[0, 1]])
print(a[[0, 1], [2, 3]])
print(a[[[0,0],
         [1,1]], 
        [[2,2],
         [3,3]]])
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

[[0 1 2 3]
 [4 5 6 7]]
 
[2 7]

[[2 2]
 [7 7]]

标签:False,True,indexing,fancy,np,arange,print,numpy,ndarray
来源: https://blog.csdn.net/w112348/article/details/114196605