python - 为什么 numba 在 numpy linspace 中引发类型错误

标签 python numpy numba

我正在使用 numba 0.34.0 和 numpy 1.13.1。一个小例子如下所示:

import numpy as np    
from numba import jit
@jit(nopython=True)
def initial(veh_size):
    t = np.linspace(0, (100 - 1) * 30, 100, dtype=np.int32)
    t0 = np.linspace(0, (veh_size - 1) * 30, veh_size, dtype=np.int32)
    return t0

initial(100)

包含tt0 的行都有相同的错误消息。

错误信息:

numba.errors.InternalError: 
[1] During: resolving callee type: Function(<function linspace at 0x000001F977678C80>)
[2] During: typing of call at ***/test1.py (6)

最佳答案

因为 np.linspace 的 numba 版本不接受任何 dtype 参数(source: numba 0.34 documentation):

2.7.3.3. Other functions

The following top-level functions are supported:

  • [...]

  • numpy.linspace() (only the 3-argument form)

  • [...]

你需要使用 astype 在 nopython-numba 函数中转换它:

import numpy as np    
from numba import jit
@jit(nopython=True)
def initial(veh_size):
    t = np.linspace(0, (100 - 1) * 30, 100).astype(np.int32)
    t0 = np.linspace(0, (veh_size - 1) * 30, veh_size).astype(np.int32)
    return t0

initial(100)

或者只是不要在 nopython-numba 函数中使用 np.linspace 并将其作为参数传递。这避免了临时数组,我怀疑 numbas np.linspace 是否比 NumPys 快。

关于python - 为什么 numba 在 numpy linspace 中引发类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46024853/

相关文章:

python - PyScripter - 无法使用 KeyboardInterrupt 终止运行

python - 使用 numpy 数组与 DataFrame 屏蔽 pandas DataFrame

python - xarray - 从另一个 DataArray 的时间标签中选择/索引 DataArray

python - Python 中的连续数组警告 (numba)

python - 如何在 Python 中查看和打印 PDF?

python openweather 包不起作用

numpy - 在另一个二维数组中查找 numpy 二维数组的行索引

python - 从要绘制的创建函数生成随机值数组

python - Numba:回退到对象模式时抑制错误

python - 如何使 numba @jit 使用所有 cpu 内核(并行化 numba @jit)