如何构建决策树,我需要选择什么属性来度量,这里我们需要通过计算才能更好的构建一棵决策树。
【有以下二分类问题训练样本】
outlook | temperat | humidity | windy | play |
sunny | hot | high | FALSE | no |
sunny | hot | high | TRUE | no |
overcast | hot | high | FALSE | yes |
rainy | mild | high | FALSE | yes |
rainy | cool | normal | FALSE | yes |
rainy | cool | normal | TRUE | no |
overcast | cool | normal | TRUE | yes |
sunny | mild | high | FALSE | no |
sunny | cool | normal | FALSE | yes |
rainy | mild | normal | FALSE | yes |
sunny | mild | normal | TRUE | yes |
overcast | mild | high | TRUE | yes |
overcast | hot | normal | FALSE | yes |
rainy | mild | high | TRUE | no |
然后我们将这个表格细分,如下所示
【信息熵的计算】
信息熵:表示信息的不确定度
从给出的数据我们可以看到,这个表格是通过天气、温度、湿度、有风为特征来判断是否去打球。
所以我们根据没有任何天气提示的情况下可以知道这一天打球大的概率为9/14,不打球的概率为5/14.
通过下面信息熵的公式(下面公式中:p(i|t) 代表了节点 t 为分类 i 的概率)
可以知道信息熵为:Entropy(D)=
【信息增溢的计算】
信息增益:信息增益指的就是划分可以带来纯度的提高,信息熵的下降。它的计算公式,是父亲节点的信息熵减去所有子节点的信息熵。
信息增溢的计算公式:
PS:公式中 D 是父亲节点,Di 是子节点,Gain(D,a)中的 a 作为 D 节点的属性选择。
下面我们来求基于outlook的划分来求Gain(outlook)
从图中我们知道outlook取值为sunny、overcast、rainy的概率分别为:5/14、4/14、5/14
outlook=sunny时,有2/5打球、3/5不打球,故entropy= 0.971
outlook= overcast时,有4个打球,概率为1,故entropy=0
outlook=rainy时,有3/5打球,2/5不打球,故entropy= 0.971
则Entropy(outlook)=5/14*0.971+4/14*0+5/14*0.971=0.693
所以信息增溢Gain(outlook)=Entropy(D)-Entropy(outlook)=0.940-0.693=0.247
同理,按照上面的步骤依次可以求得
Gain(temperature)=0.029 Gain(humidity)=0.152 Gain(windy)=0.048
根据上面的信息熵我们可以得出Gain(outlook) > Gain(humidity) > Gain(windy) > Gain(temperature)
从上面的计算结果中可以看出,信息熵越大,纯度越低。当集合中的所有样本均匀混合时,信息熵最大,纯度最低。
【基尼系数GINI的计算】
GINI 公式:
同样以上面的样本为例,计算步骤如下:
outlook取值为sunny、overcast、rainy的概率分别为:5/14、4/14、5/14
outlook=sunny时,有2/5打球、3/5不打球,故GINI=1-()=0.64
outlook= overcast时,有4个打球,概率为1,故GINI=0
outlook=rainy时,有3/5打球,2/5不打球,故GINI=1-()=0.64
则GINI(outlook)=5/14*0.64+4/14*0+5/14*0.64=0.457
temperature取值为hot、mild、cool的概率分别为:4/14、6/14、4/14
temperature=hot 时,有1/2打球,1/2不打球,故GINI=1-0.5
temperature=mild 时,有2/3打球,1/3不打球,故GINI=
temperature=cool 时,有3/4打球,1/4不打球,故
则GINI(temperature)=4/14*0.5+6/14*0.444+4/14*0.375=0.440
humidty取值为high和normal的概率分别为:1/2、1/2
humidty=high 时,有3/7打球,4/7不打球,故
humidty=normal时,有6/7打球,1/7不打球,故
则GINI(humidty)=1/2*0.500+1/2*0.245=0.373
windy取值为false和true的概率分别为:4/7,3/7
windy=false 时,有3/4打球,1/4不打球,故
windy=true 事,有1/2打球,1/2不打球,故
则GINI(windy)=1/2*0.500+1/2*0.375=0.434
【相关参考资料】
1.决策树:https://www.cnblogs.com/molieren/articles/10664954.html
2.机器学习经典算法(2)——决策树与随机森林:https://www.bilibili.com/video/BV1Ps411V7px?p=3
3.决策树零基础入门教程:https://www.bilibili.com/video/BV1T7411b7DG?from=search&seid=6628949932489280159
4.决策树(信息熵—GINI)计算习题:https://blog.csdn.net/wsp_1138886114/article/details/80955528