机器学习——决策树的可视化配置graphviz +windows+pygraphcizd

机器学习——决策树的可视化配置

                                                                    ——Windows graphviz 以及 pygraphcizd 的使用

资料已打包如下,下载地址为:https://pan.baidu.com/s/1RfYrSQ3i9qfOlOY2cjfI_g,密码为:k4go


主要用于二叉树,决策树的学习,直观显示二叉树。

1、如何在python中安装graphviz?

官网:https://pypi.python.org/pypi/graphviz,下载graphviz 0.8.2版本,然后再用pip进行安装,具体步骤如下:

(1)下载


(2)安装:找到路径D:\python2.7\anaconda2.4\Scripts下的pip,详细步骤如下


(3)在pycharm中测试是否安装成功




2、graphviz-2.38.zip下载与安装

下载地址:http://download.csdn.net/download/u010953266/8591169

安装方式地址:http://blog.csdn.net/lanchunhui/article/details/49472949

由于安装后软件没有桌面图标,如何运行?

可以发送到桌面(快捷方式)



打开后的界面为


3、如何在python中安装pygraphviz

下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pygraphviz


具体的安装方式与安装graphviz一样,只是不用配置环境变量。

4、决策树的应用说明

机器学习之决策树练习网址:http://scikit-learn.org/stable/modules/tree.html

代码如下:

from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)

import graphviz
dot_data = tree.export_graphviz(clf, out_file=None)
graph = graphviz.Source(dot_data)
graph.render("iris")

# 添加颜色
dot_data = tree.export_graphviz(clf, out_file=None,
                         feature_names=iris.feature_names,
                         class_names=iris.target_names,
                         filled=True, rounded=True,
                         special_characters=True)
graph = graphviz.Source(dot_data)
graph

如何运行并生出最终的决策树图片?断点调试:shift+F9




点击后得到的结构序列为:

digraph Tree {
node [shape=box, style="filled, rounded", color="black", fontname=helvetica] ;
edge [fontname=helvetica] ;
0 [label=<petal width (cm) ≤ 0.8<br/>gini = 0.6667<br/>samples = 150<br/>value = [50, 50, 50]<br/>class = setosa>, fillcolor="#e5813900"] ;
1 [label=<gini = 0.0<br/>samples = 50<br/>value = [50, 0, 0]<br/>class = setosa>, fillcolor="#e58139ff"] ;
0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
2 [label=<petal width (cm) ≤ 1.75<br/>gini = 0.5<br/>samples = 100<br/>value = [0, 50, 50]<br/>class = versicolor>, fillcolor="#39e58100"] ;
0 -> 2 [labeldistance=2.5, labelangle=-45, headlabel="False"] ;
3 [label=<petal length (cm) ≤ 4.95<br/>gini = 0.168<br/>samples = 54<br/>value = [0, 49, 5]<br/>class = versicolor>, fillcolor="#39e581e5"] ;
2 -> 3 ;
4 [label=<petal width (cm) ≤ 1.65<br/>gini = 0.0408<br/>samples = 48<br/>value = [0, 47, 1]<br/>class = versicolor>, fillcolor="#39e581fa"] ;
3 -> 4 ;
5 [label=<gini = 0.0<br/>samples = 47<br/>value = [0, 47, 0]<br/>class = versicolor>, fillcolor="#39e581ff"] ;
4 -> 5 ;
6 [label=<gini = 0.0<br/>samples = 1<br/>value = [0, 0, 1]<br/>class = virginica>, fillcolor="#8139e5ff"] ;
4 -> 6 ;
7 [label=<petal width (cm) ≤ 1.55<br/>gini = 0.4444<br/>samples = 6<br/>value = [0, 2, 4]<br/>class = virginica>, fillcolor="#8139e57f"] ;
3 -> 7 ;
8 [label=<gini = 0.0<br/>samples = 3<br/>value = [0, 0, 3]<br/>class = virginica>, fillcolor="#8139e5ff"] ;
7 -> 8 ;
9 [label=<sepal length (cm) ≤ 6.95<br/>gini = 0.4444<br/>samples = 3<br/>value = [0, 2, 1]<br/>class = versicolor>, fillcolor="#39e5817f"] ;
7 -> 9 ;
10 [label=<gini = 0.0<br/>samples = 2<br/>value = [0, 2, 0]<br/>class = versicolor>, fillcolor="#39e581ff"] ;
9 -> 10 ;
11 [label=<gini = 0.0<br/>samples = 1<br/>value = [0, 0, 1]<br/>class = virginica>, fillcolor="#8139e5ff"] ;
9 -> 11 ;
12 [label=<petal length (cm) ≤ 4.85<br/>gini = 0.0425<br/>samples = 46<br/>value = [0, 1, 45]<br/>class = virginica>, fillcolor="#8139e5f9"] ;
2 -> 12 ;
13 [label=<sepal length (cm) ≤ 5.95<br/>gini = 0.4444<br/>samples = 3<br/>value = [0, 1, 2]<br/>class = virginica>, fillcolor="#8139e57f"] ;
12 -> 13 ;
14 [label=<gini = 0.0<br/>samples = 1<br/>value = [0, 1, 0]<br/>class = versicolor>, fillcolor="#39e581ff"] ;
13 -> 14 ;
15 [label=<gini = 0.0<br/>samples = 2<br/>value = [0, 0, 2]<br/>class = virginica>, fillcolor="#8139e5ff"] ;
13 -> 15 ;
16 [label=<gini = 0.0<br/>samples = 43<br/>value = [0, 0, 43]<br/>class = virginica>, fillcolor="#8139e5ff"] ;
12 -> 16 ;
}

最后将方框的内容复制到graphviz中,得到下图:



参考网址:

1、pygraphviz的使用与安装

https://www.cnblogs.com/youngzii/archive/2013/05/23/Python.html

2、windows下graphviz与pygraphviz的安装及入门教程

http://blog.csdn.net/chirebingxue/article/details/50393755

2、安装pygraphviz

http://blog.csdn.net/fencingworld/article/details/53047901





已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页