1.1 資訊熵
熵是無序性(或不確定性)的度量指標。假如事件a
的全概率劃分是(
a1,a2,...,an
),每部分發生的概率是
(p1,p2,...,pn)
,那資訊熵定義為:
通常以2
為底數,所以資訊熵的單位是
bit。
1.2 決策樹
決策樹是以例項為基礎的歸納學習演算法。它從一組無次序、無規則的元組中推理出決策樹表示形式的分類規則。它採用自頂向下的遞迴方式,在決策樹的內部結點進行屬性值的比較,並根據不同的屬性值從該結點向下分支,葉結點是要學習劃分的類。從根到葉結點的一條路徑就對應著一條合取規則,整個決策樹就對應著一組析取表示式規則。
1.3 id3演算法
id3演算法的核心是:在決策樹各級結點上選擇屬性時,用資訊增益(information gain)作為屬性的選擇標準,以使得在每乙個非葉結點進行測試時,能獲得關於被測試記錄最大的類別資訊。其具體方法是:檢測所有的屬性,選擇資訊增益最大的屬性產生決策樹結點,由該屬性的不同取值建立分支,再對各分支的子集遞迴呼叫該方法建立決策樹結點的分支,直到所有子集僅包含同一類別的資料為止。最後得到一棵決策樹,它可以用來對新的樣本進行分類。
樣本資料:
outlook
temperature
humidity
windy
play
sunny
hothigh
false
nosunny
hothigh
true
noovercast
hothigh
false
yesrainy
mild
high
false
yesrainy
cool
normal
false
yesrainy
cool
normal
true
noovercast
cool
normal
true
yessunny
mild
high
false
nosunny
cool
normal
false
yesrainy
mild
normal
false
yessunny
mild
normal
true
yesovercast
mild
high
true
yesovercast
hotnormal
false
yesrainy
mild
high
true
no統計資料:(便於計算熵值)
outlook
temperature
humidity
windy
play
yesno
yesno
yesno
yesno
yesno
sunny
hothigh
false
overcast
mild
normal
trur
rainy
cool
2.1 outlook為sunny時:
temperature
humidity
windy
play
hothigh
false
nohot
high
true
nomild
high
false
nocool
normal
false
yesmild
normal
true
yestemperature
humidity
windy
play
yesno
yesno
yesno
yesno
hothigh
false
mild
normal
trur
cool
2.1.1 humidity為high時:
temperature
windy
play
hotfalse
nohot
true
nomild
false
no第二種情況,所以的樣本都屬於同一類別,用對應的類別屬性no
來標記2.1.2 humidity為normal時:
temperature
windy
play
cool
false
yesmild
true
yes
第二種情況,所以的樣本都屬於同一類別,用對應的類別屬性yes
來標記2.2 outlook為overcast時:
temperature
humidity
windy
play
hothigh
false
yescool
normal
true
yesmild
high
true
yeshot
normal
false
yes第二種情況,所以的樣本都屬於同一類別,用對應的類別屬性yes
來標記2.3 outlook為rainy時:
temperature
humidity
windy
play
mild
high
false
yescool
normal
false
yescool
normal
true
nomild
normal
false
yesmild
high
true
notemperature
humidity
windy
play
yesno
yesno
yesno
yesno
mild
high
false
cool
normal
trur
2.3.1 temperature為milk時:
humidity
windy
play
high
false
yesnormal
false
yeshigh
true
nohumidity
windy
play
yesno
yesno
yesno
high
false
normal
trur
2.3.1.1 windy為false時:
humidity
play
high
yesnormal
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
yescool
normal
true
yes第二種情況,所以的樣本都屬於同一類別,用對應的類別屬性yes
來標記經計算得到的決策樹:
id3演算法實現包括四個類的設計:
一、 決策樹節點類(treenode
類),包括類屬性:
name
(節點屬性名稱),
rule
(節點屬性值域,也就是對應決策樹的**規則),
child
(節點下的孩子節點),
datas
(當前決策下對應的樣本元組),
candidateattr
(當前決策下剩餘的分類屬性)。
二、 最大資訊增益節點計算類(gain
類):包括屬性值:
d(當前決策層次下的樣本資料),
attrlist
(當前決策層次下的剩餘分類屬性);包括方法:統計屬性取值方法,統計屬性不同取值計數方法,計算先驗熵和條件熵的方法,篩選指定屬性索引在指定值上的樣本元組方法,通過先驗熵減後驗熵計算出最大資訊增益值屬性的方法。具體方法在程式中都已經注釋,在這裡只是根據需求給出方法的大致功能。
三、決策樹建立類(decisiontree
類):包括方法:計算當前樣本中分類屬性的取值及其計數,並由此計算出多數類,決策樹節點遞迴構建構成,具體實現思想同課上講授內容,在此不在重述,借助的類是增益值計算類。
四、 id3演算法測試類(
testdecisiontree
類):借助於上面決策樹建立類,決策樹節點之間連線已經建立完畢,下面將以上第二部分的樣本資料作為測試資料,並且實現遞迴列印方法,輸出決策樹具體內容。
java實現ID3演算法
id3是經典的分類演算法,要理解id3演算法,需要先了解一些基本的資訊理論概念,包括資訊量,熵,後驗熵,條件熵。id3演算法的核心思想是選擇互資訊量最大的屬性作為分割節點,這樣做可以保證所建立的決策樹高度最小。樹結構 c4.5決策樹資料結構 author zhenhua.chen descripti...
ID3演算法的java實現
id3演算法是經典的決策樹學習生成演算法。id3演算法的核心是在決策樹各個節點上運用資訊增益準則選擇特徵,遞迴的構建決策樹。具體方法是 從根節點 root node 開始,對接點計算所有可能的特徵的資訊增益,選擇資訊增益最大的特徵作為節點的特徵,有該特徵的不同取值建立子節點 再對子節點遞迴的呼叫以上...
ID3演算法的Python實現
本篇文章的 是在 id3演算法的原理及實現 python 的基礎上進行新增和修改實現的,感謝原作者。1 新增的功能 1 拆分檔案,使得函式的呼叫更加清晰 2 增加了gui,增加了資料的讀取和 功能 3 增加了乙個遞迴終止條件 2 gui介面展示 以檔案中給出的資料集為例,填充如下 注 這裡類標籤的位...