资料已打包如下,下载地址为: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= gini = 0.6667
samples = 150
value = [50, 50, 50]
class = setosa>, fillcolor="#e5813900"] ;
1 [label= samples = 50
value = [50, 0, 0]
class = setosa>, fillcolor="#e58139ff"] ;
0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
2 [label= gini = 0.5
samples = 100
value = [0, 50, 50]
class = versicolor>, fillcolor="#39e58100"] ;
0 -> 2 [labeldistance=2.5, labelangle=-45, headlabel="False"] ;
3 [label= gini = 0.168
samples = 54
value = [0, 49, 5]
class = versicolor>, fillcolor="#39e581e5"] ;
2 -> 3 ;
4 [label= gini = 0.0408
samples = 48
value = [0, 47, 1]
class = versicolor>, fillcolor="#39e581fa"] ;
3 -> 4 ;
5 [label= samples = 47
value = [0, 47, 0]
class = versicolor>, fillcolor="#39e581ff"] ;
4 -> 5 ;
6 [label= samples = 1
value = [0, 0, 1]
class = virginica>, fillcolor="#8139e5ff"] ;
4 -> 6 ;
7 [label= gini = 0.4444
samples = 6
value = [0, 2, 4]
class = virginica>, fillcolor="#8139e57f"] ;
3 -> 7 ;
8 [label= samples = 3
value = [0, 0, 3]
class = virginica>, fillcolor="#8139e5ff"] ;
7 -> 8 ;
9 [label= gini = 0.4444
samples = 3
value = [0, 2, 1]
class = versicolor>, fillcolor="#39e5817f"] ;
7 -> 9 ;
10 [label= samples = 2
value = [0, 2, 0]
class = versicolor>, fillcolor="#39e581ff"] ;
9 -> 10 ;
11 [label= samples = 1
value = [0, 0, 1]
class = virginica>, fillcolor="#8139e5ff"] ;
9 -> 11 ;
12 [label= gini = 0.0425
samples = 46
value = [0, 1, 45]
class = virginica>, fillcolor="#8139e5f9"] ;
2 -> 12 ;
13 [label= gini = 0.4444
samples = 3
value = [0, 1, 2]
class = virginica>, fillcolor="#8139e57f"] ;
12 -> 13 ;
14 [label= samples = 1
value = [0, 1, 0]
class = versicolor>, fillcolor="#39e581ff"] ;
13 -> 14 ;
15 [label= samples = 2
value = [0, 0, 2]
class = virginica>, fillcolor="#8139e5ff"] ;
13 -> 15 ;
16 [label= samples = 43
value = [0, 0, 43]
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