【发布时间】:2017-12-12 21:52:35
【问题描述】:
我正在尝试使用 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 无济于事。有什么帮助吗?
【问题讨论】:
-
我想出了如何通过在终端中运行 dot -Tpng tree.dot -o tree.png 来获得可视化...但我仍然想弄清楚这一点。
-
1) 尝试:sudo apt-get install graphviz 2) 见此处:link。 3) PyGraphviz bin 目录应该添加到路径中。 4)see also here