决策树

引入

想象一下一个女孩的妈妈给她介绍男朋友的场景:

女儿:长的帅不帅?

妈妈:挺帅的。

女儿:有没有房子?

妈妈:在老家有一个。

女儿:收入高不高?

妈妈:还不错,年薪百万。

女儿:做什么工作的?

妈妈:IT 男,互联网公司做数据挖掘的。

女儿:好,那我见见。

在现实生活中,我们会遇到各种选择,不论是选择男女朋友,还是挑选水果,都是基于以往的经验来做判断。如果把判断背后的逻辑整理成一个结构图,你会发现它实际上是一个树状图,这就是我们今天要讲的决策树

决策树工作原理

决策树基本上就是把我们以前的经验总结出来。我给你准备了一个打篮球的训练集。如果我们要出门打篮球,一般会根据“天气”、“温度”、“湿度”、“刮风”这几个条件来判断,最后得到结果:去打篮球?还是不去?

在这里插入图片描述

上面这个图就是一棵典型的决策树。我们在做决策树的时候,会经历两个阶段:构造和剪枝

构造

什么是构造呢?构造就是生成一棵完整的决策树。简单来说,构造的过程就是选择什么属性作为节点的过程,那么在构造过程中,会存在三种节点:

  1. 根节点:就是树的最顶端,最开始的那个节点。在上图中,“天气”就是一个根节点;
  2. 内部节点:就是树中间的那些节点,比如说“温度”、“湿度”、“刮风”;
  3. 叶节点:就是树最底部的节点,也就是决策结果。

在这里插入图片描述

可以看出:决策树学习的关键在于如何选择划分属性,不同的划分属性得出不同的分支结构,从而影响整颗决策树的性 能。属性划分的目标是让各个划分出来的子节点尽可能地“纯”,即属于同一类别。因此下面便是介绍量化纯度的具体方 法,决策树最常用的算法有三种:ID3C4.5CART

剪枝

欠拟合与过拟合
在这里插入图片描述

ID3算法(判断要不要去打篮球)?

准备的示例数据集
在这里插入图片描述

那么我们要如何去构造一个判断是否去打篮球的决策树呢?

显然将哪个属性(天气、温度、湿度、刮风)作为根节点是个关键问题,在这里我们先介绍两个指标:纯度和信息熵

纯度换一种方式来解释就是让目标变量的分歧最小。

示例

  • 集合 1:6 次都去打篮球;
  • 集合 2:4 次去打篮球,2 次不去打篮球;
  • 集合 3:3 次去打篮球,3 次不去打篮球。

按照纯度指标来说,集合 1> 集合 2> 集合 3。因为集合 1 的分歧最小,集合 3 的分歧最大。

然后我们再来介绍信息熵(entropy)的概念,它表示了信息的不确定度。

在信息论中,随机离散事件出现的概率存在着不确定性。为了衡量这种信息的不确定性,信息学之父香农引入了信息熵的概念,并给出了计算信息熵的数学公式:

 Entropy  ( t ) = − ∑ i = 0 c − 1 p ( i ∣ t ) log ⁡ 2 p ( i ∣ t ) \text { Entropy }(t)=-\sum_{i=0}^{c-1} p(i \mid t) \log _{2} p(i \mid t)  Entropy (t)=i=0c1p(it)log2p(it)


p ( i ∣ t ) p(i \mid t) p(it)代表了节点 t t t为分类 i i i的概率

当不确定性越大时,它所包含的信息量也就越大,信息熵也就越高。

示例:

  • 集合 1:5 次去打篮球,1 次不去打篮球;

  • 集合 2:3 次去打篮球,3 次不去打篮球。

在集合 1 中,有 6 次决策,其中打篮球是 5 次,不打篮球是 1 次。那么假设:类别 1 为“打篮球”,即次数为 5;类别 2 为“不打篮球”,即次数为 1。那么节点划分为类别 1 的概率是 5/6,为类别 2 的概率是 1/6,带入上述信息熵公式可以计算得出:
 Entropy  ( t ) = − ( 1 / 6 ) log ⁡ 2 ( 1 / 6 ) − ( 5 / 6 ) log ⁡ 2 ( 5 / 6 ) = 0.65 \text { Entropy }(t)=-(1 / 6) \log _{2}(1 / 6)-(5 / 6) \log _{2}(5 / 6)=0.65  Entropy (t)=(1/6)log2(1/6)(5/6)log2(5/6)=0.65


集合 2 中,也是一共 6 次决策,其中类别 1 中“打篮球”的次数是 3,类别 2“不打篮球”的次数也是 3,那么信息熵为多少呢?我们可以计算得出:

 Entropy  ( t ) = − ( 3 / 6 ) log ⁡ 2 ( 3 / 6 ) − ( 3 / 6 ) log ⁡ 2 ( 3 / 6 ) = 1 \text { Entropy }(t)=-(3 / 6) \log _{2}(3 / 6)-(3 / 6) \log _{2}(3 / 6)=1  Entropy (t)=(3/6)log2(3/6)(3/6)log2(3/6)=1

从上面的计算结果中可以看出,信息熵越大,纯度越低。当集合中的所有样本均匀混合时,信息熵最大,纯度最低。

我们在构造决策树的时候,会基于纯度来构建。而经典的 “不纯度”的指标有三种,分别是信息增益(ID3 算法)、信息增益率(C4.5 算法)以及基尼指数(Cart 算法)



ID3 算法计算的是信息增益,**信息增益**指的就是划分可以带来纯度的提高,信息熵的下降。 所以信息增益的公式可以表示为:

Gain ⁡ ( D , a ) = Entrop ⁡ y ( D ) − ∑ i = 1 k ∣ D i ∣ ∣ D ∣ Entropy ( D i ) \operatorname{Gain}(D, a)=\operatorname{Entrop} y(D)-\sum_{i=1}^{k} \frac{\left|D_{i}\right|}{|D|} \text {Entropy}\left(D_{i}\right) Gain(D,a)=Entropy(D)i=1kDDiEntropy(Di)


公式中 D D D是父亲节点, D i D_i Di是子节点, G a i n ( D , a ) Gain(D,a) Gain(D,a) 中的 a a a 作为 D D D 节点的属性选择。

示例

假设天气 = 晴的时候,会有 5 次去打篮球,5 次不打篮球。

  • 其中 D1 刮风 = 是,有 2 次打篮球,1 次不打篮球

  • D2 刮风 = 否,有 3 次打篮球,4 次不打篮球




    那么 a 代表节点的属性,即天气 = 晴。

针对图上这个例子,D 作为节点的信息增益为:
Gain ⁡ ( D , a ) = Entropy ( D ) − ( 3 10 Entropy ( D 1 ) + 7 10 Entropy ( D 2 ) ) \operatorname{Gain}(D, a)=\text {Entropy}(D)-\left(\frac{3}{10} \text {Entropy}\left(D_{1}\right)+\frac{7}{10} \text {Entropy}\left(D_{2}\right)\right) Gain(D,a)=Entropy(D)(103Entropy(D1)+107Entropy(D2))

我们基于 ID3 的算法规则,完整地计算下我们的训练集
在这里插入图片描述
根节点的信息熵
Ent ⁡ ( D ) = − ∑ k = 1 2 p k log ⁡ 2 p k = − ( 3 7 log ⁡ 2 3 7 + 4 7 log ⁡ 2 4 7 ) = 0.985 \operatorname{Ent}(D)=-\sum_{k=1}^{2} p_{k} \log _{2} p_{k}=-\left(\frac{3}{7} \log _{2} \frac{3}{7}+\frac{4}{7} \log _{2} \frac{4}{7}\right)=0.985 Ent(D)=k=12pklog2pk=(73log273+74log274)=0.985

如果我们将天气作为属性的划分,会有三个叶子节点 D1、D2D3,分别对应的是晴天、阴天和小雨。我们用 + 代表去打篮球,- 代表不去打篮球。

那么记录方式为:

  • D 1 D_1 D1(天气 = 晴天)={1-,2-,6+}
  • D 2 D_2 D2(天气 = 阴天)={3+,7-}
  • D 3 D_3 D3(天气 = 小雨)={4+,5-}



    则三个叶子节点的信息熵为:
    Ent ⁡ ( D 1 ) = − ( 1 3 log ⁡ 2 1 3 + 2 3 log ⁡ 2 2 3 ) = 0.918 Ent ⁡ ( D 2 ) = − ( 1 2 log ⁡ 2 1 2 + 1 2 log ⁡ 2 1 2 ) = 1.0 Ent ⁡ ( D 3 ) = − ( 1 2 log ⁡ 2 1 2 + 1 2 log ⁡ 2 1 2 ) = 1.0 \begin{array}{l} \operatorname{Ent}\left(\mathrm{D}_{1}\right)=-\left(\frac{1}{3} \log _{2} \frac{1}{3}+\frac{2}{3} \log _{2} \frac{2}{3}\right)=0.918 \\ \operatorname{Ent}\left(\mathrm{D}_{2}\right)=-\left(\frac{1}{2} \log _{2} \frac{1}{2}+\frac{1}{2} \log _{2} \frac{1}{2}\right)=1.0 \\ \operatorname{Ent}\left(\mathrm{D}_{3}\right)=-\left(\frac{1}{2} \log _{2} \frac{1}{2}+\frac{1}{2} \log _{2} \frac{1}{2}\right)=1.0 \end{array} Ent(D1)=(31log231+32log232)=0.918Ent(D2)=(21log221+21log221)=1.0Ent(D3)=(21log221+21log221)=1.0



    D 1 D_1 D1 D D D(父节点)中的概率是 3 / 7 3/7 3/7 D 2 D_2 D2 在父节点的概率是 2 / 7 2/7 2/7 D 3 D_3 D3 在父节点的概率是 2 / 7 2/7 2/7




那么天气作为属性节点的信息增益为
Gain ⁡ ( D ,  天气  ) = 0.985 − ( 3 / 7 ∗ 0.918 + 2 / 7 ∗ 1.0 + 2 / 7 ∗ 1.0 ) = 0.020. \operatorname{Gain}(\mathrm{D}, \text { 天气 })=0.985-(3/7*0.918+2/7*1.0+2/7*1.0)=0.020 . Gain(D, 天气 )=0.985(3/70.918+2/71.0+2/71.0)=0.020.

同理我们可以计算出其他属性作为根节点的信息增益,它们分别为 :

Gain ⁡ ( D ,  温度  ) = 0.128 \operatorname{Gain}(\mathrm{D}, \text { 温度 }) = 0.128 Gain(D, 温度 )=0.128



Gain ⁡ ( D ,  湿度  ) = 0.020 \operatorname{Gain}(\mathrm{D}, \text { 湿度 }) = 0.020 Gain(D, 湿度 )=0.020



Gain ⁡ ( D ,  刮风  ) = 0.020 \operatorname{Gain}(\mathrm{D}, \text { 刮风 }) = 0.020 Gain(D, 刮风 )=0.020


我们能看出来温度作为属性的信息增益最大。因为 ID3 就是要将信息增益最大的节点作为父节点,这样可以得到纯度高的决策树,所以我们将温度作为根节点

在这里插入图片描述

