Reputation: 11
Is it possible to know which number of trees get to me the predicted value? I made an object detection project using the random forests algorithm
from sklearn.ensemble import RandomForestClassifier
RF_model = RandomForestClassifier(n_estimators = 50, random_state = 42)
# Train the model on training data
RF_model.fit(X_for_RF, y_train)
Here I have 50 trees, and I need to know which number of trees get to me this predicted value.
assume random forest extract to me this is apple then i need to get the number of trees it dcided it apple like "there are 30 trees tell me this is apple and the other 20 decide it banana".
Upvotes: 0
Views: 1028
Reputation: 2696
Yes this is possible using the estimators_
attribute:
RF_model = RandomForestClassifier(n_estimators = 50, random_state = 42)
RF_model.fit(X_for_RF, y_train)
trees = RF_model.estimators_
# Get all 50 tree predictions for the first instance
preds_for_0 = [tree.predict(X_for_RF[0].reshape(1, -1))[0] for tree in trees]
Here trees
is a list of n_estimators
(here 50) DecisionTreeClassifier()
objects from sklearn.tree
. To get the class labels from each tree, you can simply use the predict()
method.
The list preds_for_0
(of length 50) stores the labels predicted for X_for_RF[0]
by each constituent tree. Exploring this list will easily give you the majority label and also which tree gave what label to the instance.
Upvotes: 3