我想扩展scikitlearn的ClassificationCriterion
类,它被定义为内部模块^{cimport
它们)。但是,当我试图扩展ClassificationCriterion
时,我得到了错误TypeError: __cinit__() takes exactly 2 positional arguments (0 given)
。下面的MWE再现了错误,并显示错误发生在__new__
之后,__init__
之前。在
有没有办法像这样扩展Cython类?在
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree._criterion import ClassificationCriterion
class MaxChildPrecision(ClassificationCriterion):
def __new__(self, *args, **kwargs):
print('new')
super().__new__(MaxChildPrecision, *args, **kwargs)
def __init__(self, *args, **kwargs):
print('init')
super(MaxChildPrecision).__init__(*args, **kwargs)
clf = DecisionTreeClassifier(criterion=MaxChildPrecision())
有两个问题。首先,^{} requires two specific arguments to its constructor that you aren't passing it 。您必须计算出这些参数代表什么并将它们传递给基类。在
其次,还有一个赛顿问题。如果我们看the description of how to use ^{} ,那么我们会看到:
不幸的是,sklearn的编写者没有提供}参数,因此必须重写{}。这样的方法应该有效:
*
和{我将必要的参数传递给}中处理其余参数。我不需要调用基类
__new__
中的ClassificationCriterion
,并在{__init__
(因为基类没有定义__init__
)。在相关问题 更多 >
编程相关推荐