然后我们要将上图中第一个叶节点,也就是 D 1 = 1 − , 2 − , 3 + , 4 + D_1={1-,2-,3+,4+} D1=1,2,3+,4+进一步进行分裂,往下划分,计算其不同属性(天气、湿度、刮风)作为节点的信息增益
Gain ⁡ ( D ,  温度  ) = 1 \operatorname{Gain}(\mathrm{D}, \text { 温度 }) = 1 Gain(D, 温度 )=1



Gain ⁡ ( D ,  天气  ) = 1 \operatorname{Gain}(\mathrm{D}, \text { 天气 }) = 1 Gain(D, 天气 )=1



Gain ⁡ ( D ,  刮风  ) = 0.3115 \operatorname{Gain}(\mathrm{D}, \text { 刮风 }) = 0.3115 Gain(D, 刮风 )=0.3115


我们能看到湿度,或者天气为 D 1 D_1 D1的节点都可以得到最大的信息增益,这里我们选取湿度作为节点的属性划分。

最终结果

在这里插入图片描述

ID3算法缺陷

  • ID3 构造决策树的时候,由于考虑较多的训练集特征,容易产生过拟合的情况
  • ID3 算法倾向于选择取值比较多的属性, 此时,如果存在一个唯一标识,这样样本集D将会被划分为|D| 个分支,每个分支只有一个样本,这样划分后的信息熵为零,十分纯净。这种缺陷不是每次都会发生,只是存在一定的概率。
  • ID3 算法无法处理缺失值和连续值

C4.5 算法

为避免ID3算法对于取值数目较多的属性倾向,C4.5算法采用信息增益率来表示不纯度

计算公式为:
 GainRatio ( D , a ) = Gain ⁡ ( D , a ) IV ⁡ ( a ) \text { GainRatio}(D, a)=\frac{\operatorname{Gain}(D, a)}{\operatorname{IV}(a)}  GainRatio(D,a)=IV(a)Gain(D,a)




其中
I V ( a ) = − ∑ v = 1 V ∣ D v ∣ ∣ D ∣ log ⁡ 2 ∣ D v ∣ ∣ D ∣ \mathrm{IV}(a)=-\sum_{v=1}^{V} \frac{\left|D^{v}\right|}{|D|} \log _{2} \frac{\left|D^{v}\right|}{|D|} IV(a)=v=1VDDvlog2DDv



α α α属性的取值越多时 I V ( α ) 值 越 大 IV(α)值越大 IV(α)

示例

在这里插入图片描述

我们不考虑缺失的数值,可以得到温度 D = 2 − , 3 + , 4 + , 5 − , 6 + , 7 − D={2-,3+,4+,5-,6+,7-} D=2,3+,4+,5,6+,7

  • 温度 = 高:D1={2-,3+,4+}
  • 温度 = 中:D2={6+,7-}
  • 温度 = 低:D3={5-}

则属性为温度的信息增益:
Gain ⁡ ( D ′ ,  温度  ) = Ent ⁡ ( D ′ ) − 0.792 = 1.0 − 0.792 = 0.208 \operatorname{Gain}\left(\mathrm{D}^{\prime}, \text { 温度 }\right)=\operatorname{Ent}\left(\mathrm{D}^{\prime}\right)-0.792=1.0-0.792=0.208 Gain(D, 温度 )=Ent(D)0.792=1.00.792=0.208



属性熵为:
Ent ⁡ ( D ) = − ∑ k = 1 2 p k log ⁡ 2 p k = 1.459 \operatorname{Ent}(D)=-\sum_{k=1}^{2} p_{k} \log _{2} p_{k}=1.459 Ent(D)=k=12pklog2pk=1.459



信息增益率为  GainRatio(D’, 温度  ) = 0.208 / 1.459 = 0.1426 \text { GainRatio(D', 温度 })=0.208 / 1.459=0.1426  GainRatio(D’, 温度 )=0.208/1.459=0.1426



D′的样本个数为 6,而 D 的样本个数为 7,所以所占权重比例为 6/7,所以 Gain(D′,温度) 所占权重比例为 6/7,所以
 GainRatio(D, 温度  ) = 6 / 7 ⋆ 0.1426 = 0.12 2 ∘ \text { GainRatio(D, 温度 })=6 / 7^{\star} 0.1426=0.122_{\circ}  GainRatio(D, 温度 )=6/70.1426=0.122

连续值与缺失值处理

  1. 连续值

对于连续值的属性,若每个取值作为一个分支则显得不可行,因此需要进行离散化处理,常用的方法为二分法,基本思想为:给定样本集D与连续属性α,二分法试图找到一个划分点t将样本集D在属性α上分为≤t与>t。

* 首先将α的所有取值按升序排列,所有相邻属性的均值作为候选划分点(n-1个,n为α所有的取值数目)。
* 计算每一个划分点划分集合D(即划分为两个分支)后的信息增益。
* 选择最大信息增益的划分点作为最优划分点。

Gain ⁡ ( D , a ) = max ⁡ t ∈ T a Gain ⁡ ( D , a , t ) = max ⁡ t ∈ T a Ent ⁡ ( D ) − ∑ λ ∈ { − , + } ∣ D t λ ∣ ∣ D ∣ Ent ⁡ ( D t λ ) \begin{aligned} \operatorname{Gain}(D, a) &=\max _{t \in T_{a}} \operatorname{Gain}(D, a, t) \\ &=\max _{t \in T_{a}} \operatorname{Ent}(D)-\sum_{\lambda \in\{-,+\}} \frac{\left|D_{t}^{\lambda}\right|}{|D|} \operatorname{Ent}\left(D_{t}^{\lambda}\right) \end{aligned} Gain(D,a)=tTamaxGain(D,a,t)=tTamaxEnt(D)λ{,+}DDtλEnt(Dtλ)

  1. 缺失值

在属性值缺失 的情况下需要解决两个问题:(1)如何选择划分属性。(2)给定划分属性,若某样本在该属性上缺失值,如何划分到具 体的分支上

对于(1):通过在样本集D中选取在属性α上没有缺失值的样本子集,计算在该样本子集上的信息增益,最终的信息增益
等于该样本子集划分后信息增益乘以样本子集占样本集的比重

在这里插入图片描述

对于(2):若该样本子集在属性α上的值缺失,则将该样本以不同的权重(即每个分支所含样本比例)划入到所有分支节
点中。该样本在分支节点中的权重变为

在这里插入图片描述

Cart算法

实际上 CART 分类树与 C4.5 算法类似,只是属性选择的指标采用的是基尼系数。

基尼系数,它是用来衡量一个国家收入差距的常用指标。当基尼系数大于 0.4 的时候,说明财富差异悬殊。基尼系数在 0.2-0.4 之间说明分配合理,财富差距不大。

假设 t 为节点,那么该节点的 GINI 系数的计算公式为:
G I N I ( t ) = 1 − ∑ k [ p ( C k ∣ t ) ] 2 GINI(t)=1-\sum_{k}\left[p\left(C_{k} \mid t\right)\right]^{2} GINI(t)=1k[p(Ckt)]2


这里 p ( C k ∣ t ) p(Ck|t) p(Ckt)表示节点 t t t属于类别 C k C_k Ck的概率,节点 t t t的基尼系数为 1 减去各类别 C k C_k Ck概率平方和。


示例

  • 集合 1:6 个都去打篮球;

  • 集合 2:3 个去打篮球,3 个不去打篮球。

针对集合 1,所有人都去打篮球,所以 p ( C k ∣ t ) = 1 p(Ck|t)=1 p(Ckt)=1,因此 G I N I ( t ) = 1 − 1 = 0 GINI(t)=1-1=0 GINI(t)=11=0

针对集合 2,有一半人去打篮球,而另一半不去打篮球,所以 p ( C 1 ∣ t ) = 0.5 p(C1|t)=0.5 p(C1t)=0.5 p ( C 2 ∣ t ) = 0.5 p(C2|t)=0.5 p(C2t)=0.5 G I N I ( t ) = 1 − ( 0.5 ∗ 0.5 + 0.5 ∗ 0.5 ) = 0.5 GINI(t)=1-(0.5*0.5+0.5*0.5)=0.5 GINI(t)=10.50.5+0.50.5=0.5

通过两个基尼系数你可以看出,集合 1 的基尼系数最小,也证明样本最稳定,而集合 2 的样本不稳定性更大。

在 CART 算法中,基于基尼系数对特征属性进行二元分裂,假设属性 A 将节点 D 划分成了 D1 和 D2,如下图所示:

在这里插入图片描述

节点 D 的基尼系数等于子节点 D1 和 D2 的归一化基尼系数之和,用公式表示为

G I N I ( D , A ) = D 1 D G I N I ( D 1 ) + D 2 D G I N I ( D 2 ) GINI(D, A)=\frac{D_{1}}{D} G I N I\left(D_{1}\right)+\frac{D_{2}}{D} GINI\left(D_{2}\right) GINI(D,A)=DD1GINI(D1)+DD2GINI(D2)

归一化基尼系数代表的是每个子节点的基尼系数乘以该节点占整体父亲节点 D 中的比例

集合 D 1 D_1 D1和集合 D 2 D_2 D2的GINI系数
G I N I ( D 1 ) = 0 G I N I ( D 2 ) = 0.5 \begin{aligned} GINI\left(D_{1}\right) &=0 \\ GINI\left(D_{2}\right) &=0.5 \end{aligned} GINI(D1)GINI(D2)=0=0.5

所以在属性 A 的划分下,节点 D 的基尼系数:

G I N I ( D , A ) = 6 12 G I N I ( D 1 ) + 6 12 G I N I ( D 2 ) = 0.25 GINI(D, A)=\frac{6}{12} G I N I\left(D_{1}\right)+\frac{6}{12} GINI\left(D_{2}\right)=0.25 GINI(D,A)=126GINI(D1)+126GINI(D2)=0.25

ID3算法Python实现(基于信贷数据集)

import pandas as pd

pd.read_csv("loan.csv").set_index("ID")
年龄有工作有自己房子信贷情况类别(是否个给贷款)
ID
1青年一般
2青年
3青年
4青年一般
5青年一般
6中年一般
7中年
8中年
9中年非常好
10中年非常好
11老年非常好
12老年
13老年
14老年非常好
15老年一般
dataSet = [[0, 0, 0, 0, 'no'],  #数据集
            [0, 0, 0, 1, 'no'],
            [0, 1, 0, 1, 'yes'],
            [0, 1, 1, 0, 'yes'],
            [0, 0, 0, 0, 'no'],
            [1, 0, 0, 0, 'no'],
            [1, 0, 0, 1, 'no'],
            [1, 1, 1, 1, 'yes'],
            [1, 0, 1, 2, 'yes'],
            [1, 0, 1, 2, 'yes'],
            [2, 0, 1, 2, 'yes'],
            [2, 0, 1, 1, 'yes'],
            [2, 1, 0, 1, 'yes'],
            [2, 1, 0, 2, 'yes'],
            [2, 0, 0, 0, 'no']]
dataSet
[[0, 0, 0, 0, 'no'],
 [0, 0, 0, 1, 'no'],
 [0, 1, 0, 1, 'yes'],
 [0, 1, 1, 0, 'yes'],
 [0, 0, 0, 0, 'no'],
 [1, 0, 0, 0, 'no'],
 [1, 0, 0, 1, 'no'],
 [1, 1, 1, 1, 'yes'],
 [1, 0, 1, 2, 'yes'],
 [1, 0, 1, 2, 'yes'],
 [2, 0, 1, 2, 'yes'],
 [2, 0, 1, 1, 'yes'],
 [2, 1, 0, 1, 'yes'],
 [2, 1, 0, 2, 'yes'],
 [2, 0, 0, 0, 'no']]
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
from math import log
import operator
import pickle
plt.rcParams['font.sans-serif']=['SimHei'] #显示中文标签
plt.rcParams['axes.unicode_minus']=False
"""
函数说明:创建测试数据集
 
Parameters:
    无
Returns:
    dataSet - 数据集
    labels - 特征标签
"""

def createDataSet():
    dataSet = [[0, 0, 0, 0, 'no'],  #数据集
            [0, 0, 0, 1, 'no'],
            [0, 1, 0, 1, 'yes'],
            [0, 1, 1, 0, 'yes'],
            [0, 0, 0, 0, 'no'],
            [1, 0, 0, 0, 'no'],
            [1, 0, 0, 1, 'no'],
            [1, 1, 1, 1, 'yes'],
            [1, 0, 1, 2, 'yes'],
            [1, 0, 1, 2, 'yes'],
            [2, 0, 1, 2, 'yes'],
            [2, 0, 1, 1, 'yes'],
            [2, 1, 0, 1, 'yes'],
            [2, 1, 0, 2, 'yes'],
            [2, 0, 0, 0, 'no']]
    labels = ['年龄', '有工作', '有自己的房子', '信贷情况']  #特征标签
    return dataSet, labels     #返回数据集和分类属性

"""
函数说明:计算给定数据集的经验熵(香农熵)
 
Parameters:
    dataSet - 数据集
Returns:
    shannonEnt - 经验熵(香农熵)
"""

def calcShannonEnt(dataSet):
    numEntires = len(dataSet)                   #返回数据集的行数
    labelCounts = {}                   #保存每个标签(Label)出现次数的字典
    for featVec in dataSet:                   #对每组特征向量进行统计
        currentLabel = featVec[-1]                   #提取标签(Label)信息
        if currentLabel not in labelCounts.keys():    #如果标签(Label)没有放入统计次数的字典,添加进去
            labelCounts[currentLabel] = 0
        labelCounts[currentLabel] += 1                   #Label计数
    shannonEnt = 0.0                   #经验熵(香农熵)
    for key in labelCounts:                   #计算香农熵
        prob = float(labelCounts[key]) / numEntires                   #选择该标签(Label)的概率
        shannonEnt -= prob * log(prob, 2)                   #利用公式计算
    return shannonEnt        #返回经验熵(香农熵)

"""
函数说明:按照给定特征划分数据集
 
Parameters:
    dataSet - 待划分的数据集
    axis - 划分数据集的特征
    value - 需要返回的特征的值
Returns:
    无
"""

def splitDataSet(dataSet, axis, value):
    retDataSet = []  #创建返回的数据集列表
    for featVec in dataSet:   #遍历数据集
        if featVec[axis] == value:
            reducedFeatVec = featVec[:axis]  #去掉axis特征
            reducedFeatVec.extend(featVec[axis+1:])   #将符合条件的添加到返回的数据集
            retDataSet.append(reducedFeatVec)
    return retDataSet   #返回划分后的数据集

"""
函数说明:选择最优特征
 
Parameters:
    dataSet - 数据集
Returns:
    bestFeature - 信息增益最大的(最优)特征的索引值 
"""

def chooseBestFeatureToSplit(dataSet):
    numFeatures = len(dataSet[0]) - 1  #特征数量
    baseEntropy = calcShannonEnt(dataSet)   #计算数据集的香农熵
    bestInfoGain = 0.0    #信息增益
    bestFeature = -1  #最优特征的索引值
    for i in range(numFeatures):   #遍历所有特征
        #获取dataSet的第i个所有特征
        featList = [example[i] for example in dataSet]
        uniqueVals = set(featList)       #创建set集合{},元素不可重复
        newEntropy = 0.0     #经验条件熵
        for value in uniqueVals:   #计算信息增益
            subDataSet = splitDataSet(dataSet, i, value)   #subDataSet划分后的子集
            prob = len(subDataSet) / float(len(dataSet))     #计算子集的概率
            newEntropy += prob * calcShannonEnt(subDataSet)   #根据公式计算经验条件熵
        infoGain = baseEntropy - newEntropy   #信息增益
        # print("第%d个特征的增益为%.3f" % (i, infoGain))  #打印每个特征的信息增益
        if (infoGain > bestInfoGain):     #计算信息增益
            bestInfoGain = infoGain   #更新信息增益,找到最大的信息增益
            bestFeature = i   #记录信息增益最大的特征的索引值
    return bestFeature   #返回信息增益最大的特征的索引值


"""
函数说明:统计classList中出现此处最多的元素(类标签)
 
Parameters:
    classList - 类标签列表
Returns:
    sortedClassCount[0][0] - 出现此处最多的元素(类标签)
"""

def majorityCnt(classList):
    classCount = {}
    for vote in classList:   #统计classList中每个元素出现的次数
        if vote not in classCount.keys():classCount[vote] = 0
        classCount[vote] += 1
    sortedClassCount = sorted(classCount.items(), key = operator.itemgetter(1), reverse = True)  #根据字典的值降序排序
    return sortedClassCount[0][0]   #返回classList中出现次数最多的元素

"""
函数说明:创建决策树
 
Parameters:
    dataSet - 训练数据集
    labels - 分类属性标签
    featLabels - 存储选择的最优特征标签
Returns:
    myTree - 决策树
"""

def createTree(dataSet, labels, featLabels):
    classList = [example[-1] for example in dataSet]  #取分类标签(是否放贷:yes or no)
    if classList.count(classList[0]) == len(classList):#如果类别完全相同则停止继续划分
        return classList[0]
    if len(dataSet[0]) == 1 or len(labels) == 0:#遍历完所有特征时返回出现次数最多的类标签
        return majorityCnt(classList)
    bestFeat = chooseBestFeatureToSplit(dataSet)#选择最优特征
    bestFeatLabel = labels[bestFeat]#最优特征的标签
    featLabels.append(bestFeatLabel)
    myTree = {bestFeatLabel:{}}#根据最优特征的标签生成树
    del(labels[bestFeat])#删除已经使用特征标签
    featValues = [example[bestFeat] for example in dataSet]#得到训练集中所有最优特征的属性值
    uniqueVals = set(featValues)#去掉重复的属性值
    for value in uniqueVals:#遍历特征,创建决策树。
        subLabels = labels[:]
        myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value), subLabels, featLabels)
        
    return myTree

