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]]
上一篇:修改jupyter打开的默认浏览器


下一篇:完美解决idea2020一直indexing,无法操作的问题