numba:未找到用于签名的函数 Function(<built-in function getitem>) 的实现:

numba: No implementation of function Function(<built-in function getitem>) found for signature:

我很难在我的函数中实现 numba。

基本上,如果尚未添加新数据,我想连接到具有 22 列的数组。如果没有旧数据,新数据应该变成二维数组。

该函数在没有装饰器的情况下工作正常:

@jit(nopython=True)
def add(new,original=np.array([])):
  duplicate=True
  if original.size!=0:
    for raw in original:
      for ii in range(11,19):
        if raw[ii]!=new[ii]:
          duplicate=False
    if duplicate==False:
      res=np.zeros((original.shape[0]+1,22))
      res[:original.shape[0]]=original
      res[-1]=new
      return res
    else:
      return original
  else:
    res=np.zeros((1,22))
    res[0]=new
    return res

此外,如果我删除代码的最后一部分:

  else:
    res=np.zeros((1,22))
    res[0]=new
    return res

它将与 njit 一起工作

因此,如果我忽略还没有旧数据的情况,一切都会好起来的。

仅供参考:我传入的数据是混合浮点数和 np.nan。

有什么想法吗? 提前致谢!

这是我的错误日志:

---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
<ipython-input-255-d05a5f4ea944> in <module>()
     19     return res
     20 #add(a,np.array([b]))
---> 21 add(a)

2 frames
/usr/local/lib/python3.7/dist-packages/numba/core/dispatcher.py in _compile_for_args(self, *args, **kws)
    413                 e.patch_message(msg)
    414 
--> 415             error_rewrite(e, 'typing')
    416         except errors.UnsupportedError as e:
    417             # Something unsupported is present in the user code, add help info

/usr/local/lib/python3.7/dist-packages/numba/core/dispatcher.py in error_rewrite(e, issue_type)
    356                 raise e
    357             else:
--> 358                 reraise(type(e), e, None)
    359 
    360         argtypes = []

/usr/local/lib/python3.7/dist-packages/numba/core/utils.py in reraise(tp, value, tb)
     78         value = tp()
     79     if value.__traceback__ is not tb:
---> 80         raise value.with_traceback(tb)
     81     raise value
     82 

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function getitem>) found for signature:
 
 >>> getitem(float64, int64)
 
There are 22 candidate implementations:
      - Of which 22 did not match due to:
      Overload of function 'getitem': File: <numerous>: Line N/A.
        With argument(s): '(float64, int64)':
       No match.

During: typing of intrinsic-call at <ipython-input-255-d05a5f4ea944> (7)

File "<ipython-input-255-d05a5f4ea944>", line 7:
def add(new,original=np.array([])):
    <source elided>
      for ii in range(11,19):
        if raw[ii]!=new[ii]:
        ^

更新: 这是它应该如何工作。功能主要涵盖三种情况

新数据的示例输入(一维数组):

array([9.0000000e+00, 0.0000000e+00, 1.0000000e+00, 0.0000000e+00,
       0.0000000e+00,           nan, 5.7300000e-01, 9.2605450e-01,
       9.3171725e-01, 9.2039175e-01, 9.3450000e-01, 1.6491636e+09,
       1.6494228e+09, 1.6496928e+09, 1.6497504e+09, 9.2377000e-01,
       9.3738000e-01, 9.3038000e-01, 9.3450000e-01,           nan,
                 nan,           nan])

原始数据的样本输入(二维数组):

array([[4.00000000e+00, 0.00000000e+00, 1.00000000e+00, 0.00000000e+00,
        0.00000000e+00,            nan, 5.23000000e-01, 8.31589755e-01,
        8.34804877e-01, 8.28374632e-01, 8.36090000e-01, 1.64938320e+09,
        1.64966400e+09, 1.64968920e+09, 1.64975760e+09, 8.30750000e-01,
        8.38020000e-01, 8.34290000e-01, 8.36090000e-01,            nan,
                   nan,            nan]])
  1. 将添加新数据,没有原始数据
add(new)
Output:

array([[9.0000000e+00, 0.0000000e+00, 1.0000000e+00, 0.0000000e+00,
        0.0000000e+00,           nan, 5.7300000e-01, 9.2605450e-01,
        9.3171725e-01, 9.2039175e-01, 9.3450000e-01, 1.6491636e+09,
        1.6494228e+09, 1.6496928e+09, 1.6497504e+09, 9.2377000e-01,
        9.3738000e-01, 9.3038000e-01, 9.3450000e-01,           nan,
                  nan,           nan]])
  1. 将添加新数据,以前没有添加过且有原始数据
add(new,original)
Output:
array([[4.00000000e+00, 0.00000000e+00, 1.00000000e+00, 0.00000000e+00,
        0.00000000e+00,            nan, 5.23000000e-01, 8.31589755e-01,
        8.34804877e-01, 8.28374632e-01, 8.36090000e-01, 1.64938320e+09,
        1.64966400e+09, 1.64968920e+09, 1.64975760e+09, 8.30750000e-01,
        8.38020000e-01, 8.34290000e-01, 8.36090000e-01,            nan,
                   nan,            nan],
       [9.00000000e+00, 0.00000000e+00, 1.00000000e+00, 0.00000000e+00,
        0.00000000e+00,            nan, 5.73000000e-01, 9.26054500e-01,
        9.31717250e-01, 9.20391750e-01, 9.34500000e-01, 1.64916360e+09,
        1.64942280e+09, 1.64969280e+09, 1.64975040e+09, 9.23770000e-01,
        9.37380000e-01, 9.30380000e-01, 9.34500000e-01,            nan,
                   nan,            nan]])

  1. 将添加之前已经添加的新数据
add(new,original)
Output:

array([[9.0000000e+00, 0.0000000e+00, 1.0000000e+00, 0.0000000e+00,
        0.0000000e+00,           nan, 5.7300000e-01, 9.2605450e-01,
        9.3171725e-01, 9.2039175e-01, 9.3450000e-01, 1.6491636e+09,
        1.6494228e+09, 1.6496928e+09, 1.6497504e+09, 9.2377000e-01,
        9.3738000e-01, 9.3038000e-01, 9.3450000e-01,           nan,
                  nan,           nan]])

主要问题是 Numba 假定 original 是一个 1D 数组,但事实并非如此。 pure-Python 代码之所以有效,是因为 解释器 它从不执行循环体 for raw in original 但 Numba 需要在执行之前编译所有代码。您可以使用以下函数原型解决此问题:

def add(new,original=np.array([[]])):  # Note the `[[]]` instead of `[]`

由此,Numba 可以正确推断出 original 数组是二维数组。

请注意,指定 Numpy 数组和输入的维度和类型是避免此类错误和偷偷摸摸的错误(例如,由于 integer/float 截断)的好方法。