我正在尝试扩展一些
sklearn
中的类
sklearn.neighbors.dist_metrics
单元然而,整个模块都是用Cython编写的,我想用自定义类执行的操作必须实现
cdef dist
(与
def dist
),显然。
因此,在我自己的模块中(我使用的是jupyter
%%cython
现在,实际上)我想在Cython中实现我的自定义类,实现
cdef分布
方法。然而,还有许多其他
cdef
同一模块中的内容
sklearn.neighbors.typedefs
模块,以及可能需要导入的其他模块。当我尝试导入这些东西时,会出现各种各样的错误。
当我尝试天真
import
:
%%cython
import numpy as np
from sklearn.neighbors.dist_metrics import DistanceMetric
from sklearn.neighbors.typedefs import DTYPE_t, ITYPE_t
cdef class NewDistance(sklearn.neighbors.DistanceMetric):
cdef inline DTYPE_t dist(self, DTYPE_t* x1, DTYPE_t* x2,
ITYPE_t size) nogil except -1:
return 5
我得到的错误表明,一旦这些东西被“pythonized”,我就不能用它们来定义“cython”:
Error compiling Cython file:
...
First base of 'KernelDistance' is not an extension type
...
'DTYPE_t' is not a type identifier
有一个
cimport
关键字,so:
%%cython
import numpy as np
from sklearn.neighbors.dist_metrics cimport DistanceMetric
from sklearn.neighbors.typedefs cimport DTYPE_t, ITYPE_t
cdef class NewDistance(sklearn.neighbors.DistanceMetric):
cdef inline DTYPE_t dist(self, DTYPE_t* x1, DTYPE_t* x2,
ITYPE_t size) nogil except -1:
return 5
annnnddd。。。
Error compiling Cython file:
....
'sklearn/neighbors/dist_metrics.pxd' not found
....
'sklearn/neighbors/typedefs.pxd' not found
如何从其他库导入cython文件?