因为对于我的程序来说,快速索引Numpy数组是非常必要的,并且花哨的索引在考虑性能方面没有良好的声誉,我决定进行一些测试.特别是因为Numba发展很快,我尝试了哪种方法与numba配合得很好.
作为输入,我一直在使用以下数组进行小型数组测试:
import numpy as np
import numba as nb
x = np.arange(0, 100, dtype=np.float64) # array to be indexed
idx = np.array((0, 4, 55, -1), dtype=np.int32) # fancy indexing array
bool_mask = np.zeros(x.shape, dtype=np.bool) # boolean indexing mask
bool_mask[idx] = True # set same elements as in idx True
y = np.zeros(idx.shape, dtype=np.float64) # output array
y_bool = np.zeros(bool_mask[bool_mask == True].shape, dtype=np.float64) #bool output array (only for convenience)
Run Code Online (Sandbox Code Playgroud)
以下数组用于我的大型数组测试(y_bool此处需要处理重复数字randint):
x = …Run Code Online (Sandbox Code Playgroud)