numpy索引和切片 np.newaxis np.arange
ndarray对象的内容可以通过索引或切片来访问和修改,与 Python 中 list 的切片操作一样。
ndarray 数组可以基于 0 - n 的下标进行索引,切片对象可以通过内置的 slice 函数,并设置 start, stop 及 step 参数进行,从原数组中切割出一个新数组。
import numpy as np a = np.arange(10) s = slice(2,7,2) # 从索引 2 开始到索引 7 停止,间隔为2 print (a[s])
layer_idx = np.arange(18)[np.newaxis, :, np.newaxis] print(layer_idx.shape)
(1,18,1)
np.newaxis的作用是在此位置增加一个新的轴