决策树判断党派

源代码连接:http://www.oschina.net/code/snippet_2348884_47619算法

1、问题描述数组

问题背景:session

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

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

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

的数据做为测试数据,测试创建决策树的准确度。性能

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

数据集描述测试

1. Title: 1984 United States Congressional Voting Records Databaseui

 

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=?)


2、模型创建

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

号。

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

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

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

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

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



 

 

3、算法分析

知识准备:

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

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

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

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

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

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

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

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

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

1)训练数据;

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

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

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

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

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

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

模型的准确率。绝对不容许用于模型构建过程,不然会致使过渡拟合。

过渡拟合:

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

测试数据上却获得很是高的错误率。

决策树的构建过程是一个递归的过程,因此须要肯定中止条件,不然过程将不会结束。

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

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

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

过渡拟合的缘由有如下几点:


交叉验证:

交叉检验(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个孩

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



要肯定一个党派,咱们选用的是根据 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

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

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

6、如有空堆,为了保证是满3叉树,人为经过后验几率肯定一个党派。


四 测试结果

六 参考资料

相关文章
相关标签/搜索