2

I've studied the solutions outlined here:

However, none of these solutions work for me. In particular, when I try the check_call method, I get the following error:

  File "/Users/anaconda/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
  OSError: [Errno 2] No such file or directory

When I use pydot, I get this error: (graph,)=pydot.graph_from_dot_data(dotfile.getvalue()) TypeError: 'Dot' object is not iterable

Here is some example code I found on one of the above posts that I've been testing:

from sklearn import tree  
import pydot
import StringIO
from subprocess import check_call

# Define training and target set for the classifier
train = [[1,2,3],[2,5,1],[2,1,7]]
target = [10,20,30]

# Initialize Classifier. Random values are initialized with always the same random seed of value 0 
# (allows reproducible results)
dectree = tree.DecisionTreeClassifier(random_state=0)
dectree.fit(train, target)

# Test classifier with other, unknown feature vector
test = [2,2,3]
predicted = dectree.predict(test)

dotfile = StringIO.StringIO()
tree.export_graphviz(dectree, out_file=dotfile)
check_call(['dot','-Tpng','InputFile.dot','-o','OutputFile.png'])


(graph,)=pydot.graph_from_dot_data(dotfile.getvalue())
graph.write_png("dtree.png")

Thanks in advance.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
user3761743
  • 145
  • 3
  • 14

1 Answers1

0

OSError: [Errno 2] No such file or directory indicates that InputFile.dot may not be present on your filesystem.

In case you're just interested in converting dot to png, I've created a simple python example sample_tree.py which generates a png from a dot file which is working on my mac,

 import pydot
 from subprocess import check_call
 graph = pydot.Dot(graph_type='graph')
 for i in xrange(2):
     edge = pydot.Edge("a", "b%d" % i)
     graph.add_edge(edge)
 graph.write_png('sample_tree.png')

 # If a dot file needs to be created as well
 graph.write_dot('sample_tree.dot')
 check_call(['dot','-Tpng','sample_tree.dot','-o','OutputFile.png'])

Btw, this dtree example has also been used here Sckit learn with GraphViz exports empty outputs just in case any other similar issues are encountered. Thanks.

user1427026
  • 861
  • 2
  • 16
  • 32