Not Fitted Error when using Sklearn's graphviz
(Only going by the docs; no personal experience)
You are trying to plot some DecisionTree, using a function which signature reads:
sklearn.tree.export_graphviz(decision_tree, ...)
but you are passing a RandomForest, which is an ensemble of trees.
That's not going to work!
Going deeper, the code internally for this is here:
check_is_fitted(decision_tree, 'tree_')
So this is asking for the attribute tree_
of your DecisionTree, which exists for a DecisionTreeClassifier.
This attribute does not exist for a RandomForestClassifier! Therefore the error.
The only thing you can do: print every DecisionTree within your RandomForest ensemble. For this, you need to traverse random_forest.estimators_
to get the underlying decision-trees!
Like the other answer said, you cannot do this for a forest, only a single tree. You can, however, graph a single tree from that forest. Here's how to do that:
forest_clf = RandomForestClassifier()
forest_clf.fit(X_train, y_train)
tree.export_graphviz(forest_clf.estimators_[0], out_file='tree_from_forest.dot')
(graph,) = pydot.graph_from_dot_file('tree_from_forest.dot')
graph.write_png('tree_from_forest.png')
Unfortunately, there's no easy way to graph the "best" tree or an overall ensemble tree from your forest, just a random example tree.