"""
函数说明:获取决策树叶子结点的数目
 
Parameters:
    myTree - 决策树
Returns:
    numLeafs - 决策树的叶子结点的数目
"""

def getNumLeafs(myTree):
    numLeafs = 0#初始化叶子
    firstStr = next(iter(myTree))#python3中myTree.keys()返回的是dict_keys,不在是list,所以不能使用myTree.keys()[0]的方法获取结点属性,可以使用list(myTree.keys())[0]
    secondDict = myTree[firstStr]#获取下一组字典
    for key in secondDict.keys():
        if type(secondDict[key]).__name__=='dict':#测试该结点是否为字典,如果不是字典,代表此结点为叶子结点
            numLeafs += getNumLeafs(secondDict[key])
        else:   numLeafs +=1
    return numLeafs
"""
函数说明:获取决策树的层数
 
Parameters:
    myTree - 决策树
Returns:
    maxDepth - 决策树的层数
"""

def getTreeDepth(myTree):
    maxDepth = 0#初始化决策树深度
    firstStr = next(iter(myTree))#python3中myTree.keys()返回的是dict_keys,不在是list,所以不能使用myTree.keys()[0]的方法获取结点属性,可以使用list(myTree.keys())[0]
    secondDict = myTree[firstStr]#获取下一个字典
    for key in secondDict.keys():
        if type(secondDict[key]).__name__=='dict':#测试该结点是否为字典,如果不是字典,代表此结点为叶子结点
            thisDepth = 1 + getTreeDepth(secondDict[key])
        else:   thisDepth = 1
        if thisDepth > maxDepth: maxDepth = thisDepth#更新层数
    return maxDepth
