Subscribe to DSC Newsletter

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

Views: 6208

Comment

You need to be a member of Data Science Central to add comments!

Join Data Science Central

Comment by Renzo Valencia on February 20, 2019 at 5:21am
It's a great contribution, I'm working on something similar with R and the Tensorflow package but for analysis 
in emergency detection and mine tailings. but this for ecology is a big initial contribution

Videos

  • Add Videos
  • View All

© 2019   Data Science Central ®   Powered by

Badges  |  Report an Issue  |  Privacy Policy  |  Terms of Service