決策樹判斷黨派

源代碼連接: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叉樹,人爲經過後驗機率肯定一個黨派。


四 測試結果

六 參考資料

相關文章
相關標籤/搜索