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: