Home » Uncategorized

How to Visualize a Decision Tree from a Random Forest in Python using Scikit-Learn

This article was written by Will Koehrsen.

Here’s the complete code: just copy and paste into a Jupyter Notebook or Python script, replace with your data and run:

The final result is a complete decision tree as an image.


Explanation of code 

  1. Create a model train and extract: we could use a single decision tree, but since I often employ the

    random forest for modeling it’s used in this example. (The trees will be slightly different from one another!).

  2. Export Tree as .dot File: This makes use of the export_graphviz function in Scikit-Learn. There are many parameters here that control the look and information displayed. Take a look at the documentation for specifics.
  3. Convert dot to png using a system command: running system commands in Python can be handy for carrying out simple tasks. This requires installation of graphviz which includes the dot utility. For the complete options for conversion, take a look at the documentation.
  4. Visualize: the best visualizations appear in the Jupyter Notebook. (Equivalently you can use matplotlib to show images).

To read the rest of this article with code and illustrations, click here.

DSC Resources

Leave a Reply

Your email address will not be published. Required fields are marked *