调用异常:未找到 GraphViz 的可执行文件



我无法可视化或编写决策树。我该怎么做?

Python 版本 3.5,Anaconda 3,我什至设置了环境变量

from sklearn import tree
model = tree.DecisionTreeClassifier(criterion='gini') 
model=tree.DecisionTreeClassifier()
model.fit(trainData,trainLabel)
model.score(trainData,trainLabel)
predicted= model.predict(testData)
from sklearn.externals.six import StringIO
import pydot 
import pydotplus
dot_data = StringIO() 
tree.export_graphviz(model, out_file=dot_data) 
graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) 
print(graph)
graph.write_pdf("C:\Users\anagha\Desktop\SynehackData\DATA\DATA\graph.pdf") 

错误:

InvocationException: GraphViz's executables not found

我知道线程有点旧,但今天我在尝试使用 PyAgrum 库在 Jupyter 笔记本中可视化贝叶斯网络时遇到了同样的错误。

我在Windows 10上使用Anaconda软件包管理。就我而言,我需要使用以下命令python-graphviz安装软件包:

conda install python-graphviz

安装完成后,我只需要重新启动 jupyter 内核并再次运行代码。

我得到了这个错误并尝试了一百万件事。 我看到我应该添加到环境变量中,如果您在 Windows 中,则为"路径"。 我这样做了,重新启动了Python,但它不起作用。 我为graphviz和pydotplus做了这件事。

然后我看到有人使用了与我使用的路径略有不同的路径。 类似的东西 Drive:\Users\User.Name\AppData\Local\Continuum\anaconda3\envs\MyVirtualEnv\Library\bin\graphviz 所以我把它添加到路径中,并重新启动了所有的事情蟒蛇。 这可能是我尝试的第98件事。成功了!

我一直在使用这样的路径 Drive:\Users\User.Name\AppData\Local\Continuum\anaconda3\envs\MyVirtualEnv\lib\site-packages\graphviz,它不起作用,但我同时放入了两者,以及pydotplus的类似内容。

其他解决方案对我没有帮助。

我已经安装了以下内容:

graphviz==2.50.0
pydotplus==2.0.2
pydot==1.4.1

但是运行whereis dotwhereis graphviz,很明显我的操作系统上仍然缺少 graphviz 库:对于点,whereis 命令在我的系统上返回了一个路径,对于 graphviz,whereis 命令没有打印路径。

对我有帮助的(在 Ubuntu 上)是运行sudo apt-get install graphviz,因为 python 包 graphviz (https://pypi.org/project/graphviz/) 的 PyPi 页面提到了以下内容:

要呈现生成的DOT源代码,您还需要安装Graphviz(下载页面,存档版本,Windows的安装过程)。

上面链接的下载页面提到sudo apt-get install graphviz是Ubuntu上的方式。如果您有其他操作系统,请查看上面的下载页面,了解在特定操作系统上安装 graphviz 的方法。

你可以利用这段代码!

import pydotplus
from sklearn.datasets import load_iris
from sklearn import tree
import collections
# Data Collection
X = [ [180, 15,0],     
[177, 42,0],
[136, 35,1],
[174, 65,0],
[141, 28,1]]
Y = ['man', 'woman', 'woman', 'man', 'woman']    
data_feature_names = [ 'height', 'hair length', 'voice pitch' ]
# Training
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X,Y)
# Visualize data
dot_data = tree.export_graphviz(clf,
feature_names=data_feature_names,
out_file=None,
filled=True,
rounded=True)
graph = pydotplus.graph_from_dot_data(dot_data)
colors = ('turquoise', 'orange')
edges = collections.defaultdict(list)
for edge in graph.get_edge_list():
edges[edge.get_source()].append(int(edge.get_destination()))
for edge in edges:
edges[edge].sort()    
for i in range(2):
dest = graph.get_node(str(edges[edge][i]))[0]
dest.set_fillcolor(colors[i])
graph.write_png('tree.png')

最新更新