Pydot& Graphviz设置

时间:2017-07-08 20:23:03

标签: python graphviz pydot

我正在尝试使用graphviz和pydot在python中使用SKLearn绘制一些决策树。 从python代码:

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")

我收到错误:

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.

我尝试重新安装pydot和graphviz无济于事。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

听起来,graphviz尚未添加到系统路径中。请参阅this Stack Overflow questionthis more detailed explanation

并非每种安装方法都能正确设置路径(尤其是pip install)。