displaying scikit decision tree figure in jupyter notebook

Jürgen Erhardt picture Jürgen Erhardt · Jan 25, 2019 · Viewed 7.4k times · Source

I am currently creating a machine learning jupyter notebook as a small project and wanted to display my decision trees. However, all options I can find are to export the graphics and then load a picture, which is rather complicated.

Therefore, I wanted to ask whether there is a way to display my decision trees directly without exporting and loading graphics.

Answer

hellpanderr picture hellpanderr · Jan 26, 2019

You can show the tree directly using IPython.display:

import graphviz
from sklearn.tree import DecisionTreeRegressor, DecisionTreeClassifier,export_graphviz
from sklearn.datasets import make_regression

# Generate a simple dataset
X, y = make_regression(n_features=2, n_informative=2, random_state=0)
clf = DecisionTreeRegressor(random_state=0, max_depth=2)
clf.fit(X, y)
# Visualize the tree
from IPython.display import display
display(graphviz.Source(export_graphviz(clf)))