决策树报错GraphViz‘s executables not found

这篇博客介绍了如何在Python中使用sklearn库训练决策树模型,并通过pydotplus和graphviz展示决策树结构。在遇到'GraphViz executables not found'错误时,作者提供了解决方法,包括安装graphviz和添加环境变量。通过这些步骤,最终能够在Jupyter Notebook中成功显示决策树图表。
摘要由CSDN通过智能技术生成

决策树代码

from sklearn.datasets import load_iris
import pydotplus
from IPython.display import Image
from sklearn import tree
#训练模型
iris=load_iris()
clf=tree.DecisionTreeClassifier()
clf=clf.fit(iris.data,iris.target)

#绘图
dot_data=tree.export_graphviz(decision_tree=clf,
                              out_file=None,
                              rounded=True,
                              filled=True,
                              feature_names=iris.feature_names)
graph=pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png())

安装

安装pydotplus

pip install pydotplus

运行报错: GraphViz’s executables not found

安装graphviz

pip install graphviz

运行仍报错: GraphViz’s executables not found

查阅资料后,Graphviz属于一个独立的软件,需要到官网下载安装

网站:http://www.graphviz.org/Download

添加环境变量

有两种方法,亲测推荐第二种

第一种 环境变量中增加GraphViz的bin目录的路径
第二种 os方法

cmd中

python

进入python环境

import os
os.environ["PATH"]+= os.pathsep+"C:/Program Files/Graphviz/bin"  # 换成你自己的Graphviz路径

大功告成!!!

运行结果

如果和我一样是在jupyter notebook中使用的话,需要重启jupyter notebook

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值