我想用一些 sklearn 用Cython编写的模块(我不说)。在该模块中,定义了一种方法 cdef inline... ,而另一个 cdef 模块中的方法使用这些方法,但我无法访问它们。
sklearn
cdef inline...
cdef
Here 是定义类的地方,并且有一个方法 dist :
dist
cdef class EuclideanDistance(DistanceMetric): #STUFF cdef inline DTYPE_t dist(self, DTYPE_t* x1, DTYPE_t* x2, ITYPE_t size) nogil except -1: return euclidean_dist(x1, x2, size) #MORESTUFF
和 here 我们看到 距离
距离
cdef inline DTYPE_t dist(self, DTYPE_t* x1, DTYPE_t* x2, ITYPE_t size) nogil except -1: """Compute the distance between arrays x1 and x2""" self.n_calls += 1 if self.euclidean: return euclidean_dist(x1, x2, size) else: return self.dist_metric.dist(x1, x2, size) ####HERE!!!!
但当我尝试使用这种方法时,它根本不存在。。。。
import sklearn.neighbors help(sklearn.neighbors.dist_metrics.EuclideanDistance)
如何访问 距离 方法
调查 DistanceMetric class . 其用途如下:
from sklearn.neighbors import DistanceMetric dist = DistanceMetric.get_metric('euclidean') print(dist) # <sklearn.neighbors.dist_metrics.EuclideanDistance at 0x7fabe405e848>
然后要访问dist方法,请使用 pairwise() ,这将在内部调用 dist()
pairwise()
dist()
X = [[0, 1, 2], [3, 4, 5]] dist.pairwise(X) # array([[0. , 5.19615242], # [5.19615242, 0. ]])
您还可以使用两个数组X、Y和in 成对() 方法
成对()