I want to plot a decision tree of a random forest. So, i create the following code:
clf = RandomForestClassifier(n_estimators=100)
import pydotplus
import s
To access the single decision tree from the random forest in scikit-learn use estimators_ attribute:
rf = RandomForestClassifier()
# first decision tree
rf.estimators_[0]
Then you can use standard way to visualize the decision tree:
export_textexport_graphviz methodplot_tree methoddtreeviz package for tree plottingThe code with example output are described in this post.
The important thing to while plotting the single decision tree from the random forest is that it might be fully grown (default hyper-parameters). It means the tree can be really depth. For me, the tree with depth greater than 6 is very hard to read. So if the tree visualization will be needed I'm building random forest with max_depth < 7. You can check the example visualization in this post.