我正在将函数转换为numba(nopython模式)。在此函数中,我需要访问2D数组的某些元素,其行索引包含在列表中,并且列是固定的。 在通常的python中,我这样做:
array[list,column_index]
但是在numba中编译时,出现以下错误:
TypingError: Invalid use of Function(<built-in function getitem>) with argument(s) of type(s): (array(float64, 2d, C), (reflected list(int64), int64))
* 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:
TypeError: unsupported array index type reflected list(int64) in (reflected list(int64), int64)
raised from C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\typing\arraydecl.py:71
In definition 7:
TypeError: unsupported array index type reflected list(int64) in (reflected list(int64), int64)
raised from C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\typing\arraydecl.py:71
This error is usually caused by passing an argument of a type that is unsupported by the named function.
我已经尝试过将列表转换为数组,因为从https://numba.pydata.org/numba-doc/dev/reference/numpysupported.html看来是正确的方法
“还支持高级索引的一个子集:只允许一个高级索引,并且它必须是一维数组(它也可以与任意数量的基本索引结合使用”)
但编码:
array[np.array(list),column_index]
产生以下错误:
Traceback (most recent call last):
File "<ipython-input-123-c5ca441bb6b7>", line 1, in <module>
tmp = L.numba_montecarlo_barab(SEED,w,R,T,mpx,0.5)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\dispatcher.py", line 367, in _compile_for_args
raise e
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\dispatcher.py", line 324, in _compile_for_args
return self.compile(tuple(argtypes))
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\dispatcher.py", line 655, in compile
cres = self._compiler.compile(args, return_type)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\dispatcher.py", line 82, in compile
pipeline_class=self.pipeline_class)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 926, in compile_extra
return pipeline.compile_extra(func)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 374, in compile_extra
return self._compile_bytecode()
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 857, in _compile_bytecode
return self._compile_core()
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 844, in _compile_core
res = pm.run(self.status)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 255, in run
raise patched_exception
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 246, in run
stage()
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 717, in stage_nopython_backend
self._backend(lowerfn, objectmode=False)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 666, in _backend
lowered = lowerfn()
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 653, in backend_nopython_mode
self.metadata)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 1050, in native_lowering_stage
lower.create_cpython_wrapper(flags.release_gil)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\lowering.py", line 272, in create_cpython_wrapper
release_gil=release_gil)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\targets\cpu.py", line 149, in create_cpython_wrapper
builder.build()
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\callwrapper.py", line 122, in build
self.build_wrapper(api, builder, closure, args, kws)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\callwrapper.py", line 155, in build_wrapper
val = cleanup_manager.add_arg(builder.load(obj), ty)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\callwrapper.py", line 58, in add_arg
cleanup_arg()
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\callwrapper.py", line 41, in cleanup_arg
self.api.reflect_native_value(ty, native.value, self.env_manager)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\pythonapi.py", line 1417, in reflect_native_value
impl(typ, val, c)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\targets\boxing.py", line 740, in reflect_list
raise TypeError(msg)
TypeError: Failed in nopython mode pipeline (step: nopython mode backend)
cannot reflect element of reflected container: reflected list(reflected list(reflected list(int64)))
也不能转换为元组,但是我知道不支持元组。
在numba nopython模式下使用高级索引的正确方法是什么?