作者:chen_h
微信号 & QQ:862251340
微信公众号:coderpai
决策树算法将原始数据转换为基于规则的决策树。这里 ID3 是最常见的决策树算法之一。首先,它于 1986 年推出,它是 Iterative Dichotomiser 的首字母缩写。
首先,二分法意味着,我们会把东西分成两个完全相反的东西。这就是为什么,算法迭代地将属性分为两组,这两组是最主要的属性,另一组是构造树。然后,它计算每个属性的熵和信息增益。通过这种方式,可以建立最主要的属性。在那之后,最主要的一个被放在树上作为决策节点。此后,将在其他属性中再次计算熵和增益分数。因此,找到了下一个最主要的属性。最后,这个过程一直持续到该分支的结束位置,这就是为什么它被称为 Iterative Dichotomiser。所以,我们将在这篇文章中逐步提到算法。
例如,下表通知了在过去 14 天内在外面打网球的决策因素。
Day | Outlook | Temp. | Humidity | Wind | Decision |
---|---|---|---|---|---|
1 | Sunny | Hot | High | Weak | No |
2 | Sunny | Hot | High | Strong | No |
3 | Overcast | Hot | High | Weak | Yes |
4 | Rain | Mild | High | Weak | Yes |
5 | Rain | Cool | Normal | Weak | Yes |
6 | Rain | Cool | Normal | Strong | No |
7 | Overcast | Cool | Normal | Strong | Yes |
8 | Sunny | Mild | High | Weak | No |
9 | Sunny | Cool | Normal | Weak | Yes |
10 | Rain | Mild | Normal | Weak | Yes |
11 | Sunny | Mild | Normal | Strong | Yes |
12 | Overcast | Mild | High | Strong | Yes |
13 | Overcast | Hot | Normal | Weak | Yes |
14 | Rain | Mild | High | Strong | No |
我们可以总结 ID3 算法,如下所示:
E n t r o p y ( S ) = ∑ − p ( x ) ∗ l o g 2 p ( x ) Entropy(S) = \sum - p(x) * log_2p(x) Entropy(S)=∑−p(x)∗log2p(x)
G a i n ( S , A ) = E n t r o p y ( S ) − ∑ [ p ( S ∣ A ) ∗ E n t r o p y ( S ∣ A ) ] Gain(S, A) = Entropy(S) - \sum[p(S|A)*Entropy(S|A)] Gain(S,A)=Entropy(S)−∑[p(S∣A)∗Entropy(S∣A)]
这些公式可能现在会让你大吃一惊,但是后续我们会让他变得更加简单。
熵(Entropy)
我们需要先计算熵,上面的表中决策列由 14 个实例组成,包括两个标签:yes 和 no。他们有 9 个 yes 和 5 个 no 组成。
E n t r o p y ( D e c i s i o n ) = − p ( y e s ) ∗ l o g 2 p ( y e s ) − p ( n o ) ∗ l o g 2 p ( n o ) = − 9 14 ∗ l o g 2 9 14 − 5 14 ∗ l o g 2 5 14 = 0.940 Entropy(Decision) = -p(yes) * log_2p(yes) - p(no)*log_2p(no) = -\frac{9}{14} * log_2\frac{9}{14}-\frac{5}{14}*log_2\frac{5}{14} = 0.940 Entropy(Decision)=−p(yes)∗log2p(yes)−p(no)∗log2p(no)=−149∗log2149−145∗log2145=0.940
现在,我们需要找到决策最主要的因素。
Wind 因素对决定的影响
G a i n ( D e c i s i o n , W i n d ) = E n t r o p y ( D e c i s i o n ) − ∑ [ p ( D e c i s i o n ∣ W i n d ) ∗ E n t r o p y ( D e c i s i o n ∣ W i n d ) ] Gain(Decision, Wind) = Entropy(Decision) - \sum[p(Decision|Wind) * Entropy(Decision|Wind)] Gain(Decision,Wind)=Entropy(Decision)−∑[p(Decision∣Wind)∗Entropy(Decision∣Wind)]
Wind 属性有两个标签:weak 和 strong。我们会将其反映在公式中。
G a i n ( D e c i s i o n , W i n d ) = E n t r o p y ( D e c i s i o n ) − [ p ( D e c i s i o n ∣ W i n d = w e a k ) ∗ E n t r o p y ( D e c i s i o n ∣ W i n d = w e a k ) ] − [ p ( D e c i s i o n ∣ W i n d = s t r o n g ) ∗ E n t r o p y ( D e c i s i o n ∣ W i n d = s t r o n g ) ] Gain(Decision, Wind) = Entropy(Decision) - [p(Decision|Wind=weak)*Entropy(Decision|Wind=weak)] - [p(Decision|Wind=strong) * Entropy(Decision|Wind=strong)] Gain(Decision,Wind)=Entropy(Decision)−[p(Decision∣Wind=weak)∗Entropy(Decision∣Wind=weak)]−[p(Decision∣Wind=strong)∗Entropy(Decision∣Wind=strong)]
现在,我们需要分别计算 (Decision|Wind=weak) 和 (Decision|Wind=strong) 。
weak wind因素决定
Day | Outlook | Temp. | Humidity | Wind | Decision |
---|---|---|---|---|---|
1 | Sunny | Hot | High | Weak | No |
3 | Overcast | Hot | High | Weak | Yes |
4 | Rain | Mild | High | Weak | Yes |
5 | Rain | Cool | Normal | Weak | Yes |
8 | Sunny | Mild | High | Weak | No |
9 | Sunny | Cool | Normal | Weak | Yes |
10 | Rain | Mild | Normal | Weak | Yes |
13 | Overcast | Hot | Normal | Weak | Yes |
weak 因子有 8 个例子。其中,2个是 no 的决定,6 个是 yes 的决定,如上图所示。
E n t r o p y ( D e c i s i o n ∣ W i n d = W e a k ) = − p ( n o ) ∗ l o g 2 p ( n o ) − p ( y e s ) ∗ l o g 2 p ( y e s ) = − 2 8 ∗ l o g 2 2 8 − 6 8 ∗ l o g 2 6 8 = 0.811 Entropy(Decision|Wind=Weak)=-p(no)*log_2p(no)-p(yes)*log_2p(yes) = -\frac{2}{8}*log_2\frac{2}{8} - \frac{6}{8}*log_2\frac{6}{8} = 0.811 Entropy(Decision∣Wind=Weak)=−p(no)∗log2p(no)−p(yes)∗log2p(yes)=−82∗log282−86∗log286=0.811
strong wind因素决定
Day | Outlook | Temp. | Humidity | Wind | Decision |
---|---|---|---|---|---|
2 | Sunny | Hot | High | Strong | No |
6 | Rain | Cool | Normal | Strong | No |
7 | Overcast | Cool | Normal | Strong | Yes |
11 | Sunny | Mild | Normal | Strong | Yes |
12 | Overcast | Mild | High | Strong | Yes |
14 | Rain | Mild | High | Strong | No |
这里有 6 个 strong 因子,Decision 也被分为了两部分:
E n t r o p y ( D e c i s i o n ∣ W i n d = s t r o n g ) = − p ( n o ) ∗ l o g 2 p ( n o ) − p ( y e s ) ∗ l o g 2 p ( y e s ) = − 3 6 ∗ l o g 2 3 6 − 3 6 ∗ l o g 2 36 = 1 Entropy(Decision | Wind = strong) = -p(no)*log_2p(no) - p(yes)*log_2p(yes) = -\frac{3}{6}*log_2\frac{3}{6} - \frac{3}{6}*log_2{3}{6} = 1 Entropy(Decision∣Wind=strong)=−p(no)∗log2p(no)−p(yes)∗log2p(yes)=−63∗log263−63∗log236=1
现在,我们可以回到 G a i n ( D e c i s i o n , W i n d ) Gain(Decision, Wind) Gain(Decision,Wind) 方式。
G a i n ( D e c i s i o n , W i n d ) = E n t r o p y ( D e c i s i o n ) − [ p ( D e c i s i o n ∣ W i n d = w e a k ) ∗ E n t r o p y ( D e c i s i o n ∣ W i n d = W e a k ) ] − [ p ( D e c i s i o n ∣ W i n d = s t r o n g ) ∗ E n t r o p y ( D e c i s i o n ∣ W i n d = S t r o n g ) ] = 0.940 − [ 8 14 ∗ 0.811 ] − [ 6 14 ∗ 1 ] = 0.048 Gain(Decision, Wind) = Entropy(Decision)-[p(Decision|Wind=weak)*Entropy(Decision|Wind=Weak)]-[p(Decision|Wind=strong)*Entropy(Decision|Wind=Strong)] = 0.940-[\frac{8}{14}*0.811]-[\frac{6}{14}*1]=0.048 Gain(Decision,Wind)=Entropy(Decision)−[p(Decision∣Wind=weak)∗Entropy(Decision∣Wind=Weak)]−[p(Decision∣Wind=strong)∗Entropy(Decision∣Wind=Strong)]=0.940−[148∗0.811]−[146∗1]=0.048
所以 wind 因子的计算结束了,现在,我们可以对其它列进行相同的计算,以找到最主要的因素。
另外因子如下计算
我们可以用相同的方法计算 Outlook,Temperature和Humidity 因子的信息增益。
G a i n ( D e c i s i o n , O u t l o o k ) = 0.246 Gain(Decision, Outlook) = 0.246 Gain(Decision,Outlook)=0.246
G a i n ( D e c i s i o n , T e m p e r a t u r e ) = 0.029 Gain(Decision, Temperature)=0.029 Gain(Decision,Temperature)=0.029
G a i n ( D e c i s i o n , H u m i d i t y ) = 0.151 Gain(Decision, Humidity)=0.151 Gain(Decision,Humidity)=0.151
正如我们所看到的,outlook 因子是拥有最高的分数。这也是为什么,outlook 因子会出现在树的根节点的原因。
现在,我们需要测试 outlook 因子的自定义子集的数据集。
overcast 因子对 outlook 的决策
基本上,如果 outlook = overcast,那么 decision 一直是 yes 。
Day | Outlook | Temp. | Humidity | Wind | Decision |
---|---|---|---|---|---|
3 | Overcast | Hot | High | Weak | Yes |
7 | Overcast | Cool | Normal | Strong | Yes |
12 | Overcast | Mild | High | Strong | Yes |
13 | Overcast | Hot | Normal | Weak | Yes |
sunny 因子作为 outlook 的决策
Day | Outlook | Temp. | Humidity | Wind | Decision |
---|---|---|---|---|---|
1 | Sunny | Hot | High | Weak | No |
2 | Sunny | Hot | High | Strong | No |
8 | Sunny | Mild | High | Weak | No |
9 | Sunny | Cool | Normal | Weak | Yes |
11 | Sunny | Mild | Normal | Strong | Yes |
这里有 5 个 outlook = sunny 的实例,decision = yes 的概率是2/5,decision=no 的概率是 3/5 。
E n t r o p y ( O u t l o o k = S u n n y ) = − 3 5 ∗ l o g 2 3 5 − 2 5 ∗ l o g 2 2 5 = 0.971 Entropy(Outlook=Sunny) = -\frac{3}{5}*log_{2}{\frac{3}{5}}-\frac{2}{5}*log_{2}{\frac{2}{5}} = 0.971 Entropy(Outlook=Sunny)=−53∗log253−52∗log252=0.971
E n t r o p y ( O u t l o o k = S u n n y ∣ T e m p . = H o t ) = − 0 − 2 2 ∗ l o g 2 2 2 = 0 Entropy(Outlook=Sunny | Temp. = Hot) = -0-\frac{2}{2}*log_{2}{\frac{2}{2}} = 0 Entropy(Outlook=Sunny∣Temp.=Hot)=−0−22∗log222=0
E n t r o p y ( O u t l o o k = S u n n y ∣ T e m p . = C o o l ) = 0 Entropy(Outlook=Sunny|Temp. = Cool)=0 Entropy(Outlook=Sunny∣Temp.=Cool)=0
E n t r o p y ( O u t l o o k = S u n n y ∣ T e m p . = M i l d ) = − 1 2 ∗ l o g 2 1 2 − 1 2 ∗ l o g 2 1 2 = 1 Entropy(Outlook=Sunny|Temp.=Mild)=-\frac{1}{2}*log_{2}{\frac{1}{2}}-\frac{1}{2}*log_{2}{\frac{1}{2}}=1 Entropy(Outlook=Sunny∣Temp.=Mild)=−21∗log221−21∗log221=1
G a i n ( O u t l o o k = S u n n y ∣ T e m p . ) = E n t r o p y ( O u t l o o k = S u n n y ) − [ p ( O u t l o o k = S u n n y ∣ T e m p . = H o t ) ∗ E n t r o p y ( O u t l o o k = S u n n y ∣ T e m p . = H o t ) + p ( O u t l o o k = S u n n y ∣ T e m p . = C o o l ) ∗ E n t r o p y ( O u t l o o k = S u n n y ∣ T e m p . = C o o l ) + p ( O u t l o o k = S u n n y ∣ T e m p . = M i l d ) ∗ E n t r o p y ( O u t l o o k = S u n n y ∣ T e m p . = M i l d ) = 0.571 Gain(Outlook=Sunny|Temp.) = Entropy(Outlook=Sunny)-[p(Outlook=Sunny|Temp.=Hot)*Entropy(Outlook=Sunny|Temp.=Hot)+p(Outlook=Sunny|Temp.=Cool)*Entropy(Outlook=Sunny|Temp.=Cool)+p(Outlook=Sunny|Temp.=Mild)*Entropy(Outlook=Sunny|Temp.=Mild)=0.571 Gain(Outlook=Sunny∣Temp.)=Entropy(Outlook=Sunny)−[p(Outlook=Sunny∣Temp.=Hot)∗Entropy(Outlook=Sunny∣Temp.=Hot)+p(Outlook=Sunny∣Temp.=Cool)∗Entropy(Outlook=Sunny∣Temp.=Cool)+p(Outlook=Sunny∣Temp.=Mild)∗Entropy(Outlook=Sunny∣Temp.=Mild)=0.571
同理,我们可以计算出:
G a i n ( O u t l o o k = S u n n y ∣ H u m i d i t y ) = 0.970 Gain(Outlook=Sunny|Humidity)=0.970 Gain(Outlook=Sunny∣Humidity)=0.970
G a i n ( O u t l o o k = S u n n y ∣ W i n d ) = 0.019 Gain(Outlook=Sunny|Wind)=0.019 Gain(Outlook=Sunny∣Wind)=0.019
现在,我们选择 humidity 作为决策因子,因为它拥有最高分,从表中我们可以看出,如果 humidity=High,那么我们一直不会去打网球。
Day | Outlook | Temp. | Humidity | Wind | Decision |
---|---|---|---|---|---|
1 | Sunny | Hot | High | Weak | No |
2 | Sunny | Hot | High | Strong | No |
8 | Sunny | Mild | High | Weak | No |
从另一方面说,如果 humidity=Normal,那么我们就一定会去打网球。
Day | Outlook | Temp. | Humidity | Wind | Decision |
---|---|---|---|---|---|
9 | Sunny | Cool | Normal | Weak | Yes |
11 | Sunny | Mild | Normal | Strong | Yes |
最后,这意味着,如果 outlook = Sunny,那么我们只需要检查 humidity 就行了。
rain 因子作为 outlook 的决策
Day | Outlook | Temp. | Humidity | Wind | Decision |
---|---|---|---|---|---|
4 | Rain | Mild | High | Weak | Yes |
5 | Rain | Cool | Normal | Weak | Yes |
6 | Rain | Cool | Normal | Strong | No |
10 | Rain | Mild | Normal | Weak | Yes |
14 | Rain | Mild | High | Strong | No |
G a i n ( O u t l o o k = R a i n ∣ T e m p . ) = ? ? ? Gain(Outlook=Rain|Temp.) = ??? Gain(Outlook=Rain∣Temp.)=???
G a i n ( O u t l o o k = R a i n ∣ H u m i d i t y ) = ? ? ? Gain(Outlook=Rain|Humidity)=??? Gain(Outlook=Rain∣Humidity)=???
G a i n ( O u t l o o k = R a i n ∣ W i n d ) = ? ? ? Gain(Outlook=Rain|Wind)=??? Gain(Outlook=Rain∣Wind)=???
大家可以自己计算一下,如果 outlook=rain,那么 wind 因子将会获得最高的分数,这也是为什么我们需要把 wind 因子作为第二层节点。
因此,如果wind=weak,outlook=rain,那么决策将永远是肯定的。
Day | Outlook | Temp. | Humidity | Wind | Decision |
---|---|---|---|---|---|
4 | Rain | Mild | High | Weak | Yes |
5 | Rain | Cool | Normal | Weak | Yes |
10 | Rain | Mild | Normal | Weak | Yes |
更重要的是,如果wind=strong,outlook=rain,那么决策将永远是否定的,即我不会出去打网球。
Day | Outlook | Temp. | Humidity | Wind | Decision |
---|---|---|---|---|---|
6 | Rain | Cool | Normal | Strong | No |
14 | Rain | Mild | High | Strong | No |
因此,决策树的构建已经结束了,我们可以使用下图来表示。
结论
因此,决策树算法将原始数据转换为基于规则的机制。在这篇文章中,我们提到了一种最常见的决策树算法,名为 ID3。他们可以直接使用特征属性来进行分类,而大多数常见的机器学习算法都是不能的。但是,需要在 ID3 中将数字特征转换为标量。尽管决策树算法功能强大,但是他们的训练时间很长。另一方面,他们倾向于过度拟合。