使用pycharm 调用import graphviz画出 决策树

在pycharm中我们建立了一个树

dot_data = tree.export_graphviz(clf
                                    ,out_file = None
                                    ,feature_names= feature_name
                                    ,class_names=["琴酒","雪莉","贝尔摩德"]
                                    ,filled=True
                                    ,rounded=True
                                    )
graph = graphviz.Source(dot_data)
print(graph)

然后将打印出来的树copy

digraph Tree {
node [shape=box, style="filled, rounded", color="black", fontname=helvetica] ;
edge [fontname=helvetica] ;
0 [label="类黄酮 <= 2.315\nentropy = 1.543\nsamples = 124\nvalue = [45, 51, 28]\nclass = 雪莉", fillcolor="#f0fdf5"] ;
1 [label="颜色强度 <= 3.825\nentropy = 0.986\nsamples = 65\nvalue = [0, 37, 28]\nclass = 雪莉", fillcolor="#cff9e0"] ;
0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
2 [label="entropy = 0.0\nsamples = 34\nvalue = [0, 34, 0]\nclass = 雪莉", fillcolor="#39e581"] ;
1 -> 2 ;
3 [label="类黄酮 <= 1.4\nentropy = 0.459\nsamples = 31\nvalue = [0, 3, 28]\nclass = 贝尔摩德", fillcolor="#8e4ee8"] ;
1 -> 3 ;
4 [label="entropy = 0.0\nsamples = 27\nvalue = [0, 0, 27]\nclass = 贝尔摩德", fillcolor="#8139e5"] ;
3 -> 4 ;
5 [label="非黄烷类酚类 <= 0.245\nentropy = 0.811\nsamples = 4\nvalue = [0, 3, 1]\nclass = 雪莉", fillcolor="#7beeab"] ;
3 -> 5 ;
6 [label="entropy = 0.0\nsamples = 1\nvalue = [0, 0, 1]\nclass = 贝尔摩德", fillcolor="#8139e5"] ;
5 -> 6 ;
7 [label="entropy = 0.0\nsamples = 3\nvalue = [0, 3, 0]\nclass = 雪莉", fillcolor="#39e581"] ;
5 -> 7 ;
8 [label="脯氨酸 <= 679.0\nentropy = 0.791\nsamples = 59\nvalue = [45, 14, 0]\nclass = 琴酒", fillcolor="#eda877"] ;
0 -> 8 [labeldistance=2.5, labelangle=-45, headlabel="False"] ;
9 [label="entropy = 0.0\nsamples = 12\nvalue = [0, 12, 0]\nclass = 雪莉", fillcolor="#39e581"] ;
8 -> 9 ;
10 [label="酒精 <= 13.02\nentropy = 0.254\nsamples = 47\nvalue = [45, 2, 0]\nclass = 琴酒", fillcolor="#e68742"] ;
8 -> 10 ;
11 [label="od280/od315稀释葡萄酒 <= 3.51\nentropy = 1.0\nsamples = 4\nvalue = [2, 2, 0]\nclass = 琴酒", fillcolor="#ffffff"] ;
10 -> 11 ;
12 [label="entropy = 0.0\nsamples = 2\nvalue = [0, 2, 0]\nclass = 雪莉", fillcolor="#39e581"] ;
11 -> 12 ;
13 [label="entropy = 0.0\nsamples = 2\nvalue = [2, 0, 0]\nclass = 琴酒", fillcolor="#e58139"] ;
11 -> 13 ;
14 [label="entropy = 0.0\nsamples = 43\nvalue = [43, 0, 0]\nclass = 琴酒", fillcolor="#e58139"] ;
10 -> 14 ;
}

粘贴到我们安装的软件graphviz中(安装包在这里:https://download.csdn.net/download/weixin_38135620/15246832?utm_medium=distribute.pc_relevant_t0.none-task-download-BlogCommendFromMachineLearnPai2-1.control&dist_request_id=0509a4d0-e949-491a-bd9b-2c361d14f2b9&depth_1-utm_source=distribute.pc_relevant_t0.none-task-download-BlogCommendFromMachineLearnPai2-1.control)

安装之后,将环境变量G:\software\graphviz\bin(注意:是你自己安装路径)添加到系统变量里:

使用pycharm 调用import graphviz画出 决策树_第1张图片

打开软件,粘贴

使用pycharm 调用import graphviz画出 决策树_第2张图片

注意:直接复制的代码 字体格式要修改,不然中文会乱码,在pycharm里基于sklearn的wine数据生成决策树,遇到了决策树中文乱码的问题,是因为graphviz默认的fontname为helvetica,找到第2行和第3行的fontname=helvetica,将字体类型helvetica更改为中文字体类型,windows上的一些中文字体类型如下:

使用pycharm 调用import graphviz画出 决策树_第3张图片

点击小黑人就可以运行了。

使用pycharm 调用import graphviz画出 决策树_第4张图片

你可能感兴趣的:(机器学习之路)