Python Numba-使用可变索引访问Numpy数组
我正在研究使用numba来加速迭代计算,特别是在计算有时依赖于以前计算的结果,因此矢量化并不总是适用的情况下。我发现其中一个不足之处是它似乎不允许数据帧。虽然我认为没有问题,但是您可以传递一个2D numpy数组和一个列名称的numpy数组,并且我尝试实现一个函数,通过它们的列名而不是索引来引用值。这是我到目前为止的代码Python Numba-使用可变索引访问Numpy数组,python,numpy,numba,Python,Numpy,Numba,我正在研究使用numba来加速迭代计算,特别是在计算有时依赖于以前计算的结果,因此矢量化并不总是适用的情况下。我发现其中一个不足之处是它似乎不允许数据帧。虽然我认为没有问题,但是您可以传递一个2D numpy数组和一个列名称的numpy数组,并且我尝试实现一个函数,通过它们的列名而不是索引来引用值。这是我到目前为止的代码 从numba导入jit 将numpy作为np导入 @jit(nopython=True) def get_索引(列、列): 对于范围内的i(len(cols)): 如果cols
从numba导入jit
将numpy作为np导入
@jit(nopython=True)
def get_索引(列、列):
对于范围内的i(len(cols)):
如果cols[i]==col:
返回i
@jit(nopython=True)
def get_元素(ndarr:np.ndarray,cols:np.ndarray,row:np.int8,name:str):
ind=获取索引(cols,name)
打印(行)
打印(ind)
打印(ndarr[0][0])
#打印(ndarr[行][ind])
get_元素(np.array([['HI'],['BYE'],['HISAHASDG']]),np.array(['COLUMN_1']),0,“COLUMN_1”)
我已经得到了_索引,我已经独立测试了它,它是有效的。这基本上是np.where的一个实现,我想知道这是否会导致我的错误。因此,在注释掉该打印之后,该代码现在可以运行了。它按预期打印出0、0,然后是“HI”。所以理论上,所有注释掉的行都应该打印“HI”,就像前一行的打印一样,因为行和ind都是0。但当我取消注释它时,我得到以下信息:
---------------------------------------------------------------------------
TypingError Traceback (most recent call last)
<timed exec> in <module>
/sas/python/app/miniconda3/envs/py3lu/lib/python3.6/site-packages/numba/core/dispatcher.py in _compile_for_args(self, *args, **kws)
399 e.patch_message(msg)
400
--> 401 error_rewrite(e, 'typing')
402 except errors.UnsupportedError as e:
403 # Something unsupported is present in the user code, add help info
/sas/python/app/miniconda3/envs/py3lu/lib/python3.6/site-packages/numba/core/dispatcher.py in error_rewrite(e, issue_type)
342 raise e
343 else:
--> 344 reraise(type(e), e, None)
345
346 argtypes = []
/sas/python/app/miniconda3/envs/py3lu/lib/python3.6/site-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)
Invalid use of Function(<built-in function getitem>) with argument(s) of type(s): (array([unichr x 50], 1d, C), OptionalType(int64) i.e. the type 'int64 or None')
* parameterized
In definition 0:
All templates rejected with literals.
In definition 1:
All templates rejected without literals.
In definition 2:
All templates rejected with literals.
In definition 3:
All templates rejected without literals.
In definition 4:
All templates rejected with literals.
In definition 5:
All templates rejected without literals.
In definition 6:
All templates rejected with literals.
In definition 7:
All templates rejected without literals.
In definition 8:
All templates rejected with literals.
In definition 9:
All templates rejected without literals.
In definition 10:
All templates rejected with literals.
In definition 11:
All templates rejected without literals.
In definition 12:
TypeError: unsupported array index type OptionalType(int64) i.e. the type 'int64 or None' in [OptionalType(int64)]
raised from /sas/python/app/miniconda3/envs/py3lu/lib/python3.6/site-packages/numba/core/typing/arraydecl.py:69
In definition 13:
TypeError: unsupported array index type OptionalType(int64) i.e. the type 'int64 or None' in [OptionalType(int64)]
raised from /sas/python/app/miniconda3/envs/py3lu/lib/python3.6/site-packages/numba/core/typing/arraydecl.py:69
In definition 14:
All templates rejected with literals.
In definition 15:
All templates rejected without literals.
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: typing of intrinsic-call at <timed exec> (15)
File "<timed exec>", line 15:
<source missing, REPL/exec in use?>
---------------------------------------------------------------------------
打字机错误回溯(最近一次呼叫最后一次)
在里面
/sas/python/app/miniconda3/envs/py3lu/lib/python3.6/site-packages/numba/core/dispatcher.py in_compile_for_args(self,*args,**kws)
399 e.patch_信息(msg)
400
-->401错误\u重写(例如,“键入”)
402错误除外。不支持错误为e:
403#用户代码中存在不支持的内容,请添加帮助信息
/sas/python/app/miniconda3/envs/py3lu/lib/python3.6/site-packages/numba/core/dispatcher.py出错(e,问题类型)
342上升e
343其他:
-->344重新拍卖(e类、e类、无)
345
346 argtypes=[]
/sas/python/app/miniconda3/envs/py3lu/lib/python3.6/site-packages/numba/core/utils.py(tp、value、tb)
78值=tp()
79如果值.\uuuu回溯\uuuuu不是tb:
--->80提升值。带回溯(tb)
81提高价值
82
TypingError:在nopython模式管道中失败(步骤:nopython前端)
函数()与类型为(数组([unichr x 50],1d,C),可选类型(int64)的参数一起使用无效,即类型为“int64或None”)
*参数化
在定义0中:
所有模板均拒绝使用文字。
在定义1中:
拒绝所有没有文字的模板。
在定义2中:
所有模板均拒绝使用文字。
在定义3中:
拒绝所有没有文字的模板。
在定义4中:
所有模板均拒绝使用文字。
在定义5中:
拒绝所有没有文字的模板。
在定义6中:
所有模板均拒绝使用文字。
在定义7中:
拒绝所有没有文字的模板。
在定义8中:
所有模板均拒绝使用文字。
在定义9中:
拒绝所有没有文字的模板。
在定义10中:
所有模板均拒绝使用文字。
在定义11中:
拒绝所有没有文字的模板。
在定义12中:
TypeError:不支持的数组索引类型OptionalType(int64),即[OptionalType(int64)]
由/sas/python/app/miniconda3/envs/py3lu/lib/python3.6/site-packages/numba/core/typing/arraydecl.py:69提出
在定义13中:
TypeError:不支持的数组索引类型OptionalType(int64),即[OptionalType(int64)]
由/sas/python/app/miniconda3/envs/py3lu/lib/python3.6/site-packages/numba/core/typing/arraydecl.py:69提出
在定义14中:
所有模板均拒绝使用文字。
在定义15中:
拒绝所有没有文字的模板。
此错误通常由传递指定函数不支持的类型的参数引起。
[1] 期间:在(15)处键入内部调用
文件“”,第15行:
有什么我遗漏的吗?我检查了row和ind的类型,它们确实是int类型。为什么numba不让我使用int变量子集?谢谢。
numba
在这里真的很聪明!考虑当您通过<代码> COL>代码> <代码> > GETX索引>代码>时,它不在<代码> COLS中。code>cols[i]==col永远不会为真,循环将退出,并且由于函数末尾没有catchallreturn
,因此返回值将为None
numba
因此正确推断get\u index
的返回类型为OptionalType(int64)
,即一个值可以是int64或None
。但是None
不是索引的有效类型,因此不能使用None
值对数组进行索引
您可以通过在末尾添加catchallreturn
来解决此问题
@jit(nopython=True)
def get_index(cols, col):
for i in range(len(cols)):
if cols[i] == col:
return i
return -1
当然,在这种情况下,这可能不是您想要的行为;可能最好引发一个异常,numba
也能正确处理该异常
@jit(nopython=True)
def get_index(cols, col):
for i in range(len(cols)):
if cols[i] == col:
return i
raise IndexError('list index out of range')
哇,这太疯狂了,我确实看到了可选部分,但我想这可能只是用于所有int返回值。非常感谢您,添加的回报确实起到了作用:)