python - 在 numba 函数中创建一个新的列表/数组

标签 python numba

“test_numba”下面的函数给出错误:“无法键入空列表”,但它在没有 numba 编译的情况下工作。

def test(list_test,count):

    test_list =[]
    for all in list_test:
        test_list.append(all)

    if count ==1:

        return np.asarray(test_list, dtype=int)
    else:
        return np.asarray([1,2,3,4],dtype=int)

    test_numba = numba.jit("int64[:](int32[:],int32)")(test)
    # calling numba function
    test_numba(np.asarray([1,2,3,4],dtype=int),1)
    # calling python function
    test(np.asarray([1,2,3,4],dtype=int),1)

最佳答案

Numba 签名需要 Numpy-Arrays。如果你只是省略签名,你可以让它工作:

test_numba = numba.jit()(test)

但由于这会回退到 Python 列表,因此不会提供任何加速。

使用您的签名,您强制 numba 假定 int32 1D numpy.array 作为第一个参数传递,标量 uint 作为第二个参数传递.该函数应返回一个 int64 1D numpy.array。但是你实际上返回了一个列表,所以 numba 无法编译该函数。

另见 numba signatures and eager compilation

关于python - 在 numba 函数中创建一个新的列表/数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35318186/

相关文章:

python - 具有多色 y 轴和相应颜色条的热图

python - 我的 socket 和 pickle 有问题。值​不保存到 txt 中

python - python numba中非常大的整数

anaconda - 如何在 numba 中清除缓存(或强制重新编译)

python - 计算大量 3x3 点积的最快方法

python - 错误: number of arguments and self

python - tensorflow 属性错误: 'module' object has no attribute 'DNNClassifier'

python - 神经网络的 Keras 模型 load_weights

python - 我想根据外键关系迭代项目并返回它,但只有一个项目返回

python - 如何知道安装了 numba 或 tensorflow 的 python 代码中每个 block 的最大线程数?