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

时间:2017-09-03 15:14:57

标签: 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)

1 个答案:

答案 0 :(得分:4)

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

  

2.7.3.3。其他功能

     

支持以下顶级功能:

     
      
  • [...]

  •   
  • numpy.linspace()(仅限3个参数形式)

  •   
  • [...]

  •   

您需要使用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更快。