我正在尝试编写一个函数来初始化数组并在返回之前将其打乱。 将 numba 导入为 nb
@nb.jit(nopython=True, cache=True)
def test(x):
ind = np.array(range(len(x)))
np.random.shuffle(ind)
return ind
错误消息说我使用了不受支持的功能或数据类型:
NotImplementedError: range_state_int64 cannot be represented as a Numpy dtype
numba 是否支持 numpy.random.shuffle()?如何修改?谢谢!
请您参考如下方法:
这实际上与random.shuffle无关,因为numba supports the random module out of the box .
这里的问题是 numba cannot support the range object (因为它是一个 python 对象)当设置了 nopython 标志时。将 range 替换为 np.arange :
@nb.njit(cache=True) # same as @nb.jit(nopython=True, ...)
def test(x):
ind = np.arange(len(x))
np.random.shuffle(ind)
return ind
test([1, 2, 3])
# array([1, 0, 2])
