ID3 决策树算法
介绍
迭代二分法 3 或者 ID3 是一个用来产生决策树的算法, 关于 ID3 算法的细节在 这里. 那里有很多 ID3 算法的用法,特别在 机器学习领域. 在本文中, 我们将看到在 ID3 算法中使用的特性选择过程. 特性选择部分已经被分为与数据集有关的基础信息, 熵和信息增益已经讨论过了,一些示例已经用来展现如何使用示例数据计算熵和信息增益.
特性选择
特性选择是构造一个决策树的基本步骤. 这儿有两个术语熵和信息增益用来处理特性选择, 使用特性选择 ID3 算法,选择的特性将变为决策树上的一个节点等等. Bef在深入前需要介绍一些在特性选择过程中的术语,
Outlook | Temperature | Humidity | Wind | Play ball |
Sunny | Hot | High | Weak | No |
Sunny | Hot | High | Strong | No |
Overcast | Hot | High | Weak | Yes |
Rain | Mild | High | Weak | Yes |
Rain | Cool | Normal | Weak | Yes |
Rain | Cool | Normal | Strong | No |
Overcast | Cool | Normal | Strong | Yes |
Sunny | Mild | High | Weak | No |
Sunny | Cool | Normal | Weak | Yes |
Rain | Mild | Normal | Weak | Yes |
Sunny | Mild | Normal | Strong | Yes |
Overcast | Mild | High | Strong | Yes |
Overcast | Hot | Normal | Weak | Yes |
Rain | Mild | High | Strong | No |
Total | 14 |
属性
在上表中, Day, Outlook, Temperature, Humidity, Wind, Play ball的指示都作为特性,
类(C) 或者分类器
在这些特性中 Play ball 称之为类(C) 或者分类器. 因为基于 Outlook, Temperature, Humidity 和 Wind 我们必须决定我们是否可以开始球赛, 这就是为什么 Play ball 是一个做出决定的分类器.
集合(S)
所有表中的记录都称为集合 (S).
熵计算
熵在信息理论中是一种测量过程, 关于熵的细节在 这里. 在这里, 我们将看到如何计算给定数据集的熵. 这里用的测试数据是图 1 的数据,
Entropy(S) = ∑n=1-p(I)log2p(I)
p(I) 指的是属于类 I 的 S 的比例, 例如. 上表中我们有两种类 {No, Yes} with {5, 9} (这里 5 是 No 的总数 9 是 Yes 的总数, 集合大小是 S=14. 所以对于整个集合,整个 C 的 p(I) 是 No (5/14) Yes (9/14).
log2p(I), 指定整个 C 的 log2(5/14) 和 log2(9/14).
∑ 整个c 例如. 所有分类器的项的合计. 在这个案例中是 -p(No/S)log2p(No/S) 和 -p(Yes/S)log2p(Yes/S) 的合计 = -p(No/S)log2p(No/S) + -p(Yes/S)log2p(Yes/S)
所以,
Entropy(S) = ∑-p(I)log2p(I)
=) Entropy(S) = -p(No/S)log2p(No/S) + -p(Yes/S)log2p(Yes/S)
=) Entropy(S) = ((-(5/14)log2(5/14)) + (-(9/14)log2(9/14)) )
=) Entropy(S) = (-0.64285 x -0.63743) + (-0.35714 x -1.485427)
=) Entropy(S) = 0.40977 + 0.5305096 = 0.940
所以 S 的熵是 0.940
信息增益 G(S,A)
信息增益是选择一部分特性变为决策树上的一个决策节点的过程. 请看 这里 了解关于信息增益的细节.
信息增益是 G(S,A)
这里 S 是数据集中的数据的集合, A 是覆盖集合 S 的哪个增益特性将被计算. 所以如果 Gain(S, Wind) 那么它涉及 所有 S 的 Wind 的增益.
Gain(S, A) = Entropy(S) - ∑( ( |Sv|/|S| ) x Entropy(Sv) )
S 是全部记录的集合.
A 是将计算的增益特性.
v 是所有特性 A 的可能, 例如在本例中如果我们关心 Windy 特性,那么 v 的集合是 { Weak, Strong }.
Sv 是每个 v 元素的数字 例如 Sweak = 8 Sstrong = 6.
∑ 是 v 集合的所有项 ( ( |Sv|/|S| ) x Entropy(Sv) ) 的合计 例如. ( ( |Sweak|/|S| ) x Entropy(Sweak) ) + ( ( |Sstrong|/|S| ) x Entropy(Sstrong) )
所以如果我们要计算所有 S 集合的 Wind 的信息增益,使用下面的公式,
Gain(S, A) = Entropy(S) - ∑( ( |Sv|/|S| ) x Entropy(Sv) )
然后它会变成下面这样,
=) Gain(S, Wind) = Entropy(S) - ( ( |Sweak|/|S| ) x Entropy(Sweak) ) - ( ( |Sstrong|/|S| ) x Entropy(Sstrong) )
从上表关于 Wind 特性这里有两种数据类型 ( Weak, Strong ) 所以新数据集将被分为下面两个子集,
Outlook | Temperature | Humidity | Wind | Play ball |
Sunny | Hot | High | Weak | No |
Overcast | Hot | High | Weak | Yes |
Rain | Mild | High | Weak | Yes |
Rain | Cool | Normal | Weak | Yes |
Sunny | Mild | High | Weak | No |
Sunny | Cool | Normal | Weak | Yes |
Rain | Mild | Normal | Weak | Yes |
Overcast | Hot | Normal | Weak | Yes |
Total | 8 |
Outlook | Temperature | Humidity | Wind | Play ball |
Sunny | Hot | High | Strong | No |
Rain | Cool | Normal | Strong | No |
Overcast | Cool | Normal | Strong | Yes |
Sunny | Mild | Normal | Strong | Yes |
Overcast | Mild | High | Strong | Yes |
Rain | Mild | High | Strong | No |
Total | 6 |
所以, Wind 特性集合时 (Weak, Strong) 并且元素总计合计是 (8,6).
=) Gain(S, Wind) = 0.940 - ( (8/14 ) x Entropy(Sweak) ) - ( ( 6/14 ) x Entropy(Sstrong) )
如前面提到的, 如何计算熵, 现在我们将计算 Weak 的熵, 这里有两个分类器 No 和 Yes, 对于 Weak 分类器集合是 (No,Yes) 对应元素数字的集合是 (2,6) Strong (No,Yes) 集合是 (3,3).
Entropy(Sweak) = ∑-p(I)log2p(I) = ( - ( (2/8)xlog2(2/8) ) ) + ( - ( (6/8)xlog2(6/8) ) )
=) Entropy(Sweak) = 使用前面提到的熵计算过程计算 Sweak 的值.
Entropy(Sstrong) = ∑-p(I)log2p(I) = ( - ( (3/6)xlog2(3/6) ) ) + ( - ( (3/6)xlog2(3/6) ) )
=) Entropy(Sstrong) = 使用前面提到的熵计算过程计算 Sstrong的值.
所以 Gain(S, Wind) 会变成下面这样,
=) Gain(S, Wind) = 0.940 - ( (8/14 ) x Entropy(Sweak) ) - ( ( 6/14 ) x Entropy(Sstrong) )
=) Gain(S,Wind) = 0.940 - Value Of Sweak - Value Of Sstrong
=) Gain(S, Wind) = 0.048
所以所有 S 的 Wind 信息增益是 0.048. 使用一样的过程它可能计算 Outlook, Temperature 和 Humidity的信息增益 . 基于 ID3 算法最高的增益将被选择为决策节点, 这里 Outlook, 作为图 1 展示的数据集的一个结果将被分成如下几部分,
Outlook | Temperature | Humidity | Wind | Play ball |
Sunny | Hot | High | Weak | No |
Sunny | Hot | High | Strong | No |
Sunny | Mild | High | Weak | No |
Sunny | Cool | Normal | Weak | Yes |
Sunny | Mild | Normal | Strong | Yes |
Total | 5 |
Outlook | Temperature | Humidity | Wind | Play ball |
Overcast | Hot | High | Weak | Yes |
Overcast | Cool | Normal | Strong | Yes |
Overcast | Mild | High | Strong | Yes |
Overcast | Hot | Normal | Weak | Yes |
Total | 4 |
Outlook | Temperature | Humidity | Wind | Play ball |
Rain | Mild | High | Weak | Yes |
Rain | Cool | Normal | Weak | Yes |
Rain | Cool | Normal | Strong | No |
Rain | Mild | Normal | Weak | Yes |
Rain | Mild | High | Strong | No |
Total | 5 |
Outlook 有三个不同的值 Sunny, Overcast,Rain 他们将用作决策节点.
所以, 使用上面三个新集合, 信息增益将计算 Temperature, Humidity 和 Wind 并且基于计算的信息增益值进一步选择产生决策树节点. 例如, 如果我们要计算 Humidity 对于 Sunny 的信息增益,那么,
Gain(S, A) = Entropy(S) - ∑( ( |Sv|/|S| ) x Entropy(Sv) )
=) Gain(S, A) = ∑-p(I)log2p(I) - ∑( ( |Sv|/|S| ) x Entropy(Sv) ) =) Gain(Ssunny, Humidity) = (- 3/5log23/5 - 2/5log22/5) - ( ( 3/5 ) x Entropy(Shigh) ) - ( ( 2/5 ) x Entropy(Slow) )
3/5 指定 No 总计/ No 和 Yes 的总计
2/5 指定 Yes 总计/ Sunny 集合中 No 和 Yes 的总计
=) Gain(Ssunny, Humidity) = (0.4421 +0.528 ) - ( ( 3/5 ) x ∑-p(I)log2p(I) ) - ( ( 2/5 ) x ∑-p(I)log2p(I) ) )
=) Gain(Ssunny, Humidity) = (0.9708 ) - ( ( 3/5 ) x (- (3/3log2 3/3) - ( 0/3log2 0/3 ) ) ) - ( ( 2/5 ) x ( -(0/2)log2 0/2 - (2/2log22/2) ) ) )
3/3 指定 No 总计/high 集合中 No 和 Yes 的总计
0/3 指定 Yes 总计/high 集合中 No 和 Yes 的总计
0/2 指定 No 总计/low 集合中 No 和 Yes 的总计
2/2 指定 Yes 总计/low 集合中 No 和 Yes 的总计
=) Gain(Ssunny, Humidity) = 0.9708 - 3/5 x (-1 x 0 ) - 2/5 x ( 0 - ( 1x 0))
=) Gain(Ssunny, Humidity) = 0.9708
所以 Humidity 对于 Ssunny 集合的信息增益是 0.9708. 同样的方式, 我们可以计算 Temperature, Wind 对于 Ssunny 的信息增益.
参考文献
- http://en.wikipedia.org/wiki/ID3_algorithm
- http://en.wikipedia.org/wiki/Entropy_(information_theory)
- http://en.wikipedia.org/wiki/Information_gain_in_decision_trees
- http://en.wikipedia.org/wiki/Machine_learning
- http://en.wikipedia.org/wiki/Decision_tree_learning
- http://chem-eng.utoronto.ca/~datamining/dmc/decision_tree.htm
- http://mrthin.blogspot.com/2010/09/id3-algorithm.html
- http://logbase2.blogspot.com/2008/08/log-calculator.html
posted on 2015-11-01 12:58 Sky.Y.Chen 阅读(169) 评论(0) 收藏 举报