I'm trying to figure out how to inherit all of the attributes/methods from one class into another class. I'm basing it off of How to inherit a python base class? but I can't figure out how to get it to work for my simple example. In this example, I just want to make a new class that has all the functionality of RandomForestClassifier
but with a new attribute (called new_attribute
). In this method, I can't use the arguments of the original RandomForestClassifier
but I can add my new attribute.
我试图弄清楚如何从一个类继承所有属性/方法到另一个类。我基于如何继承python基类?但我无法弄清楚如何让它适用于我的简单例子。在这个例子中,我只想创建一个具有RandomForestClassifier的所有功能但具有新属性(称为new_attribute)的新类。在这个方法中,我不能使用原始RandomForestClassifier的参数,但我可以添加我的新属性。
How can I set it up so I can use all of the parameters from the original RandomForestClassifier along with adding this new_attribute
?
如何设置它以便我可以使用原始RandomForestClassifier中的所有参数并添加此new_attribute?
from sklearn.ensemble import RandomForestClassifier
class NewClassifier(RandomForestClassifier):
def __init__(self, new_attribute):
Super(RandomForestClassifier, self).__init__()
self.new_attribute = new_attribute
A = NewClassifier(n_estimators=1, new_attribute=0)
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-221-686839873f88> in <module>()
5 Super(RandomForestClassifier, self).__init__()
6 self.new_attribute = new_attribute
----> 7 A = NewClassifier(n_estimators=1, new_attribute=0)
TypeError: __init__() got an unexpected keyword argument 'n_estimators'
Hindsight: This was a poorly constructed question. I got the above to work with the code below. However, @Mseifert has a better representation in the answers:
后见之明:这是一个构造不良的问题。我得到了上面的代码来处理下面的代码。但是,@ Mysfrt在答案中的表现更好:
class NewClassifier(RandomForestClassifier):
def __init__(self, new_attribute, n_estimators=10, criterion='gini', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', max_leaf_nodes=None, min_impurity_split=1e-07, bootstrap=True, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False, class_weight=None):
RandomForestClassifier.__init__(self, n_estimators, criterion, max_depth, min_samples_split, min_samples_leaf, min_weight_fraction_leaf, max_features, max_leaf_nodes, min_impurity_split, bootstrap, oob_score, n_jobs, random_state, verbose, warm_start, class_weight)
self.new_attribute = new_attribute
A = NewClassifier(n_estimators=1, new_attribute=0)
1 个解决方案
#1
1
The easiest way would be to accept new_attribute
as first and must-have argument:
最简单的方法是接受new_attribute作为first和must-have参数:
from sklearn.ensemble import RandomForestClassifier
class NewClassifier(RandomForestClassifier):
def __init__(self, *args, **kwargs): # just accept any argument
# Find out what the value of "new_argument is" and remove it either from
# the positional arguments (args) or keyword arguments (kwargs) so that
# the remaining arguments can simply be passed to the super-class.
if args: # if there are positional arguments
new_attribute = args[0]
args = args[1:]
else: # no positional arguments
new_attribute = kwargs.pop('new_attribute')
super().__init__(*args, **kwargs)
self.new_attribute = new_attribute
Note that it's super
not Super
and you don't need the arguments in python-3.x
请注意,它不是Super而且你不需要python-3.x中的参数
#1
1
The easiest way would be to accept new_attribute
as first and must-have argument:
最简单的方法是接受new_attribute作为first和must-have参数:
from sklearn.ensemble import RandomForestClassifier
class NewClassifier(RandomForestClassifier):
def __init__(self, *args, **kwargs): # just accept any argument
# Find out what the value of "new_argument is" and remove it either from
# the positional arguments (args) or keyword arguments (kwargs) so that
# the remaining arguments can simply be passed to the super-class.
if args: # if there are positional arguments
new_attribute = args[0]
args = args[1:]
else: # no positional arguments
new_attribute = kwargs.pop('new_attribute')
super().__init__(*args, **kwargs)
self.new_attribute = new_attribute
Note that it's super
not Super
and you don't need the arguments in python-3.x
请注意,它不是Super而且你不需要python-3.x中的参数