Skip to content

Instantly share code, notes, and snippets.

@ratchetwrench
Last active August 8, 2017 01:54
Show Gist options
  • Save ratchetwrench/3c8782b8d431f4a62329a8031fac9b78 to your computer and use it in GitHub Desktop.
Save ratchetwrench/3c8782b8d431f4a62329a8031fac9b78 to your computer and use it in GitHub Desktop.
Plot a CART Decision Tree from sklearn with pydotplus and graphviz
def plot_tree():
"""Make sure you have the correct imports.
Imports:
from sklearn import tree
import pydotplus
from IPython.display import Image
Note: Make sure you already have a 'tree'.
...
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, y)
...
"""
dot_data = tree.export_graphviz(clf, out_file=None)
graph = pydotplus.graph_from_dot_data(dot_data)
return Image(graph.create_png())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment