Visualize or Print Decision Tree Algorithm Model

As a machine learning engineer you may have created the Decision Tree Model, but have you ever tried to visualize it.

If not , this is the post for you.

In this post we will learn how to Visualize or Print Decision Tree Model in Jupyter notebook.

Let us import the required library:

from IPython.display import Image  
from sklearn import tree
import pydotplus

After that we will use the export_graphviz method of tree class and pass the names of features and class used to Train the Decision Tree Classifier Model named DecisionTreeClfModel. If you are not aware how to create a decision tree classifier model please follow the link above to read about that. You may also like to read Train the Decision Tree Regression Model.

tree_data = tree.export_graphviz(DecisionTreeClfModel, out_file=None,feature_names=['x1','x2'],class_names=['0','1'])

then you can use the graph_from_dot_data function of pydotplus to create the graph and use the Image class to print the graph generated.

tree_graph = pydotplus.graph_from_dot_data(tree_data)  
Visualize and Print Decision Tree Classification or Regression Model
Visualize and Print Decision Tree Classification or Regression Model

You may optionally like to explore Top Advantages and Disadvantages of Decision Tree Algorithm.

I hope you enjoyed this article and can start using some of the techniques described here in your own projects soon. Cheers !!


Leave a Reply