Python macos:将点转换为png
我已经研究了这里概述的解决方案:Python macos:将点转换为png,python,dot,Python,Dot,我已经研究了这里概述的解决方案: 然而,这些解决方案都不适合我。特别是,当我尝试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
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\u from\u dot\u数据(dotfile.getvalue())
TypeError:“点”对象不可编辑
下面是我在上面的一篇文章中找到的一些示例代码,我一直在测试这些代码:
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")
提前感谢。OSError:[Errno 2]没有这样的文件或目录表明文件系统中可能不存在InputFile.dot 如果您对将点转换为png感兴趣,我已经创建了一个简单的python示例示例_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示例,以防遇到任何其他类似问题。谢谢