要获取等间距索引的列表,请使用
np.linspace:
idx = np.round(np.linspace(0, len(arr) - 1, numElems)).astype(int)
接下来,索引回
arr以获取相应的值:
arr[idx]
在转换为整数之前,请始终使用舍入。在内部,在提供dtype参数时
linspace调用
astype。因此,此方法 不 等效于:
# this simply truncates the non-integer partidx = np.linspace(0, len(array) - 1, numElems).astype(int)idx = np.linspace(0, len(arr) - 1, numElems, dtype='int')



