我有以下类的工作方式如下:
class DecisionTreeRegressor():
def __init__(self, min_leaf=5, impurity_threshold=1e-5,
root=None, leaf_value=None, impurity=None):
self.min_leaf = min_leaf
self.impurity_threshold = impurity_threshold
self.root = root
self._leaf_calculation = leaf_value
self._impurity_calculation = impurity
我想DecisionTreeRegressor
在另一个名为的类中启动该类,该类RandomForestRegressor
目前具有以下结构,用于自身及其父类RandomForest
:
class RandomForest():
def __init__(self, n_estimators=10, min_leaf=5,
sample_size = 2/3, min_impurity=1e-5):
self.n_estimators = n_estimators
self.min_leaf = min_leaf
self.sample_size = sample_size
self.min_impurity = min_impurity
class RandomForestRegressor(RandomForest):
def __init__(self):
super().__init__()
self.tree = DecisionTreeRegressor
self.trees = [self.tree(min_leaf=self.min_leaf)
for i in range(self.n_estimators)]
这将返回以下错误消息:
TypeError: __init__() got an unexpected keyword argument 'min_leaf'
我觉得这很令人费解,原因如下:
DecisionTreeRegressor(min_leaf=5)
工作得很好。
另外,如果我RandomForestRegressor
改为:
class RandomForestRegressor(RandomForest):
def __init__(self):
super().__init__()
self.tree = DecisionTreeRegressor
self.trees = [self.tree() for i in range(self.n_estimators)]
这也可以正常工作,因为self.trees
它是一个填充了DecisionTreeRegressor
类的n 个不同实例的列表。
为什么传入min_leaf
参数会像这样调用错误消息?
我假设你是这样称呼的:
RandomForestRegressor(min_leaf=5)
RandomForestRegressor
类的初始值设定项没有声明的关键字参数,它根本不需要。
您需要明确地将它们添加到签名中或将 kwargs 添加到签名中。
class RandomForestRegressor(RandomForest):
def __init__(self, **kwargs):
super().__init__(**kwargs)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句