# NumPy切片和索引

# 数组切割  slice
av=np.arange(20);
aq=slice(5,16,1)
print(av[aq])

[ 5  6  7  8  9 10 11 12 13 14 15]

Process finished with exit code 0

aq=av[5:16:1]

a = np.arange(10)
print(a[2:])

[2 3 4 5 6 7 8 9]

Process finished with exit code 0

a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print (a[...,1])   # 第2列元素
print (a[1,...])   # 第2行元素
print (a[...,1:])  # 第2列及剩下的所有元素

[2 4 5]
[3 4 5]
[[2 3]
[4 5]
[5 6]]

Process finished with exit code 0

# 高级索引
xg=np.array([[1,2],[3,4],[5,6]])
xy=xg[[0,1,0],[0,1,1]]
print(xy)

[1 4 2]

Process finished with exit code 0

# 综合运用： ...
az=np.array([[1,2,3],[4,5,6],[7,8,9]])
ab=az[1:3,1:3]
ac=az[1:3,[1,0]]
print('ab:  ')
print(ab)
print('ac:  ')
print(ac)
print(ad)

ab:
[[5 6]
[8 9]]
ac:
[[5 4]
[8 7]]
[[2 3]
[5 6]
[8 9]]

Process finished with exit code 0

# 过滤· ~
ap=np.array([np.nan,3,12,np.nan])
print(ap[~np.isnan(ap)])

[ 3. 12.]

Process finished with exit code 0

x=np.arange(32).reshape((8,4))
print (x[[0，3，5],...])

[[ 0 1 2 3]
[12 13 14 15]
[20 21 22 23]]

x=np.arange(32).reshape((8,4))
print (x[[-0，-3，-5],...])

x=np.arange(32).reshape((8,4))
print (x[np.ix_([1,5,7,2],[0,3,1,2])])

[[ 4  7  5  6]
[20 23 21 22]
[28 31 29 30]
[ 8 11  9 10]]

Process finished with exit code 0
posted @ 2020-01-20 12:47  Super~me  阅读(...)  评论(...编辑  收藏