解决使用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 查看list中是否含有某元素的方法

用关键字 in 和not in 来 如下: qwe =[1,2,3,4,5] if 2 in qwe: print ‘good!' else: print ‘not goo...

python轻松实现代码编码格式转换

  最近刚换工作不久,没太多的时间去整理工作中的东西,大部分时间都在用来熟悉新公司的业务,熟悉他们的代码框架了,最主要的是还有很多新东西要学,我之前主要是做php后台开发的,来这边之后还...

深入浅析python定时杀进程

之前写了个python脚本用selenium+phantomjs爬新帖子,在循环拉取页面的过程中,phantomjs总是block住,使用WebDriverWait设置最长等待时间无效。...

Python.append()与Python.expand()用法详解

如下所示: alist=[1,2]] >>>[1,2] alist.append([3,4]) >>>[1, 2, [3, 4]] alist...

Django ORM 查询管理器源码解析

ORM 查询管理器 对于 ORM 定义: 对象关系映射, Object Relational Mapping, ORM, 是一种程序设计技术,用于实现面向对象编程语言里不同类型系统的数...