I am currently working on random forests under scikit-learn.
Is there a possible way to change the weights of each estimator used in the random forest generated?
I am currently working on random forests under scikit-learn.
Is there a possible way to change the weights of each estimator used in the random forest generated?
Are you asking how to change the weights of each estimator individually or how to change the answer weight of each tree in the voting system when doing
predict()
?
When you have a fitted random forest, the parameter estimators_ returns an array of decision trees, and all of them can be edited individually, f.e:
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=2)
df = pd.DataFrame([[1, True], [2, False]])
model.fit(df[0].to_numpy().reshape(-1,1), df[1])
print(model.estimators_)
Outputs:
[DecisionTreeClassifier(ccp_alpha=0.0, class_weight=None, criterion='gini',
max_depth=None, max_features='auto', max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, presort='deprecated',
random_state=1942352063, splitter='best'),
DecisionTreeClassifier(ccp_alpha=0.0, class_weight=None, criterion='gini',
max_depth=None, max_features='auto', max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, presort='deprecated',
random_state=1414900336, splitter='best')]
So you can select the first one just using model.estimators_[0]
.
Then, if you read the Decision tree docu, you can change the feature_importances_.
If your question is how to change the random forest voting system, then I recommend you take a look at the code, but let me tell you that it's not a good decision to modify this feature.
As you can see here, random forest takes for each output the biggest probability (of all trees), so you could work with the prediction probability of each decision tree individually.