决策树判断党派

源代码链接:http://www.oschina.net/code/snippet_2348884_47619

一、问题描述

问题背景:

对根据投票人对每项决议(一共16项决议)的投票结果(分为yesno,?(丢失)

三种)和他的党派,构建一棵决策树,用于预测一个未知党派的投票者的党派。对于一个

给定的具有435条数据项的数据集,选取一部分数据作为训练集生成决策树,然后利用剩下

的数据作为测试数据,测试建立决策树的准确度。

数据集中还包含每个人的党派属性和每项决议属性的先验和后验概率。

数据集描述

1. Title: 1984 United States Congressional Voting Records Database

 

2. Source Information:

    (a) Source:  Congressional Quarterly Almanac, 98th Congress,

                 2nd session 1984, Volume XL: Congressional Quarterly Inc.

                 Washington, D.C., 1985.

    (b) Donor: Jeff Schlimmer (Jeffrey.Schlimmer@a.gp.csNaNu.edu)

    (c) Date: 27 April 1987

 

3. Past Usage

   - Publications

     1. Schlimmer, J. C. (1987).  Concept acquisition through

representational adjustment.  Doctoral dissertation, Department of

        Information and Computer Science, University of California, Irvine, CA.

        -- Results: about 90%-95% accuracy appears to be STAGGER's asymptote

     - Predicted attribute: party affiliation (2 classes)

 

4. Relevant Information:

      This data set includes votes for each of the U.S. House of

      Representatives Congressmen on the 16 key votes identified by the

      CQA.  The CQA lists nine different types of votes: voted for, paired

for, and announced for (these three simplified to yea), voted

against, paired against, and announced against (these three

simplified to nay), voted present, voted present to avoid conflict

of interest, and did not vote or otherwise make a position known

      (these three simplified to an unknown disposition).

 

5. Number of Instances: 435 (267 democrats, 168 republicans)

 

6. Number of Attributes: 16 + class name = 17 (all Boolean valued)

 

7. Attribute Information:

   1. Class Name: 2 (democrat, republican)

   2. handicapped-infants: 2 (y,n)

   3. water-project-cost-sharing: 2 (y,n)

   4. adoption-of-the-budget-resolution: 2 (y,n)

   5. physician-fee-freeze: 2 (y,n)

   6. el-salvador-aid: 2 (y,n)

   7. religious-groups-in-schools: 2 (y,n)

   8. anti-satellite-test-ban: 2 (y,n)

   9. aid-to-nicaraguan-contras: 2 (y,n)

  10. mx-missile: 2 (y,n)

  11. immigration: 2 (y,n)

  12. synfuels-corporation-cutback: 2 (y,n)

  13. education-spending: 2 (y,n)

  14. superfund-right-to-sue: 2 (y,n)

  15. crime: 2 (y,n)

  16. duty-free-exports: 2 (y,n)

  17. export-administration-act-south-africa: 2 (y,n)

 

8. Missing Attribute Values: Denoted by "?"

 

   NOTE: It is important to recognize that "?" in this database does

not mean that the value of the attribute is unknown.  It

means simply, that the value is not "yea" or "nay" (see

         "Relevant Information" section above).

 

   Attribute:  #Missing Values:

           1:  0

           2:  0

           3:  12

           4:  48

           5:  11

           6:  11

           7:  15

           8:  11

           9:  14

          10:  15

          11:  22

          12:  7

          13:  21

          14:  31

          15:  25

          16:  17

          17:  28

 

9. Class Distribution: (2 classes)

   1. 45.2 percent are democrat

   2. 54.8 percent are republican

 

