Decision Tree Regressors and Classifiers are being widely used as separate algorithms or as components for more complex models. Visualizing them is crucial in order to correctly understand how certain decisions are being made inside the algorithm, which is always important for business applications.

In this short tutorial, I would like to briefly describe the process of visualizing Decision Tree models from sklearn library. Note: Graphviz installed and configured is required to run the code below.

As a toy dataset, I will be using a well known Iris dataset. Let’s import the main libraries and download the data for the experiment.

Now we will just create a simple Decision Tree Classifier and fit it on the full dataset.

Finally, the interesting steps are coming. We export our fitted decision tree as a .dot file, which is the standard extension for graphviz files. The tree.dot file will be saved in the same directory as your Jupyter Notebook script. Don’t forget to include the _feature_names _parameter, which indicates the feature names, that will be used when displaying the tree.

Now, by running the following command we will convert the .dot file to **.png **file. This will work only in Jupyter Notebook, as the “!” symbol indicates that the command will be performed directly in the console.

After this manipulation, the tree.png file will appear in the same folder. Now we can easily display it, using well-known libraries.

#decision-tree #machine-learning #data-science #python #visualization

Visualizing Decision Trees in Jupyter Notebook
2.80 GEEK