【问题标题】:NotImplementedError: range_state_int64 cannot be represented as a Numpy dtypeNotImplementedError:range_state_int64 不能表示为 Numpy dtype
【发布时间】:2019-10-23 04:34:39
【问题描述】:

我正在尝试编写一个函数来初始化一个数组并在返回之前对其进行洗牌。 将 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()?如何修改它?谢谢!

【问题讨论】:

    标签: python python-2.7 numba


    【解决方案1】:

    这其实和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])
    

    【讨论】:

    猜你喜欢
    • 2019-06-27
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    相关资源
    最近更新 更多