"""
函数说明:对树进行塌陷处理(即返回树平均值)
Parameters:
    tree - 树
Returns:
    树的平均值
"""

def getMean(tree):
    if isTree(tree['right']): tree['right'] = getMean(tree['right'])
    if isTree(tree['left']): tree['left'] = getMean(tree['left'])
    return (tree['left'] + tree['right']) / 2.0
"""
函数说明:后剪枝
Parameters:
    tree - 树
    test - 测试集
Returns:
    树的平均值
"""

def prune(tree, testData):

    #如果测试集为空,则对树进行塌陷处理
    if np.shape(testData)[0] == 0: return getMean(tree)
    #如果有左子树或者右子树,则切分数据集
    if (isTree(tree['right']) or isTree(tree['left'])):
        lSet, rSet = binSplitDataSet(testData, tree['spInd'], tree['spVal'])
    #处理左子树(剪枝)
    if isTree(tree['left']): tree['left'] = prune(tree['left'], lSet)
    #处理右子树(剪枝)
    if isTree(tree['right']): tree['right'] =  prune(tree['right'], rSet)
    #如果当前结点的左右结点为叶结点
    if not isTree(tree['left']) and not isTree(tree['right']):
        lSet, rSet = binSplitDataSet(testData, tree['spInd'], tree['spVal'])
        #计算没有合并的误差
        errorNoMerge = np.sum(np.power(lSet[:,-1] - tree['left'],2)) + np.sum(np.power(rSet[:,-1] - tree['right'],2))
        #计算合并的均值
        treeMean = (tree['left'] + tree['right']) / 2.0
        #计算合并的误差
        errorMerge = np.sum(np.power(testData[:,-1] - treeMean, 2))
        #如果合并的误差小于没有合并的误差,则合并
        if errorMerge < errorNoMerge: 
            # print("merging")
            return treeMean
        else: return tree
    else: return tree
"""
函数说明:绘制结点
 
Parameters:
    nodeTxt - 结点名
    centerPt - 文本位置
    parentPt - 标注的箭头位置
    nodeType - 结点格式
Returns:
    无
"""

def plotNode(nodeTxt, centerPt, parentPt, nodeType):
    arrow_args = dict(arrowstyle="<-")#定义箭头格式
    font = FontProperties(fname=r"c:\windows\fonts\simhei.ttf", size=14)#设置中文字体
    createPlot.ax1.annotate(nodeTxt, xy=parentPt,  xycoords='axes fraction',#绘制结点
        xytext=centerPt, textcoords='axes fraction',
        va="center", ha="center", bbox=nodeType, arrowprops=arrow_args, FontProperties=font)

"""
函数说明:标注有向边属性值
 
Parameters:
    cntrPt、parentPt - 用于计算标注位置
    txtString - 标注的内容
Returns:
    无
"""

def plotMidText(cntrPt, parentPt, txtString):
    xMid = (parentPt[0]-cntrPt[0])/2.0 + cntrPt[0]    #计算标注位置
    yMid = (parentPt[1]-cntrPt[1])/2.0 + cntrPt[1]
    createPlot.ax1.text(xMid, yMid, txtString, va="center", ha="center", rotation=30)

