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

yipeiwu_com5年前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设计】。

相关文章

Python获取基金网站网页内容、使用BeautifulSoup库分析html操作示例

本文实例讲述了Python获取基金网站网页内容、使用BeautifulSoup库分析html操作。分享给大家供大家参考,具体如下: 利用 urllib包 获取网页内容 #引入包 fr...

TensorFlow Session会话控制&Variable变量详解

TensorFlow Session会话控制&Variable变量详解

这篇文章主要讲TensorFlow中的Session的用法以及Variable。 Session会话控制 Session是TensorFlow为了控制和输出文件的执行语句,运行sessi...

Python实现PS滤镜中马赛克效果示例

Python实现PS滤镜中马赛克效果示例

本文实例讲述了Python实现PS滤镜中马赛克效果。分享给大家供大家参考,具体如下: 这里利用 Python 实现PS 滤镜中的马赛克效果,具体的算法原理和效果可以参考附录说明,Pyth...

在Pytorch中使用样本权重(sample_weight)的正确方法

step: 1.将标签转换为one-hot形式。 2.将每一个one-hot标签中的1改为预设样本权重的值 即可在Pytorch中使用样本权重。 eg: 对于单个样本:loss = -...

5款Python程序员高频使用开发工具推荐

5款Python程序员高频使用开发工具推荐

很多Python学习者想必都会有如下感悟:最开始学习Python的时候,因为没有去探索好用的工具,吃了很多苦头。后来工作中深刻体会到,合理使用开发的工具的便利和高效。今天,我就把Pyth...