我研究了这里概述的解决方案:
然而,这些解决方案都不适合我。特别是,当我尝试check_call方法时,我收到以下错误:
File "/Users/anaconda/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
当我使用pydot时,我收到此错误:(graph,)=pydot.graph_from_dot_data(dotfile.getvalue())
TypeError: 'Dot' object is not iterable
以下是我在上述帖子之一中发现的一些示例代码:
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")
提前致谢。
答案 0 :(得分:0)
OSError:[Errno 2]没有这样的文件或目录表明您的文件系统上可能没有InputFile.dot。
如果您只想将点转换为png,我已经创建了一个简单的python示例sample_tree.py,它可以从我的mac上运行的点文件生成一个png,
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'])
顺便说一句,这个dtree示例也在Sckit learn with GraphViz exports empty outputs使用,以防遇到任何其他类似问题。感谢。