Python Numba和二维numpy数组列表

Python Numba和二维numpy数组列表,python,list,numpy,numba,Python,List,Numpy,Numba,我有一些课: 来自numba导入类、int32、float32、类型 从a.typed导入列表 _规格=[ ('Y_rf',types.List(float32[:,:]), ... ] @jitclass(_spec) 等级密度比估算: 定义初始(自我,西格玛): 自我西格玛=西格玛 self.Y_rf=[np.array([[0.]],dtype=float32] 但我不能让它工作。它总是因不同的错误而中断。目前的错误是: TypingError: Failed in nopython m

我有一些课:

来自numba导入类、int32、float32、类型
从a.typed导入列表
_规格=[
('Y_rf',types.List(float32[:,:]),
...
]
@jitclass(_spec)
等级密度比估算:
定义初始(自我,西格玛):
自我西格玛=西格玛
self.Y_rf=[np.array([[0.]],dtype=float32]
但我不能让它工作。它总是因不同的错误而中断。目前的错误是:

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Internal error at <numba.typeinfer.CallConstraint object at 0x00000277CBBBF550>.
Failed in nopython mode pipeline (step: nopython mode backend)


File "src\models\DDRE.py", line 26:
    def __init__(self, sigma):
        <source elided>
        self.sigma = sigma
        self.Y_rf = [np.array([[0.]], dtype=float32)]
        ^

[1] During: lowering "(self).Y_rf = $0.11" at D:\anomaly-detection\src\models\DDRE.py (26)
[2] During: resolving callee type: jitclass.DensityRatioEstimation#277c8a6cdd8<sigma:float32,Y_rf:list(array(float32, 2d, A)),Y_te:list(array(float32, 2d, A)),k:int32,alphas:array(float32, 1d, A),b:array(float32, 1d, A)>
[3] During: typing of call at <string> (3)

Enable logging at debug level for details.

File "<string>", line 3:
<source missing, REPL/exec in use?>
TypingError:在nopython模式管道中失败(步骤:nopython前端)
在的内部错误。
在nopython模式管道中失败(步骤:nopython模式后端)
文件“src\models\DDRE.py”,第26行:
定义初始(自我,西格玛):
自我西格玛=西格玛
self.Y_rf=[np.array([[0.]],dtype=float32]
^
[1] 期间:在D:\normal detection\src\models\DDRE.py(26)处降低“(self).Y_rf=$0.11”
[2] 期间:解析被调用方类型:jitclass.DensityRatioEstimation#277c8a6cdd8
[3] 期间:在(3)处键入呼叫
在调试级别启用日志记录以了解详细信息。
文件“”,第3行:

我还尝试使用
numba.types.List中的
List.empty_List(float32[:,:])
来代替
[np.array([[0.]],dtype=float32)]
。但它也不起作用。如何解决这个问题?

您的代码片段有一个问题,您正在尝试使用Numba数据类型创建Numpy数组

np.array([[1, 2], [3, 4]], dtype=np.float32) # OK
np.array([[1, 2], [3, 4]], dtype=nb.float32) # Not OK
但是,主要问题是需要使用
numba.types.npytypes.Array
指定列表的类型。这与使用
float32([:,:])
指定数组的函数签名不同

输出
import numba as nb
import numpy as np

_spec = [
    ('Y_rf', nb.types.List(nb.types.Array(nb.types.float32, 2, 'C'))),
    ('sigma', nb.types.int32)
]

@jitclass(_spec)
class DensityRatioEstimation:
    def __init__(self, sigma):
        self.sigma = sigma
        self.Y_rf = [np.array([[1, 2], [3, 4]], dtype=np.float32)]


dre = DensityRatioEstimation(1)
dre.Y_rf
[array([[1., 2.],
        [3., 4.]], dtype=float32)]