sklearn决策树可视化时报错(graph.write_png()),出错原因是没有安装GraphViz's executables。Graphviz不是一个python tool,仍然需要安装GraphViz's executables.下载链接 http://www.graphviz.org/download/
对于一般IDE环境下安装后需要将Graphviz的bin路径添加到环境变量Path中,对于虚拟环境,例如conda,需要将bin路径下的内容拷贝到虚拟环境下的Library目录下(重复不覆盖)。
import pydotplus
import collections
data_feature_names = [']
# dot_data = export_graphviz(clf_DT, out_file='tree.dot')
dot_data = export_graphviz(clf_DT,
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')
Refs:
本文详细介绍了在使用sklearn进行决策树可视化时遇到的graph.write_png()错误的解决方案。错误源于GraphViz's executables未安装或路径配置不当。文章提供了正确的安装步骤和环境变量设置方法,适用于一般IDE和虚拟环境如conda。

2307

被折叠的 条评论
为什么被折叠?



