解决使用export_graphviz可视化树报错的问题

yipeiwu_com6年前Python基础

在使用可视化树的过程中,报错了。说是‘dot.exe'not found in path

原代码:

# import tools needed for visualization
from sklearn.tree import export_graphviz
import pydot
 
#Pull out one tree from the forest
tree = rf.estimators_[5]
 
# Export the image to a dot file
export_graphviz(tree, out_file = 'tree.dot', feature_names = features_list, rounded = True, precision = 1)
 
#Use dot file to create a graph
(graph, ) = pydot.graph_from_dot_file('tree.dot')
 
# Write graph to a png file
graph.write_png('tree.png');

报错信息:

解决方法:

先使用安装pydot:

pip install pydot

然后再下载Graphviz(http://www.graphviz.org/ 选择msi版本)一路安装,记住默认的安装路径

c:\Program Files (x86)\Graphviz2.38\。

将Graphviz2.38添加到环境变量中

import os
os.environ['PATH'] = os.environ['PATH'] + (';c:\\Program Files (x86)\\Graphviz2.38\\bin\\')

之后便可以正常使用了。

修改后代码:

# import tools needed for visualization
from sklearn.tree import export_graphviz
import pydot
import os
 
os.environ['PATH'] = os.environ['PATH'] + (';c:\\Program Files (x86)\\Graphviz2.38\\bin\\')
 
#Pull out one tree from the forest
tree = rf.estimators_[5]
 
# Export the image to a dot file
export_graphviz(tree, out_file = 'tree.dot', feature_names = features_list, rounded = True, precision = 1)
 
#Use dot file to create a graph
(graph, ) = pydot.graph_from_dot_file('tree.dot')
 
# Write graph to a png file
graph.write_png('tree.png');

以上这篇解决使用export_graphviz可视化树报错的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

使用tqdm显示Python代码执行进度功能

使用tqdm显示Python代码执行进度功能

在使用Python执行一些比较耗时的操作时,为了方便观察进度,通常使用进度条的方式来可视化呈现。Python中的tqdm就是用来实现此功能的。 先来看看tqdm的进度条效果:  ...

python获取list下标及其值的简单方法

当在python中遍历一个序列时,我们通常采用如下的方法: for item in sequence: process(item) 如果要取到某个item的位置,可以...

在Python的Django框架中为代码添加注释的方法

就像HTML或者Python,Django模板语言同样提供代码注释。 注释使用 {# #} : {# This is a comment #} 注释的内容不会在模板渲染时输出。...

详解python里的命名规范

文件名 全小写,可使用下划线 包 应该是简短的、小写的名字。如果下划线可以改善可读性可以加入。如mypackage。 模块 与包的规范同。如mymodule。 类 总是使用首字母大写单词...

python获取文件版本信息、公司名和产品名的方法

本文实例讲述了python获取文件版本信息、公司名和产品名的方法,分享给大家供大家参考。具体如下: 该python代码可得到文件版本信息、公司名和产品名。其他的信息都在返回的字典中。具体...