1、引例
假如讓不瞭解NBA的社區張大媽猜去年奪冠的球隊 【去年總決賽勇士VS騎士,勇士總冠軍】 。給出的候選球隊依次爲w-1-火箭、w-2-勇士、w-3-步行者、w-4-雷霆、e-1-猛龍、e-2-凱爾特人、e-3-76人 和 e-4-騎士這8支隊伍。當大媽每猜一次時只能給出正確與錯誤的反饋時,那麼如何讓不知情的張大媽能在最少次數下答對?
在不知情的情況下猜測,每個隊伍的奪冠概率相同,那麼最糟的情況是猜了7次,利用折半法也需要3次。因爲當概率均等時,信息熵最大,即不確定性最大。(信息熵概念下文有)那如果大媽平時偶爾瞭解過NBA並且知道球隊分爲東西部以及瞭解某幾個球隊比較有奪冠實力的話,那不就可以減少猜測的次數了?
這背後就隱藏着決策樹了,決策樹就是降低信息不確定性的過程。
2、基本概念
引入決策樹前,必須要了解“信息熵”以及“信息增益”。
2.1、信息熵
想了解熵的更多信息,可以參閱另外一篇博客。
熵是表示最忌變量不確定性的度量。
信息論之父克勞德·香農給出的信息熵的三個性質[1]:
1、單調性,發生概率越高的事件,其攜帶的信息量越低;
2、非負性,信息熵可以看作爲一種廣度量,非負性是一種合理的必然;
3、累加性,即多隨機事件同時發生存在的總不確定性的量度是可以表示爲各事件不確定性的量度的和,這也是廣度量的一種體現。
熵(entropy)一般用 H 表示,單位爲比特。當概率均等時,信息熵最大,即不確定性最大。 香農從數學上嚴格證明了滿足上述三個條件的隨機變量不確定性度量函數具有唯一形式:
H(X)=−Cx∈χ∑p(x)logp(x)
其中 C 爲常數,我們將 C=1 便可以得到信息熵公式。
2.2、條件熵
條件熵(condition entropy)表示在給定已知某種條件 X 下,事物 Y 的熵即爲條件熵 H(Y∣X) ,其公式定義爲:
H(Y∣X)=x∈X∑p(x)H(Y∣x)=−x∈X∑p(x)y∈Y∑p(y∣x)logp(y∣x)
2.3、信息增益
信息增益(information gain)表示存在某種條件 X ,它使得條件熵 H(Y∣X) 儘可能的小(也就是“不確定性”減少的多),即當引入信息 X 之後,事物 Y 的熵變小了。則信息增益記爲:
g(Y∣X)=H(Y)−H(Y∣X)
3、小實例(利用ID3算法處理)
【數據介紹】:14天打籃球情況,包含4種環境特徵
【目標】:構造決策樹
【數據展示】:
ID |
weather(天氣) |
temperature(溫度) |
humidity(溼度) |
windy(風況) |
play(是否打球) |
1 |
sunny |
hot |
high |
false |
no |
2 |
sunny |
hot |
high |
true |
no |
3 |
overcast |
hot |
high |
false |
yes |
4 |
rainy |
mild |
high |
false |
yes |
5 |
rainy |
cool |
normal |
false |
yes |
6 |
rainy |
cool |
normal |
true |
no |
|
overcast |
cool |
normal |
true |
yes |
8 |
sunny |
mild |
high |
false |
no |
9 |
sunny |
cool |
normal |
false |
yes |
10 |
rainy |
mild |
normal |
false |
yes |
11 |
sunny |
mild |
normal |
true |
yes |
12 |
overcast |
mild |
high |
true |
yes |
13 |
overcast |
hot |
normal |
false |
yes |
14 |
rainy |
mild |
high |
true |
no |
【開始分析】:
第一步,根據標籤值(打沒打籃球),計算熵。
14天中有9天打,5天沒打,此時熵:
⎩⎪⎨⎪⎧χ={no,yes}p(x=yes)=149p(x=no)=145
H(play)=−x∈χ∑p(x)logp(x)=−149log149−145log145=0.940
第二步,根據4種特徵分別計算條件熵。
-
基於weather條件。
通過數據表格,我們知道:
weather(14)⎩⎪⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎧sunny(5){yes(2)no(3)overcast(4){yes(4)no(0)rainy(5){yes(3)no(2)
那麼,計算基於weather的條件熵:
H(play∣weather)=x∈weather∑p(x)H(play∣x)
=p(sunny)H(play∣sunny)+p(overcast)H(play∣overcast)+p(rainy)H(play∣rainy)
其中:
⎩⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎧weather={sunny,overcast,rainy}play={yes,no}p(sunny)=145p(overcast)=144p(rainy)=145;⎩⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎧p(yes∣sunny)=52p(no∣sunny)=53p(yes∣overcast)=44=1p(no∣overcast)=0p(yes∣rainy)=53p(no∣rainy)=52
⎩⎪⎨⎪⎧H(play∣suny)=−∑y∈playp(y∣sunny)logp(y∣sunny)=−52log52−53log53=0.971H(play∣overcast)=−∑y∈playp(y∣overcast)logp(y∣overcast)=−1×log1−0=0H(play∣rainy)=−∑y∈playp(y∣rainy)logp(y∣rainy)=−53log53−52log52=0.971
所以有:
H(play∣weather)=145×0.971+144×0+145×0.971=0.693
-
基於temperautre條件。
通過數據表格,我們知道:
temperature(14)⎩⎪⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎧hot(4){yes(2)no(2)mild(6){yes(4)no(2)cool(4){yes(3)no(1)
爲了不贅述,計算過程同1,可以計算出基於temperature的條件熵:
H(play∣temperature)=x∈temperature∑p(x)H(play∣x)=144×1+146×0.918+144×0.811=0.911
-
基於humidity條件。
通過數據表格,我們知道:
humidity(14)⎩⎪⎪⎪⎪⎨⎪⎪⎪⎪⎧high(7){yes(3)no(4)normal(7){yes(6)no(1)
計算出基於humidity的條件熵:
H(play∣humidity)=x∈humidity∑p(x)H(play∣x)=147×0.985+147×0.592=0.788
-
基於windy條件。
通過數據表格,我們知道:
windy(14)⎩⎪⎪⎪⎪⎨⎪⎪⎪⎪⎧false(8){yes(6)no(2)true(6){yes(3)no(3)
計算出基於windy的條件熵:
H(play∣windy)=x∈windy∑p(x)H(play∣x)=148×0.811+146×1=0.892
第三步,計算信息增益。
根據第一步計算的熵以及第二步所計算的條件熵,我們可以計算出第一輪的信息增益:
⎩⎪⎪⎪⎨⎪⎪⎪⎧gain(play∣weather)=H(play)−H(play∣weather)=0.940−0.693=0.247gain(play∣temperature)=H(play)−H(play∣temperature)=0.940−0.911=0.029gain(play∣humidity)=H(play)−H(play∣humidity)=0.940−0.788=0.152gain(play∣windy)=H(play)−H(play∣windy)=0.940−0.892=0.048
可以得知 $gain(play\mid weather) $ 的信息增益最大 ,所以第一輪產生的決特徵爲所以第一輪產生的決特徵爲weather,將其設置爲決策樹的根節點。
此時,根據特徵weather的劃分後,數據表爲:
ID |
weather(天氣) |
temperature(溫度) |
humidity(溼度) |
windy(風況) |
play(是否打球) |
1 |
sunny |
hot |
high |
false |
no |
2 |
sunny |
hot |
high |
true |
no |
8 |
sunny |
mild |
high |
false |
no |
9 |
sunny |
cool |
normal |
false |
yes |
11 |
sunny |
mild |
normal |
true |
yes |
3 |
overcast |
hot |
high |
false |
yes |
7 |
overcast |
cool |
normal |
true |
yes |
12 |
overcast |
mild |
high |
true |
yes |
13 |
overcast |
hot |
normal |
false |
yes |
4 |
rainy |
mild |
high |
false |
yes |
5 |
rainy |
cool |
normal |
false |
yes |
6 |
rainy |
cool |
normal |
true |
no |
10 |
rainy |
mild |
normal |
false |
yes |
14 |
rainy |
mild |
high |
true |
no |
根據特徵 weather 劃分後的表格可以清晰的看到,在 weather 爲 overcast 的情況下都去打球了。
此時決策樹第一步的畫法:
第四步,重複第二、三倆步驟,選出第二個特徵。
此時還剩下三個特徵,則開始計算除了 overcast 的其餘10條數據的熵,因爲這10天裏,有5天打球5天沒打,所以此時的熵爲:
H(play)=−105×log105−105×log105=1
和第一輪一樣的處理流程,我們分別計算餘下的三個特徵的條件熵:
-
基於 temperature 的條件熵。
通過劃分後的數據表格,我們知道:
temperature(10)⎩⎪⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎧hot(2){yes(0)no(2)mild(5){yes(3)no(2)cool(3){yes(2)no(1)
H(play∣temperature)=x∈temperature∑p(x)H(play∣x)
=p(hot)H(play∣hot)+p(mild)H(play∣mild)+p(cool)H(play∣cool)
其中:
⎩⎪⎨⎪⎧p(hot)=102p(mild)=105p(cool)=103;⎩⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎧p(yes∣hot)=0p(no∣hot)=1p(yes∣mild)=53p(no∣mild)=52p(yes∣cool)=32p(yes∣cool)=31
⎩⎪⎨⎪⎧H(play∣hot)=−∑y∈playp(y∣hot)logp(y∣hot)=−0−1×log1=0H(play∣mild)=−∑y∈playp(y∣mild)logp(y∣mild)=−53×log53−52×log52=0.971H(play∣cool)=−∑y∈playp(y∣cool)logp(y∣cool)=−32×log32−31×log31=0.918
所以有:
H(play∣temperature)=x∈temperature∑p(x)H(play∣x)=102×0+105×0.971+103×0.918=0.761
-
基於 humidity 的條件熵。
通過劃分後的數據表格,我們知道:
humidity(10)⎩⎪⎪⎪⎪⎨⎪⎪⎪⎪⎧high(5){yes(1)no(4)normal(5){yes(4)no(1)
同上,計算得:
H(play∣humidity)=x∈humidity∑p(x)H(play∣x)=105×0.722+105×0.722=0.722
-
基於 windy 的條件熵。
通過劃分後的數據表格,我們知道:
windy(10)⎩⎪⎪⎪⎪⎨⎪⎪⎪⎪⎧false(6){yes(4)no(2)true(4){yes(1)no(3)
同上,計算得:
H(play∣windy)=x∈windy∑p(x)H(play∣x)=106×0.918+104×0.811=0.875
-
計算新一輪的信息增益,並選出最大的作爲下一個決策特徵。
第二輪的信息增益爲:
⎩⎪⎨⎪⎧gain(play∣temperature)=H(play)−H(play∣temperature)=1−0.761=0.0.239gain(play∣humidity)=H(play)−H(play∣humidity)=1−0.722=0.278gain(play∣windy)=H(play)−H(play∣windy)=1−0.875=0.125
可知,gain(paly∣humidity) 最大,第二輪產生的決策特徵爲humidity 。
此時,根據特徵 humidity 的劃分後,數據表爲:
ID |
weather(天氣) |
temperature(溫度) |
humidity(溼度) |
windy(風況) |
play(是否打球) |
1 |
sunny |
hot |
high |
false |
no |
2 |
sunny |
hot |
high |
true |
no |
8 |
sunny |
mild |
high |
false |
no |
4 |
rainy |
mild |
high |
false |
yes |
14 |
rainy |
mild |
high |
true |
no |
9 |
sunny |
cool |
normal |
false |
yes |
11 |
sunny |
mild |
normal |
false |
yes |
5 |
rainy |
cool |
normal |
false |
yes |
6 |
rainy |
cool |
normal |
true |
no |
10 |
rainy |
mild |
normal |
false |
yes |
|
|
|
|
|
|
3 |
overcast |
hot |
high |
false |
yes |
7 |
overcast |
cool |
normal |
true |
yes |
12 |
overcast |
mild |
high |
true |
yes |
13 |
overcast |
hot |
normal |
false |
yes |
根據特徵 humidity劃分後的表格可以清晰的看到,在 weather 爲 sunny 並且 humidity 爲 hight 的情況下都沒打球,在 weather 爲 sunny 並且 humidity 爲 normal 的情況下都打球了。
所以此時補充決策樹的畫法:
第五步,重複二、三倆步驟,選出第三個特徵。
。。。 。。。
後面運算以及表格的變化都與前面的操作相似,由於篇幅有限,這裏就不贅述了,直接給出最後的分劃數據表以及決策樹。
第三個選出來的特徵爲 windy ,然後全部決策並劃分結束。
ID |
weather(天氣) |
temperature(溫度) |
humidity(溼度) |
windy(風況) |
play(是否打球) |
4 |
rainy |
mild |
high |
false |
yes |
5 |
rainy |
cool |
normal |
false |
yes |
10 |
rainy |
mild |
normal |
false |
yes |
14 |
rainy |
mild |
high |
true |
no |
6 |
rainy |
cool |
normal |
true |
no |
|
|
|
|
|
|
1 |
sunny |
hot |
high |
false |
no |
2 |
sunny |
hot |
high |
true |
no |
8 |
sunny |
mild |
high |
false |
no |
9 |
sunny |
cool |
normal |
false |
yes |
11 |
sunny |
mild |
normal |
false |
yes |
3 |
overcast |
hot |
high |
false |
yes |
7 |
overcast |
cool |
normal |
true |
yes |
12 |
overcast |
mild |
high |
true |
yes |
13 |
overcast |
hot |
normal |
false |
yes |
最後的決策樹畫法:
參考