Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

The problem appears to be that your PATH statement does not include a reference to GraphViz - so your pydot module can't find it. To verify that this is the problem, type "PATH" from your Windows cmd window.

C:\Users\Ron Fredericks>path PATH=C:\Program Files\CollabNet\Subversion Client;C:\Program Files (x86)\Graphviz2.34\bin

The simplified path output above shows that my system includes Graphviz in my PATH. If you don't have Graphviz in your PATH, look for the answer on how to get Graphviz installed into your user path here at SO searching on this string "Graphviz executable not found"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top