"""
函数说明:绘制决策树
 
Parameters:
    myTree - 决策树(字典)
    parentPt - 标注的内容
    nodeTxt - 结点名
Returns:
    无
"""

def plotTree(myTree, parentPt, nodeTxt):
    decisionNode = dict(boxstyle="sawtooth", fc="0.8")#设置结点格式
    leafNode = dict(boxstyle="round4", fc="0.8")#设置叶结点格式
    numLeafs = getNumLeafs(myTree)  #获取决策树叶结点数目,决定了树的宽度
    depth = getTreeDepth(myTree)#获取决策树层数
    firstStr = next(iter(myTree))#下个字典   
    cntrPt = (plotTree.xOff + (1.0 + float(numLeafs))/2.0/plotTree.totalW, plotTree.yOff)#中心位置
    plotMidText(cntrPt, parentPt, nodeTxt)#标注有向边属性值
    plotNode(firstStr, cntrPt, parentPt, decisionNode) #绘制结点
    secondDict = myTree[firstStr]  #下一个字典,也就是继续绘制子结点
    plotTree.yOff = plotTree.yOff - 1.0/plotTree.totalD#y偏移
    for key in secondDict.keys():
        if type(secondDict[key]).__name__=='dict':#测试该结点是否为字典,如果不是字典,代表此结点为叶子结点
            plotTree(secondDict[key],cntrPt,str(key))#不是叶结点,递归调用继续绘制
        else:#如果是叶结点,绘制叶结点,并标注有向边属性值 
            plotTree.xOff = plotTree.xOff + 1.0/plotTree.totalW
            plotNode(secondDict[key], (plotTree.xOff, plotTree.yOff), cntrPt, leafNode)
            plotMidText((plotTree.xOff, plotTree.yOff), cntrPt, str(key))
    plotTree.yOff = plotTree.yOff + 1.0/plotTree.totalD

"""
函数说明:创建绘制面板
 
Parameters:
    inTree - 决策树(字典)
Returns:
    无
"""

def createPlot(inTree):
    fig = plt.figure(1, facecolor='white')  #创建fig
    fig.clf()  #清空fig
    axprops = dict(xticks=[], yticks=[])
    createPlot.ax1 = plt.subplot(111, frameon=False, **axprops)   #去掉x、y轴
    plotTree.totalW = float(getNumLeafs(inTree))  #获取决策树叶结点数目
    plotTree.totalD = float(getTreeDepth(inTree))  #获取决策树层数
    plotTree.xOff = -0.5/plotTree.totalW; plotTree.yOff = 1.0;#x偏移
    plotTree(inTree, (0.5,1.0), '')   #绘制决策树
    plt.show()  #显示绘制结果

"""
函数说明:使用决策树分类
 
Parameters:
    inputTree - 已经生成的决策树
    featLabels - 存储选择的最优特征标签
    testVec - 测试数据列表,顺序对应最优特征标签
Returns:
    classLabel - 分类结果
"""

def classify(inputTree, featLabels, testVec):
    firstStr = next(iter(inputTree))#获取决策树结点
    secondDict = inputTree[firstStr]#下一个字典
    featIndex = featLabels.index(firstStr)
    for key in secondDict.keys():
        if testVec[featIndex] == key:
            if type(secondDict[key]).__name__ == 'dict':
                classLabel = classify(secondDict[key], featLabels, testVec)
            else: classLabel = secondDict[key]
    return classLabel

"""
函数说明:存储决策树
 
Parameters:
    inputTree - 已经生成的决策树
    filename - 决策树的存储文件名
Returns:
    无
"""

def storeTree(inputTree, filename):
    with open(filename, 'wb') as fw:
        pickle.dump(inputTree, fw)
"""
函数说明:读取决策树
 
Parameters:
    filename - 决策树的存储文件名
Returns:
    pickle.load(fr) - 决策树字典
"""

def grabTree(filename):
    fr = open(filename, 'rb')
    return pickle.load(fr)

if __name__ == '__main__':
    dataSet, labels = createDataSet()
    featLabels = []
    myTree = createTree(dataSet, labels, featLabels)
    createPlot(myTree)
    testVec = [0,1]        #测试数据
    result = classify(myTree, featLabels, testVec)
    if result == 'yes':
        print('放贷')
    if result == 'no':
        print('不放贷')

在这里插入图片描述

放贷

基于Sklearn库的决策树模型(Titanic乘客生存预测)

DecisionTreeClassifier(class_weight=None, criterion='entropy', max_depth=None, max_features=None, max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, presort=False, random_state=None, splitter='best')
DecisionTreeClassifier(ccp_alpha=0.0, class_weight=None, criterion='entropy',
                       max_depth=None, max_features=None, max_leaf_nodes=None,
                       min_impurity_decrease=0.0, min_impurity_split=None,
                       min_samples_leaf=1, min_samples_split=2,
                       min_weight_fraction_leaf=0.0, presort=False,
                       random_state=None, splitter='best')

img

import math
import graphviz
import pandas as pd
from sklearn.feature_extraction import DictVectorizer
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.model_selection import cross_val_score

# 数据加载
train_data = pd.read_csv('./train.csv')
test_data = pd.read_csv('./test.csv')
# 数据探索
train_data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
 #   Column       Non-Null Count  Dtype  
