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.
How can I set it up so I can use all of the parameters from the original RandomForestClassifier along with adding this 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:
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)