Class predictiveness and predictability: Pr(C|A=V) and Pr(A=V|C)

 Attribute 1: (A = handicapped-infants)

  0.91;  1.21  (C=democrat; V=y)

  0.09;  0.10  (C=republican; V=y)

  0.43;  0.38  (C=democrat; V=n)

  0.57;  0.41  (C=republican; V=n)

  0.75;  0.03  (C=democrat; V=?)

  0.25;  0.01  (C=republican; V=?)

 Attribute 2: (A = water-project-cost-sharing)

  0.62;  0.45  (C=democrat; V=y)

  0.38;  0.23  (C=republican; V=y)

  0.62;  0.45  (C=democrat; V=n)

  0.38;  0.23  (C=republican; V=n)

  0.58;  0.10  (C=democrat; V=?)

  0.42;  0.06  (C=republican; V=?)

 Attribute 3: (A = adoption-of-the-budget-resolution)

  0.91;  0.87  (C=democrat; V=y)

  0.09;  0.07  (C=republican; V=y)

  0.17;  0.11  (C=democrat; V=n)

  0.83;  0.44  (C=republican; V=n)

  0.64;  0.03  (C=democrat; V=?)

  0.36;  0.01  (C=republican; V=?)

 Attribute 4: (A = physician-fee-freeze)

  0.08;  0.05  (C=democrat; V=y)

  0.92;  0.50  (C=republican; V=y)

  0.99;  0.92  (C=democrat; V=n)

  0.01;  0.01  (C=republican; V=n)

  0.73;  0.03  (C=democrat; V=?)

  0.27;  0.01  (C=republican; V=?)

 Attribute 5: (A = el-salvador-aid)

  0.26;  0.21  (C=democrat; V=y)

  0.74;  0.48  (C=republican; V=y)

  0.96;  0.75  (C=democrat; V=n)

  0.04;  0.02  (C=republican; V=n)

  0.80;  0.04  (C=democrat; V=?)

  0.20;  0.01  (C=republican; V=?)

 Attribute 6: (A = religious-groups-in-schools)

  0.45;  0.46  (C=democrat; V=y)

  0.55;  0.46  (C=republican; V=y)

  0.89;  0.51  (C=democrat; V=n)

  0.11;  0.05  (C=republican; V=n)

  0.82;  0.03  (C=democrat; V=?)

  0.18;  0.01  (C=republican; V=?)

 Attribute 7: (A = anti-satellite-test-ban)

  0.84;  0.75  (C=democrat; V=y)

  0.16;  0.12  (C=republican; V=y)

  0.32;  0.22  (C=democrat; V=n)

  0.68;  0.38  (C=republican; V=n)

  0.57;  0.03  (C=democrat; V=?)

  0.43;  0.02  (C=republican; V=?)

 Attribute 8: (A = aid-to-nicaraguan-contras)

  0.90;  0.82  (C=democrat; V=y)

  0.10;  0.07  (C=republican; V=y)

  0.25;  0.17  (C=democrat; V=n)

  0.75;  0.41  (C=republican; V=n)

  0.27;  0.01  (C=democrat; V=?)

  0.73;  0.03  (C=republican; V=?)

 Attribute 9: (A = mx-missile)

  0.91;  0.70  (C=democrat; V=y)

  0.09;  0.06  (C=republican; V=y)

  0.29;  0.22  (C=democrat; V=n)

  0.71;  0.45  (C=republican; V=n)

  0.86;  0.07  (C=democrat; V=?)

  0.14;  0.01  (C=republican; V=?)

 Attribute 10: (A = immigration)

  0.57;  0.46  (C=democrat; V=y)

  0.43;  0.28  (C=republican; V=y)

  0.66;  0.52  (C=democrat; V=n)

  0.34;  0.23  (C=republican; V=n)

  0.57;  0.01  (C=democrat; V=?)

  0.43;  0.01  (C=republican; V=?)

 Attribute 11: (A = synfuels-corporation-cutback)

  0.86;  0.48  (C=democrat; V=y)

  0.14;  0.06  (C=republican; V=y)

  0.48;  0.47  (C=democrat; V=n)

  0.52;  0.43  (C=republican; V=n)

  0.57;  0.04  (C=democrat; V=?)

  0.43;  0.03  (C=republican; V=?)

 Attribute 12: (A = education-spending)

  0.21;  0.13  (C=democrat; V=y)

  0.79;  0.42  (C=republican; V=y)

  0.91;  0.80  (C=democrat; V=n)

  0.09;  0.06  (C=republican; V=n)

  0.58;  0.07  (C=democrat; V=?)

  0.42;  0.04  (C=republican; V=?)

 Attribute 13: (A = superfund-right-to-sue)

  0.35;  0.27  (C=democrat; V=y)

  0.65;  0.42  (C=republican; V=y)

  0.89;  0.67  (C=democrat; V=n)

  0.11;  0.07  (C=republican; V=n)

  0.60;  0.06  (C=democrat; V=?)

  0.40;  0.03  (C=republican; V=?)

 Attribute 14: (A = crime)

  0.36;  0.34  (C=democrat; V=y)

  0.64;  0.49  (C=republican; V=y)

  0.98;  0.63  (C=democrat; V=n)

  0.02;  0.01  (C=republican; V=n)

  0.59;  0.04  (C=democrat; V=?)

  0.41;  0.02  (C=republican; V=?)

 Attribute 15: (A = duty-free-exports)

  0.92;  0.60  (C=democrat; V=y)

  0.08;  0.04  (C=republican; V=y)

  0.39;  0.34  (C=democrat; V=n)

  0.61;  0.44  (C=republican; V=n)

  0.57;  0.06  (C=democrat; V=?)

  0.43;  0.04  (C=republican; V=?)

 Attribute 16: (A = export-administration-act-south-africa)

  0.64;  0.65  (C=democrat; V=y)

  0.36;  0.30  (C=republican; V=y)

  0.19;  0.04  (C=democrat; V=n)

  0.81;  0.15  (C=republican; V=n)

  0.79;  0.31  (C=democrat; V=?)

  0.21;  0.07  (C=republican; V=?)


二、模型建立

决策树的每个节点包含当前这一堆分类的人的信息和该节点选用的最佳分割属性的序

号。

每条数据项除党派之外有16个属性每个属性有3个可能的属性值,将16个属性作为分

割属性,利用435条数据项目中一部分数据项作为训练集,根据ID3算法建立决策树。

建树过程中每次需要算最佳分割属性,即使用分堆后加权熵最小的属性。对于属性用

完后仍存在杂堆的情况,直接选择杂堆中党派较多的作为该堆的党派。

遇到分堆时有空堆的时候,根据后验概率确定该分堆的党派。



 114303_jD5f_2348884.png

 

三、算法分析

知识准备:

在数据分析中,有些算法需要利用现有的数据构建模型,比如贝叶斯分类器,决策树,

线性回归等,这类算法统称为监督学习(Supervisied Learning)算法。构建模型需要的

数据称之为训练数据(TrainData)。

决策树模型构建完后,需要利用数据验证模型的正确性,这部分数据被称为测试数据

TestData)。测试数据不能用于模型构建之中,只能用于最后检验模型的准确性。

训练数据,验证数据和测试数据

一般做预测分析时,会将数据分为两大部分。一部分是训练数据,用于构建模型,一部分

是测试数据,用于检验模型。但是,有时候模型的构建过程中也需要检验模型,辅助模型

构建,所以会将训练数据在分为两个部分:

1)训练数据;

2)验证数据(ValidationData)。验证数据用于负责模型的构建。典型的例子是用K-

Fold Cross Validation裁剪决策树,求出最优叶节点数,防止过渡拟合(Overfitting)。

下面形式的描述一下前面提到的3类数据:

训练数据(TestData):用于模型构建

验证数据(ValidationData):可选,用于辅助模型构建,可以重复使用。

测试数据(TestData):用于检测模型构建,此数据只在模型检验时使用,用于评估

模型的准确率。绝对不允许用于模型构建过程,否则会导致过渡拟合。

过渡拟合:

也就是该决策树对训练数据可以得到很低的错误率,但是运用到

测试数据上却得到非常高的错误率。

决策树的构建过程是一个递归的过程,所以需要确定停止条件,否则过程将不会结束。

一种最直观的方式是当每个子节点只有一种类型的记录时停止,但是这样往往会使得树的

节点过多,导致过拟合问题(Overfitting)。另一种可行的方法是当前节点中的记录数低

于一个最小的阀值,那么就停止分割,将 max(P(i))对应的分类作为当前叶节点的分类。

