第一部分:簡介node
ID3和C4.5算法都是被Quinlan提出的,用於分類模型,也被叫作決策樹。咱們給一組數據,每一行數據都含有相同的結構,包含了一系列的attribute/value對。 其中一個屬性表明了記錄的類別。決策樹的問題是對那些沒有類別屬性的記錄預測出正確的類別。通常,類別屬性取值爲true或者false,yes或者no,success或者faliure。web
舉例來看,咱們這有一些數據是是否打高爾夫球和天氣條件的關係。類別屬性是是否打高爾夫。非類別屬性具體以下:算法
ATTRIBUTE | POSSIBLE VALUES
============+=======================
outlook | sunny, overcast, rain
------------+-----------------------
temperature | continuous
------------+-----------------------
humidity | continuous
------------+-----------------------
windy | true, false
============+=======================ui
訓練數據以下:spa
OUTLOOK | TEMPERATURE | HUMIDITY | WINDY | PLAY
=====================================================
sunny | 85 | 85 | false | Don't Play
sunny | 80 | 90 | true | Don't Play
overcast| 83 | 78 | false | Play
rain | 70 | 96 | false | Play
rain | 68 | 80 | false | Play
rain | 65 | 70 | true | Don't Play
overcast| 64 | 65 | true | Play
sunny | 72 | 95 | false | Don't Play
sunny | 69 | 70 | false | Play
rain | 75 | 80 | false | Play
sunny | 75 | 70 | true | Play
overcast| 72 | 90 | true | Play
overcast| 81 | 75 | false | Play
rain | 71 | 80 | true | Don't Playorm
考慮到5個非類別屬性中,有2個是連續值,Temperature and Humidity,ID3算法沒有直接處理這樣的狀況,咱們須要C4.5裏面來思路來處理這種狀況,它是ID3的擴展。blog
一個決策樹最重要的不是它總結了咱們自己所知道的,而是咱們指望它能對新的case能夠預測準確。排序
ID3背後的基本含義是:ci
第二部分:ID3的定義it
一般,咱們給一個類別結果的一個機率分佈P = (p1, p2, .., pn),這個分佈傳達的信息量也被爲P的熵,爲:
I(P) = -(p1*log(p1) + p2*log(p2) + .. + pn*log(pn))
舉例,好比P=(0.5,0.5),那麼I(P)=1,若是P=(0.67, 0.33),那麼I(P)=0.92,P=(1,0),那麼I(P)=0。分佈越均勻,熵越大。
咱們根據非類別屬性X的值進行分割爲T1,T2...,Info(X,T) = Sum for i from 1 to n of ----(|Ti|/|T|) * Info(Ti)。
以上面的是否打高爾夫球爲例,以outlook屬性進行分割,Info(Outlook,T) = 5/14*I(2/5,3/5) + 4/14*I(4/4,0) + 5/14*I(3/5,2/5) = 0.694。
信息增益爲,Gain(X,T) = Info(T) - Info(X,T)。這個信息增益表明的意思是,在有和沒有屬性X值的上信息量的不一樣。
在Outlook屬性上,Gain(Outlook,T) = Info(T) - Info(Outlook,T) = 0.94 - 0.694 = 0.246。同理,Info(Windy,T) = 0.892 and Gain(Windy,T) = 0.048。
Gain(Outlook,T)>Gain(Windy,T),說明能夠帶來更大的信息增益。咱們用信息增益來對屬性值進行rank排序,來做爲首先決策的節點。
第三部分:ID3算法
ID3算法用來構建決策樹,給你一些非分類的屬性C1,C2...Cn,一個分類的屬性C;一個訓練集T;
function ID3 (R: a set of non-categorical attributes,
C: the categorical attribute,
S: a training set) returns a decision tree;
begin
If S is empty, return a single node with value Failure;
If S consists of records all with the same value for
the categorical attribute,
return a single node with that value;
If R is empty, then return a single node with as value
the most frequent of the values of the categorical attribute
that are found in records of S; [note that then there
will be errors, that is, records that will be improperly
classified];
Let D be the attribute with largest Gain(D,S)
among attributes in R;
Let {dj| j=1,2, .., m} be the values of attribute D;
Let {Sj| j=1,2, .., m} be the subsets of S consisting
respectively of records with value dj for attribute D;
Return a tree with root labeled D and arcs labeled
d1, d2, .., dm going respectively to the trees
ID3(R-{D}, C, S1), ID3(R-{D}, C, S2), .., ID3(R-{D}, C, Sm);
end ID3;
在高爾夫的例子中,咱們構建了下面的決策樹:
Outlook / | \ / | \ overcast / |sunny \rain / | \ Play Humidity Windy / | | \ / | | \ <=75 / >75| true| \false / | | \ Play Don'tPlay Don'tPlay Play
第四部分:使用信息增益率
觀念是信息增益會致使偏心那些包含不少值的屬性,舉一個極端例子,若是咱們有一個屬性,它的每一條記錄的屬性值都不一樣,那麼Info(D,T) = 0,可是G(D,T) = 最大。因此,Quinlan建議咱們用信息增益率來代替信息增益。
GainRatio(D,T) = Gain(D,T) / SplitInfo(D,T)。而SplitInfo(D,T)表明屬性值D的基礎上分類T的信息,SplitInfo(D,T) = I(|T1|/|T|, |T2|/|T|, .., |Tm|/|T|)。以高爾夫爲例子,SplitInfo(Outlook,T) = -5/14*log(5/14) - 4/14*log(4/14) - 5/14*log(5/14) = 1.577。那麼GainRatio(Outlook,T) = 0.246/1.577 = 0.156。
第五部分:C4.5擴展
C4.5是ID3算法的一個擴展。改進地點有:
1)根據信息增益率來選擇屬性。用來克服了ID3用信息增益選擇屬性時偏向選擇取值多的屬性的不足。
2)在樹構造過程當中進行剪枝,在構造決策樹的時候,那些掛着幾個元素的節點,不考慮最好,否則容易致使overfitting。
3)對非離散數據也能處理。
4)可以對不完整數據進行處理。