---  ------       --------------  -----  
 0   PassengerId  891 non-null    int64  
 1   Survived     891 non-null    int64  
 2   Pclass       891 non-null    int64  
 3   Name         891 non-null    object 
 4   Sex          891 non-null    object 
 5   Age          714 non-null    float64
 6   SibSp        891 non-null    int64  
 7   Parch        891 non-null    int64  
 8   Ticket       891 non-null    object 
 9   Fare         891 non-null    float64
 10  Cabin        204 non-null    object 
 11  Embarked     889 non-null    object 
dtypes: float64(2), int64(5), object(5)
memory usage: 83.7+ KB
train_data.describe()
PassengerIdSurvivedPclassAgeSibSpParchFare
count891.000000891.000000891.000000714.000000891.000000891.000000891.000000
mean446.0000000.3838382.30864229.6991180.5230080.38159432.204208
std257.3538420.4865920.83607114.5264971.1027430.80605749.693429
min1.0000000.0000001.0000000.4200000.0000000.0000000.000000
25%223.5000000.0000002.00000020.1250000.0000000.0000007.910400
50%446.0000000.0000003.00000028.0000000.0000000.00000014.454200
75%668.5000001.0000003.00000038.0000001.0000000.00000031.000000
max891.0000001.0000003.00000080.0000008.0000006.000000512.329200
train_data.head()
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarked
0103Braund, Mr. Owen Harrismale22.010A/5 211717.2500NaNS
1211Cumings, Mrs. John Bradley (Florence Briggs Th...female38.010PC 1759971.2833C85C
2313Heikkinen, Miss. Lainafemale26.000STON/O2. 31012827.9250NaNS
3411Futrelle, Mrs. Jacques Heath (Lily May Peel)female35.01011380353.1000C123S
4503Allen, Mr. William Henrymale35.0003734508.0500NaNS
# 数据清洗
# 使用平均年龄来填充年龄中的 nan 值
train_data['Age'].fillna(train_data['Age'].mean(), inplace=True)
test_data['Age'].fillna(test_data['Age'].mean(),inplace=True)
# 使用票价的均值填充票价中的 nan 值
train_data['Fare'].fillna(train_data['Fare'].mean(), inplace=True)
test_data['Fare'].fillna(test_data['Fare'].mean(),inplace=True)
print(train_data['Embarked'].value_counts())

# 使用登录最多的港口来填充登录港口的 nan 值
train_data['Embarked'].fillna('S', inplace=True)
test_data['Embarked'].fillna('S',inplace=True)

# 特征选择
features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']
train_features = train_data[features]
train_labels = train_data['Survived']
test_features = test_data[features]

dvec = DictVectorizer(sparse=False)
train_features = dvec.fit_transform(train_features.to_dict(orient='record'))
print(dvec.feature_names_)


S    644
C    168
Q     77
Name: Embarked, dtype: int64
['Age', 'Embarked=C', 'Embarked=Q', 'Embarked=S', 'Fare', 'Parch', 'Pclass', 'Sex=female', 'Sex=male', 'SibSp']
# 构造 ID3 决策树
clf = DecisionTreeClassifier(criterion='entropy')
# 决策树训练
clf.fit(train_features, train_labels)

test_features=dvec.transform(test_features.to_dict(orient='record'))
# 决策树预测
pred_labels = clf.predict(test_features)

# 得到决策树准确率
acc_decision_tree = round(clf.score(train_features, train_labels), 6)
print(u'score 准确率为 %.4lf' % acc_decision_tree)
score 准确率为 0.9820
dot_data = export_graphviz(clf, out_file=None)
graph = graphviz.Source(dot_data)
graph.view()

基于cart算法的分类树


# encoding=utf-8
import math
import graphviz
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.datasets import load_iris
# 准备数据集
iris=load_iris()
# 获取特征集和分类标识
features = iris.data
labels = iris.target
# 随机抽取33%的数据作为测试集,其余为训练集
train_features, test_features, train_labels, test_labels = train_test_split(features, labels, test_size=0.33, random_state=0)
# 创建CART分类树
clf = DecisionTreeClassifier(criterion='gini')
# 拟合构造CART分类树
clf = clf.fit(train_features, train_labels)
# 用CART分类树做预测
test_predict = clf.predict(test_features)
# 预测结果与测试集结果作比对
score = accuracy_score(test_labels, test_predict)
print("CART分类树准确率 %.4lf" % score)
CART分类树准确率 0.9600
dot_data = export_graphviz(clf, out_file=None)
graph = graphviz.Source(dot_data)
graph.view()

在这里插入图片描述

基于cart算法的回归树


# encoding=utf-8
import math
import graphviz
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_boston
from sklearn.metrics import r2_score,mean_absolute_error,mean_squared_error
from sklearn.tree import DecisionTreeRegressor, export_graphviz
# 准备数据集
boston=load_boston()
# 探索数据
print(boston.feature_names)
# 获取特征集和房价
features = boston.data
prices = boston.target
# 随机抽取33%的数据作为测试集,其余为训练集
train_features, test_features, train_price, test_price = train_test_split(features, prices, test_size=0.33)
# 创建CART回归树
dtr=DecisionTreeRegressor()
# 拟合构造CART回归树
dtr.fit(train_features, train_price)
# 预测测试集中的房价
predict_price = dtr.predict(test_features)
# 测试集的结果评价
print('回归树二乘偏差均值:', mean_squared_error(test_price, predict_price))
print('回归树绝对值偏差均值:', mean_absolute_error(test_price, predict_price)) 
['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO'
 'B' 'LSTAT']
回归树二乘偏差均值: 22.601856287425147
回归树绝对值偏差均值: 3.4520958083832336
# 参数是回归树模型名称,不输出文件。
dot_data = export_graphviz(dtr, out_file=None)
graph = graphviz.Source(dot_data)
# render 方法会在同级目录下生成 Boston PDF文件,内容就是回归树。
graph.render('Boston')
graph.view()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值