【问题标题】:macos: converting dot to pngmacos:将点转换为png
【发布时间】:2018-01-05 03:18:54
【问题描述】:

我研究了这里列出的解决方案:

但是,这些解决方案都不适合我。特别是,当我尝试 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")

提前致谢。

【问题讨论】:

    标签: python dot


    【解决方案1】:

    OSError: [Errno 2] No such file or directory 表明 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,以防万一遇到任何其他类似问题。谢谢。

    【讨论】:

      猜你喜欢
      • 2011-07-16
      • 2022-10-06
      • 2018-04-03
      • 2011-04-30
      • 2011-01-01
      • 2010-10-13
      • 2011-01-18
      • 1970-01-01
      • 2017-06-10
      相关资源
      最近更新 更多