【发布时间】:2017-11-18 18:47:32
【问题描述】:
我正在尝试编写 sklearn 的类 sklearn.tree._splitter.Splitter 的子类。 我在 cython 中编写了以下子类:
from libc.string cimport memcpy
import numpy as np
cimport numpy as np
np.import_array()
from sklearn.tree._splitter import Splitter, SplitRecord
from ._utils import rand_int
cdef double INFINITY = np.inf
cdef class StochasticSplitter(Splitter):
"""Splitter for finding a split stochastically."""
def __reduce__(self):
return (StochasticSplitter, (self.criterion,
self.max_features,
self.min_samples_leaf,
self.min_weight_leaf,
self.random_state,
self.presort), self.__getstate__())
cdef int node_split(self, double impurity, SplitRecord* split,
SIZE_t* n_constant_features) nogil except -1:
""" Find the best split on node samples[start:end]
Returns -1 in case of failure to allocate memory (and raise MemoryError)
or 0 otherwise.
"""
" My Logic...."
# Return values
split[0] = chosen_split
n_constant_features[0] = n_total_constants
return 0
但是当我尝试编译该文件时,出现以下错误:
'Splitter' 不是类型名称
和
'SplitRecord' 不是类型标识符
为什么会这样? 以及如何导入 Splitter 和 SplitRecord 以便在 cdef 类中使用它们?
注意事项: - 我没有 _splitter 模块的 .pyx 和 .pxd,所以我不能导入这些类。我只有 .pyd 文件。 - 我想将这个类定义为 cdef 类(而不是常规的 pythonic 类),因为函数 node_split 使用指针(而且我不知道如何在 python 中使用指针)
我在互联网和这里搜索,发现了类似的问题,但其中的答案都没有帮助我...... 谁能帮帮我?
【问题讨论】:
标签: scikit-learn cython