Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a graph in pydot from decision tree in sklearn Python

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.

like image 519
skmathur Avatar asked Mar 22 '23 21:03

skmathur


1 Answers

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.

like image 96
ajsmith007 Avatar answered Apr 07 '23 07:04

ajsmith007