Make a graph in pydot from decision tree in sklearn Python

skmathur picture skmathur · Oct 27, 2013 · Viewed 10.3k times · Source

I expected this code to create a PDF graph of the tree.

from sklearn import datasets,tree
import StringIO
import pydot
from sklearn.externals.six import StringIO  

iris = datasets.load_iris()

clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris['data'],iris['target'])

dot_data = StringIO.StringIO()
tree.export_graphviz(clf, out_file=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")

Is there a way to do what I want though pydot? This way is a dead end.

Explaining the problem further, the code fails in the last statement. graph.write_pdf() is looking for Graphviz in graph.progs() but there are no entries there. The error message says Graphviz executable not found.

Anyhow I was able to pdf file by invoking the dot.exe command in a DOS terminal, but it would be better to use pydot to do this step.

Answer

ajsmith007 picture ajsmith007 · Apr 19, 2014

After you add the PATH variable it looks like you are importing StringIO then calling StringIO.StringIO()

try just calling:

dot_data = StringIO()

Works for my python 2.7.6 Win7 x64 environment with the pydot and graphviz error: Couldn't import dot_parser, loading of dot files will not be possible fix.