过渡拟合的原因有以下几点:

114903_EJ7y_2348884.png

114903_Zogo_2348884.png

114747_60PB_2348884.png


交叉验证:

交叉检验(Cross Validation

K次交叉检验(K-Fold  Cross   Validation

K次交叉检验的大致思想是将数据大致分为 K个子样本,每次取一个样本作为验证数

据,取余下的 K-1个样本作为训练数据。模型构建后作用于验证数据上,计算出当前错误

率。重复 K次,将 K次错误率平均,得到一个总体的错误率。可以通过整体错误率,估计

当前整体数据用于建模的错误率。

举个例子,K = 10(常见情况),求出总体错误率为 8.7%。那么将当前的所有数据全

部作为训练数据,得到的模型的错误率 90%的可能在 9.7%左右。

剪枝概述:

其实剪枝的准则是如何确定决策树的规模,可以参考的剪枝思路有以下几个:

1:使用训练集合(TrainingSet)和验证集合(ValidationSet),来评估剪枝方法在修

剪结点上的效用

2:使用所有的训练集合进行训练,但是用统计测试来估计修剪特定结点是否会改善训练

集合外的数据的评估性能,如使用Chi-SquareQuinlan1986)测试来进一步扩展结点

是否能改善整个分类数据的性能,还是仅仅改善了当前训练集合数据上的性能。

3:使用明确的标准来衡量训练样例和决策树的复杂度,当编码长度最小时,停止树增长,

MDL(Minimum DescriptionLength)准则。

思路:

本题使用决策树ID3算法,考虑的重点因素有:

1)由于每个属性都对应的是 3个可能的属性值,也就是说每个属性分堆后堆数都在   0-

3堆,所以分裂信息度量没有什么太大作用,就没有选用  C4.5算法。

2)要满足验证的目标,必须确保决策树是满 n叉树,即每个非叶子节点必须有n个孩

子,所以要对没有样例的属性值的堆人为添加一个党派属性。如下图示意。



115147_96lv_2348884.png

要确定一个党派,我们选用的是根据 P(C|A)来确定,使用了两种方法。

具体方法:

1、只根据当前属性的后验概率,即根据父节点所依据的分堆的属性对应的概率确定是哪

一个党派(P(C| A = ?) >0.5则为该党派).

2、根据之前所用的全部属性的乘积来判断,比较两个党派概率的大小,大的为所选择的

党派(以下两个概率哪个较大,就是哪个党派。

P(C =   Re )=P(C = Re| A0) * …* P(C = Re| Ai)

P(C =   De )=P(C = De| A0) * …* P(C = De| Ai)

3)解决决策树过度拟合的方案,采用交叉验证。将 435条数据分成  5份,每份  87条数

据。(在网上查阅资料的时候还发现一系列的剪枝算法,预先剪枝,后置剪枝等,由于时

间关系,还没来得及实现,之后会继续思考完善)

解题步骤:

1、从文件中读入概率数据:所有后验概率,存储在概率数组中。读入数据项同时,将未选

用的数据项写入一个新的文件,这将作为测试集使用。

2、读取文件dataset,从中选取训练样本中的数据项,

决策树根节点属性。

加入决策树的根节点中,初始化

3、构建决策树,使用非递归层次遍历方法,借助队列实现。每一次选择具有最大信息增益

率的分类属性作为决定属性,生成叶子节点进入队列。

4、选择确定最佳分割属性的函数:熵函数Entropy

1Entropyfunction

115305_iyr5_2348884.png

则总的熵为各堆按人数加权。选取最后总额熵最小的分割属性为最佳分割属性。

5、若属性值用完,选取当前人数中占多数的党派。

6、若有空堆,为了保证是满3叉树,人为通过后验概率确定一个党派。


四 测试结果

120042_v6jB_2348884.png

120042_59kl_2348884.png

120042_L1D1_2348884.png

120042_1FeC_2348884.png

120042_Ganm_2348884.png

六 参考资料

120224_tJKM_2348884.png

转载于:https://my.oschina.net/u/2348884/blog/406762

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值