ID3算法java实现
1 ID3算法概述
1.1 信息熵
熵是无序性(或不确定性)的度量指标。假如事件A的全概率划分是(A1,A2,...,An),每部分发生的概率是(p1,p2,...,pn)。那信息熵定义为:
通常以2为底数。所以信息熵的单位是bit。
1.2 决策树
决策树是以实例为基础的归纳学习算法。它从一组无次序、无规则的元组中推理出决策树表示形式的分类规则。它採用自顶向下的递归方式。在决策树的内部结点进行属性值的比較,并依据不同的属性值从该结点向下分支,叶结点是要学习划分的类。从根到叶结点的一条路径就相应着一条合取规则,整个决策树就相应着一组析取表达式规则。
1.3 ID3算法
ID3算法的核心是:在决策树各级结点上选择属性时,用信息增益(information gain)作为属性的选择标准,以使得在每个非叶结点进行測试时,能获得关于被測试记录最大的类别信息。其详细方法是:检測全部的属性,选择信息增益最大的属性产生决策树结点,由该属性的不同取值建立分支,再对各分支的子集递归调用该方法建立决策树结点的分支,直到全部子集仅包括同一类别的数据为止。
最后得到一棵决策树,它能够用来对新的样本进行分类。
2 取样实验
样本数据:
outlook | temperature | 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 |
统计数据:(便于计算熵值)
outlook | temperature | humidity | windy | play | |||||||||
| yes | no |
| yes | no |
| yes | no |
| yes | no | yes | no |
sunny | 2 | 3 | hot | 2 | 2 | high | 3 | 4 | FALSE | 6 | 2 | 9 | 5 |
overcast | 4 | 0 | mild | 4 | 2 | normal | 6 | 1 | TRUR | 3 | 3 |
|
|
rainy | 3 | 2 | cool | 3 | 1 |
|
|
|
|
|
|
|
|
2.1 Outlook为sunny时:
temperature | humidity | windy | play |
hot | high | FALSE | no |
hot | high | TRUE | no |
mild | high | FALSE | no |
cool | normal | FALSE | yes |
mild | normal | TRUE | yes |
temperature | humidity | windy | play | |||||||
| yes | no |
| yes | no |
| yes | no | yes | no |
hot | 0 | 2 | high | 0 | 3 | FALSE | 1 | 2 | 2 | 3 |
mild | 1 | 1 | normal | 2 | 0 | TRUR | 1 | 1 |
|
|
cool | 1 | 0 |
|
|
|
|
|
|
|
|
2.1.1 humidity为high时:
temperature | windy | play |
hot | FALSE | no |
hot | TRUE | no |
mild | FALSE | no |
另外一种情况,所以的样本都属于同一类别,用相应的类别属性no来标记
2.1.2 humidity为normal时:
temperature | windy | play |
cool | FALSE | yes |
mild | TRUE | yes |
另外一种情况,所以的样本都属于同一类别。用相应的类别属性yes来标记
2.2 Outlook为overcast时:
temperature | humidity | windy | play |
hot | high | FALSE | yes |
cool | normal | TRUE | yes |
mild | high | TRUE | yes |
hot | normal | FALSE | yes |
另外一种情况。所以的样本都属于同一类别,用相应的类别属性yes来标记
2.3 Outlook为rainy时:
temperature | humidity | windy | play |
mild | high | FALSE | yes |
cool | normal | FALSE | yes |
cool | normal | TRUE | no |
mild | normal | FALSE | yes |
mild | high | TRUE | no |
temperature | humidity | windy | play | |||||||
| yes | no |
| yes | no |
| yes | no | yes | no |
mild | 2 | 1 | high | 1 | 1 | FALSE | 3 | 0 | 3 | 2 |
cool | 1 | 1 | normal | 2 | 1 | TRUR | 0 | 2 |
|
|
2.3.1 temperature为milk时:
humidity | windy | play |
high | FALSE | yes |
normal | FALSE | yes |
high | TRUE | no |
humidity | windy | play | |||||
| yes | no |
| yes | no | yes | no |
high | 1 | 1 | FALSE | 2 | 0 | 2 | 1 |
normal | 1 | 0 | TRUR | 0 | 1 |
|
|
2.3.1.1 windy为false时:
humidity | play |
high | yes |
normal | yes |
另外一种情况,所以的样本都属于同一类别。用相应的类别属性yes来标记
2.3.1.2 windy为true时:
humidity | play |
high | no |
另外一种情况。所以的样本都属于同一类别,用相应的类别属性no来标记
2.3.2 temperature为cool时:
temperature | humidity | windy | play |
cool | normal | FALSE | yes |
cool | normal | TRUE | yes |
另外一种情况,所以的样本都属于同一类别,用相应的类别属性yes来标记
经计算得到的决策树:
3 ID3算法Java实现
ID3算法实现包含四个类的设计:
一、 决策树节点类(TreeNode类),包含类属性:name(节点属性名称)。rule(节点属性值域,也就是相应决策树的分裂规则),child(节点下的孩子节点)。datas(当前决策下相应的样本元组), candidateAttr(当前决策下剩余的分类属性)。
二、 最大信息增益节点计算类(Gain类):包含属性值:D(当前决策层次下的样本数据),attrList(当前决策层次下的剩余分类属性);包含方法:统计属性取值方法,统计属性不同取值计数方法,计算先验熵和条件熵的方法。筛选指定属性索引在指定值上的样本元组方法。通过先验熵减后验熵计算出最大信息增益值属性的方法。详细方法在程序中都已经凝视。在这里仅仅是依据需求给出方法的大致功能。
三、决策树建立类(DecisionTree类):包含方法:计算当前样本中分类属性的取值及其计数,并由此计算出多数类,决策树节点递归构建构成,详细实现思想同课上讲授内容。在此不在重述,借助的类是增益值计算类。
四、 ID3算法測试类(TestDecisionTree类):借助于上面决策树建立类,决策树节点之间连接已经建立完成。以下将以上第二部分的样本数据作为測试数据,而且实现递归打印方法,输出决策树详细内容。