I want to display the decision tree of the random forest that I bagged.

Asked 2 years ago, Updated 2 years ago, 54 views

I would like to display the one with the best test score among the random forest decision trees that I bagged.
I wrote the following code.
clf=BaggingClassifier(RandomForestClassifier(max_depth=depth, random_state=0), n_estimators=100, random_state=0)

# Build the model
clf=clf.fit(X_train,y_train)
# Display results by decision tree
train_score=np.zeros(100)
test_score=np.zeros(100)
for i, val in enumerate (clf.estimators_):
    model=clf.estimators_[i]
    model=model.fit(X_train,y_train)
    train_score[i] = model.score(X_train,y_train)
    test_score[i] = model.score(X_test,y_test)      

num=np.argmax(test_score)
# Visualize data
model=clf.estimators_[num]
model=model.fit(X_train,y_train)
dot_data=tree.export_graphviz(model, feature_names=["Tol", class_names=["C1", "good", "C5, D5"], filled=True, round=True)

The following error appears:

Traceback (most recent call last):
  File "rf.py", line 79, in<module>
    dot_data=tree.export_graphviz(model, feature_names=["Tol", class_names=["C1", "good", "C5, D5"], filled=True, round=True)
  File"/Library/Frameworks/Python.framework/Version/3.7/lib/python 3.7/site-packages/sklearn/tree/export.py", line 757, in export_graphviz
    check_is_fitted(decision_tree, 'tree_')
  File"/Library/Frameworks/Python.frameworks/Version/3.7/lib/python 3.7/site-packages/sklearn/utils/validation.py", line 914, check_is_fitted
    raise NotFittedError(msg%{'name':type(estimator).__name_})
sklearn.exceptions.NotFittedError: This RandomForestClassifier instance is not fitted yet.Call 'fit' with approve arguments before using this method.

python machine-learning

2022-09-30 20:18

1 Answers

Earlier question I also commented on where I get errors when I want to visualize the decision tree that I bagged, but so does RandomForestClassifier.

#Visualize data
model=clf.estimators_[num]
model=model.fit(X_train,y_train)
for i, tin enumerate (model.estimators_):
  dot_data=tree.export_graphviz(
    t, feature_names=["Tol", class_names=["C1", "good", "C5, D5",
    filled = True, round = True
  )
  pydotplus.graph_from_dot_data(dot_data).write_png(f'{(i+1):03d}.png')

After execution, a decision tree image file (001 to 100.png) is generated in the current directory.


2022-09-30 20:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.