-1

I am trying to plot some a decision tree using graphviz and pydot using SKLearn in python. From the python code:

import numpy as np
from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris(
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)
test_idx=[0,50,100]

tree.export_graphviz(clf,
        out_file='tree.dot')#gives text file
print iris.feature_names
print iris.target_names
from sklearn.externals.six import StringIO
import pydot
dot_data = StringIO() 
tree.export_graphviz(clf, out_file=dot_data) 
graph = pydot.graph_from_dot_data(dot_data.getvalue()) 
graph[0].write_pdf("iris.pdf")

I get the error:

Traceback (most recent call last):
  File "/Users/student/Desktop/Data Structures/decision_tree_visualization.py", line 18, in <module>
    graph[0].write_pdf("iris.pdf")
  File "build/bdist.macosx-10.6-intel/egg/pydot.py", line 1691, in <lambda>
    self.write(path, format=f, prog=prog))
  File "build/bdist.macosx-10.6-intel/egg/pydot.py", line 1774, in write
    s = self.create(prog, format)
  File "build/bdist.macosx-10.6-intel/egg/pydot.py", line 1883, in create
    prog=prog))
Exception: "dot" not found in path.

I have tried reinstalling pydot and graphviz to no avail. Any help?

Abe Kipnis
  • 25
  • 7
  • I figured out how to get a visualization by running dot -Tpng tree.dot -o tree.png in my Terminal... but I still want to figure this out. – Abe Kipnis Jul 08 '17 at 21:10
  • 1) try : sudo apt-get install graphviz 2) see here: [link](https://stackoverflow.com/questions/40243753/exception-dot-not-found-in-path-in-python-on-mac). 3) the PyGraphviz bin directory should be added to the path. 4) [see also here](https://stackoverflow.com/questions/13987353/python-valueerror-program-dot-not-found-in-path) – seralouk Jul 08 '17 at 22:37

1 Answers1

0

It sounds like graphviz has not been added to the system path. See this Stack Overflow questionand this more detailed explanation.

Not every method of installed sets the path correctly (especially pip install).

Alan
  • 2,914
  • 2
  • 14
  • 26