All Revision Notes

COM6004 Data Mining · Chapter 8

Ensemble Methods: Bagging, Boosting (AdaBoost) & Random Forest(非考試範圍)

COM6004 · Chapter 8 · Source: COM6004_Chapter_8_Ensemble_Methods.pptx (27 slides) · Tan, Steinbach, Karpatne, Kumar, Introduction to Data Mining 2nd ed.

Contents · 13 sections
🚫 非 COM6004 考試範圍

COM6004 考試範圍只包括 Chapter 1–6,所以呢頁冇 Exam 標記。不過呢章同 Chapter 4 Classification 嘅 decision tree、entropy、overfitting 關係好密切,可以當係 classification 嘅延伸嚟睇。

🎯 本講內容

  • Ensemble 嘅概念:由 training data 建立一組 base classifiers,再將佢哋嘅 predictions 合併(majority vote / weighted majority vote)。
  • 點解 ensemble 有用:25 個 ε=0.35\varepsilon = 0.35 嘅 independent classifiers,majority vote 之後 error 跌到大約 0.06;同埋兩個 necessary conditions。
  • 三類 ensemble:manipulate data distribution(bagging、boosting)、manipulate input features(random forests)、manipulate class labels(error-correcting output coding)。
  • Bagging:bootstrap sampling,每個 instance 被抽中嘅機會 1−(1−1/n)n≈0.6321-(1-1/n)^n \approx 0.632;1-D decision stump 例子。
  • Boosting / AdaBoost:weights 點樣隨每個 round 改變,error rate εi\varepsilon_i、importance αi\alpha_i、weight update 同 weighted vote 嘅公式;1-D 例子。
  • Random Forest 同 Gradient Boosting(XGBoost) 嘅基本概念。

🧭 三種主要方法比較

BaggingBoosting (AdaBoost)Random Forest
點樣整唔同嘅 training setsBootstrap(uniform,sampling with replacement)按 weights 抽樣,weights 每 round 更新Bootstrap + 每個 node 隨機揀 pp 個 attributes
Base classifiers 之間互相獨立,可以 parallel 訓練有先後次序(sequential),後一個專攻前一個錯嘅 records獨立,而且 decorrelated
點樣合併Majority voteWeighted vote(weight =αi= \alpha_i)Majority vote
主要改善Variance(適合 unstable classifiers)Bias 同 variance 都可以改善,但較易受 noise 影響Variance,同時唔太影響 bias

1. 咩係 Ensemble Methods(Slide 2–3)

呢章講兩類 ensemble techniques:Bagging 同 Boosting,之後再介紹 Random Forest 同 Gradient Boosting。Slide 3 嘅定義:

  • Construct a set of classifiers from the training data
  • Predict class label of test records by combining the predictions made by multiple classifiers

即係唔再靠一個 classifier 做決定,而係訓練好多個 base classifiers,再用投票嘅方式決定最終 class label。

💡 直覺

好似問 25 個醫生同一個病症。每個醫生都有機會睇錯,但如果佢哋嘅錯誤唔係一齊犯(independent),而每個人又比亂估好,咁大多數人同意嘅答案通常會比任何一個醫生嘅答案更準。

2. 點解 Ensemble 有效(Slide 4–5)

Slide 4 嘅設定:

  • 有 25 個 base classifiers,每個嘅 error rate 都係 ε=0.35\varepsilon = 0.35
  • 用 majority vote 做 classification
  • 假設各個 classifiers 犯嘅錯誤 uncorrelated

Ensemble 要錯,即係 25 個入面最少 13 個錯(過半數)。每個 classifier 錯唔錯可以當係一次 Bernoulli trial,所以「錯嘅數目」跟 Binomial distribution B(25,0.35)B(25, 0.35):

P(ensemble wrong)=∑i=1325(25i)εi(1−ε)25−i=∑i=1325(25i)(0.35)i(0.65)25−i≈0.06P(\text{ensemble wrong}) = \sum_{i=13}^{25} \binom{25}{i} \varepsilon^{i} (1-\varepsilon)^{25-i} = \sum_{i=13}^{25} \binom{25}{i} (0.35)^{i} (0.65)^{25-i} \approx 0.06

由單一 classifier 嘅 0.35 跌到 ensemble 嘅 0.06,改善好明顯。

Slide 5 嘅圖顯示 25 個 base classifiers(errors uncorrelated)時,base classifier error(x 軸)同 ensemble classifier error(y 軸)嘅關係。紅色曲線係 ensemble,藍色虛線係 y=xy = x(即係同單一 classifier 一樣):

  • ε<0.5\varepsilon < 0.5:紅線喺虛線下面,ensemble 比單一 classifier 好,例如 ε=0.35\varepsilon = 0.35 時大約 0.06。
  • ε=0.5\varepsilon = 0.5:兩條線相交,ensemble 冇幫助。
  • ε>0.5\varepsilon > 0.5:紅線喺虛線上面,ensemble 反而更差,好快去到接近 1。

所以 Slide 5 列出 ensemble 比單一 base classifier 好嘅 necessary conditions:

  1. All base classifiers are independent of each other
  2. All base classifiers perform better than random guessing(binary classification 嘅 error rate <0.5< 0.5)
⚠️ 現實限制

實際上 base classifiers 用同一份 data 訓練,好難完全 independent。所以 bagging、boosting、random forest 嘅設計目標,就係用唔同方法令 base classifiers 盡量唔同,令佢哋嘅錯誤唔會集中喺同一啲 records。即使只係 slightly correlated,ensemble 通常都會有改善。

3. General Approach(Slide 6)

Slide 6 嘅流程圖分三步:

  1. Step 1: Create Multiple Data Sets:由 original training data DD 產生 D1,D2,…,Dt−1,DtD_1, D_2, \dots, D_{t-1}, D_t
  2. Step 2: Build Multiple Classifiers:每份 data set 訓練一個 classifier C1,…,CtC_1, \dots, C_t
  3. Step 3: Combine Classifiers:合併成 C∗C^*,using majority vote or weighted majority vote(weighted according to their accuracy or relevance)
🖼️ 圖片內容

Slide 6 嘅流程圖係圖片。上面三個 step 名同 D1…DtD_1 \dots D_t 呢類標籤係由圖入面嘅文字記錄抽出嚟,箭咀嘅細節就冇解釋。

4. Ensemble Methods 嘅種類(Slide 7)

做法例子點樣令 base classifiers 唔同
Manipulate data distributionBagging、Boosting每個 classifier 用唔同嘅 training sample(resampling)
Manipulate input featuresRandom forests每個 classifier(或者每個 split)只用部分 attributes
Manipulate class labelsError-correcting output coding (ECOC)將 multi-class 問題拆成好多個 binary 問題,每個 classifier 學一種 class 分組

Slide 只列出 ECOC 嘅名,冇詳細講。概念上,ECOC 會為每個 class 編一個 binary code,每個 base classifier 負責預測其中一個 bit,最後揀同預測 code 最接近嘅 class。

5. Bagging(Slide 8–9)

Bagging(Bootstrap aggregating):

  • Bootstrap sampling:Sampling with replacement。每個 bootstrap sample 嘅 size 同原本 training set 一樣(nn 個 records),所以有啲 records 會重複出現,有啲就冇被抽中。
  • Build classifier on each bootstrap sample。

每抽一次,某個 instance 冇被抽中嘅機會係 1−1/n1 - 1/n。抽 nn 次都冇被抽中嘅機會係 (1−1/n)n(1-1/n)^n。所以:

P(instance selected)=1−(1−1n)n  ⟶  1−1e≈0.632(n large)P(\text{instance selected}) = 1 - \left(1 - \frac{1}{n}\right)^{n} \;\longrightarrow\; 1 - \frac{1}{e} \approx 0.632 \quad (n \text{ large})

例如 n=10n = 10:1−0.910=1−0.3487≈0.6511 - 0.9^{10} = 1 - 0.3487 \approx 0.651;nn 越大就越接近 0.632。即係每個 bootstrap sample 大約包含原本 63.2% 嘅 distinct records,其餘大約 36.8% 冇被抽中。

Bagging Algorithm(Slide 9),用文字描述:

  1. 設 kk 為 bootstrap samples 嘅數目。
  2. For i=1i = 1 to kk:由 DD 用 sampling with replacement 抽一個 size 為 nn 嘅 bootstrap sample DiD_i,再用 DiD_i 訓練 base classifier CiC_i。
  3. Test record xx 嘅 prediction:C∗(x)=arg⁡max⁡y∑iδ(Ci(x)=y)C^*(x) = \arg\max_y \sum_i \delta(C_i(x) = y),即係 majority vote。(δ(⋅)\delta(\cdot) 條件成立時等於 1,否則等於 0)
🖼️ 圖片內容

Slide 9 嘅 algorithm 係一張圖片,冇抽到文字。上面係 bagging 嘅標準步驟(Tan 書 Algorithm 4.5),符號未必同 slide 完全一樣。

💡 Bagging 幫到乜

Bagging 主要減低 variance。對 unstable classifiers(例如 decision tree,data 少少改變 tree 就變好多)效果最好。對 stable classifiers(例如 k-NN 用大 kk、naive Bayes)就幫助有限。因為每個 sample 都係 uniform 抽樣,bagging 唔會特別針對某啲 records,所以比 boosting 較少 overfit noise。

6. Bagging Example(Slide 10–15)

1-dimensional data set(10 個 records):

x0.10.20.30.40.50.60.70.80.91.0
y111−1−1−1−1111

Base classifier 係 decision stump(decision tree of size 1,即係得一個 split 嘅 tree)。Decision rule:x≤kx \le k 就預測 ylefty_{left},x>kx > k 就預測 yrighty_{right}。Split point kk 用 entropy 揀。

⚠️ 重點

呢份 data 中間係 −1、兩邊係 1。任何一個 stump 都只可以切一刀,所以最好嘅 accuracy 都只係 70%(例如 x≤0.35⇒1x \le 0.35 \Rightarrow 1,否則 −1,會錯晒 0.8–1.0)。呢個例子想示範:好多個弱嘅 stumps 合埋,可以做到單一 stump 做唔到嘅結果。

Slide 11–13 顯示 10 個 bagging rounds 嘅 bootstrap samples 同每個 stump。例如 Round 1 抽到 x = 0.1, 0.2, 0.2, 0.3, 0.4, 0.4, 0.5, 0.6, 0.9, 0.9,學到 x≤0.35⇒y=1x \le 0.35 \Rightarrow y = 1,x>0.35⇒y=−1x > 0.35 \Rightarrow y = -1。Round 2 抽到 0.1, 0.2, 0.3, 0.4, 0.5, 0.5, 0.9, 1.0, 1.0, 1.0,學到 k=0.7k = 0.7,兩邊都預測 1。

Summary of training sets(Slide 14):

RoundSplit point kkLeft classRight class
10.351−1
20.711
30.351−1
40.31−1
50.351−1
60.75−11
70.75−11
80.75−11
90.75−11
100.0511

Slide 15:假設 test set 同 original data 一樣,用 majority vote。將每個 round 嘅 prediction(+1 / −1)加埋,睇個 sum 嘅正負號(Sign):

x0.10.20.30.40.50.60.70.80.91.0
Sum222−6−6−6−6222
Predicted (Sign)111−1−1−1−1111
True class111−1−1−1−1111

點計:x = 0.1 時,Round 1–5 同 10 都預測 1(+6),Round 6–9 預測 −1(−4),sum =6−4=2= 6 - 4 = 2。x = 0.4 時,只有 Round 2 同 10 預測 1(+2),其餘 8 個預測 −1,sum =2−8=−6= 2 - 8 = -6。x = 0.8 時,Round 2、6–10 預測 1(+6),Round 1、3、4、5 預測 −1(−4),sum =2= 2。

結果 ensemble 嘅 accuracy 係 100%,但單一 stump 最多只有 70%。Bagging 合併咗「左邊切一刀」同「右邊切一刀」兩類 stumps,等於 decision boundary 有兩個切點。

🖼️ 圖片內容

Slide 10–15 嘅表格同 stump 圖係圖片 / embedded objects。上面嘅數字係由呢啲圖片入面嘅文字記錄抽出嚟,Sum 一行亦用 Slide 14 嘅 summary 重新計過,結果一致。

7. Boosting(Slide 16–17)

Slide 16 嘅定義:

  • An iterative procedure to adaptively change distribution of training data by focusing more on previously misclassified records
  • Initially, all NN records are assigned equal weights(1/N1/N)
  • Unlike bagging, weights may change at the end of each boosting round

Slide 17:

  • Records that are wrongly classified will have their weights increased
  • Records that are classified correctly will have their weights decreased

Slide 17 嘅例子:Original data 係 record 1–10;Boosting Round 1 抽到 7, 3, 2, 8, 7, 9, 4, 10, 6, 3;Round 2 抽到 5, 4, 9, 4, 2, 5, 1, 7, 4, 2;Round 3 抽到 4, 4, 8, 10, 4, 5, 4, 6, 3, 4。Example 4 is hard to classify,佢嘅 weight 增加咗,所以之後嘅 rounds 越嚟越多機會抽中佢(Round 3 出現咗 4 次)。

💡 直覺

Boosting 好似溫書時做 past paper:第一次做錯嘅題目,下次溫書就花多啲時間喺嗰度。每個新 classifier 都專門處理之前做錯嘅 records,所以 base classifiers 一定要按次序訓練,唔可以好似 bagging 咁 parallel 做。

8. AdaBoost 公式(Slide 18–20)

Base classifiers:C1,C2,…,CTC_1, C_2, \dots, C_T。

Error rate(weighted):

εi=∑j=1Nwj δ(Ci(xj)≠yj)\varepsilon_i = \sum_{j=1}^{N} w_j \,\delta\big(C_i(x_j) \ne y_j\big)

即係將所有被 CiC_i 分錯嘅 records 嘅 weights 加埋。δ(⋅)\delta(\cdot) 條件成立時等於 1,否則等於 0。

Importance of a classifier:

αi=12ln⁡ ⁣(1−εiεi)\alpha_i = \frac{1}{2} \ln\!\left(\frac{1 - \varepsilon_i}{\varepsilon_i}\right)

Slide 18 嘅圖係 ln⁡((1−ε)/ε)\ln((1-\varepsilon)/\varepsilon) 對 ε\varepsilon 嘅曲線:

  • ε\varepsilon 接近 0 時,α\alpha 好大(準嘅 classifier 投票 weight 大)。
  • ε=0.5\varepsilon = 0.5 時,α=0\alpha = 0(同亂估一樣,冇投票權)。
  • ε>0.5\varepsilon > 0.5 時,α<0\alpha < 0。所以 AdaBoost 會避免呢種情況(見下面 weight update)。
🖼️ 圖片內容

Slide 18 嘅 αi\alpha_i 公式係 MathType 圖片。圖入面嘅文字記錄有 α\alpha、ε\varepsilon、ln、1 同 2,同上面嘅標準 AdaBoost 公式吻合,而且下面例子嘅 α\alpha 值都用呢條公式計到。

Weight update(Slide 19):

wj(i+1)=wj(i)Zi×{exp⁡(−αi)if Ci(xj)=yjexp⁡(αi)if Ci(xj)≠yjw_j^{(i+1)} = \frac{w_j^{(i)}}{Z_i} \times \begin{cases} \exp(-\alpha_i) & \text{if } C_i(x_j) = y_j \\ \exp(\alpha_i) & \text{if } C_i(x_j) \ne y_j \end{cases}

ZiZ_i 係 normalization factor,令新 weights 加埋等於 1。分啱嘅乘 e−αi<1e^{-\alpha_i} < 1(weight 減少),分錯嘅乘 eαi>1e^{\alpha_i} > 1(weight 增加)。

如果中途任何一個 round 嘅 error rate 高過 50%,weights 會 reset 返 1/n1/n,再重新做 resampling。

Classification(Slide 19):

C∗(x)=arg⁡max⁡y∑i=1Tαi δ(Ci(x)=y)C^*(x) = \arg\max_{y} \sum_{i=1}^{T} \alpha_i \,\delta\big(C_i(x) = y\big)

即係 weighted majority vote:每個 classifier 嘅一票,價值等於佢嘅 αi\alpha_i。對 y∈{−1,1}y \in \{-1, 1\},等同計 ∑iαiCi(x)\sum_i \alpha_i C_i(x) 嘅正負號。

🖼️ 圖片內容

Slide 20 嘅 AdaBoost Algorithm 係一張圖片,冇抽到文字。流程同上面嘅公式一致:(1) weights 初始化為 1/N1/N;(2) 每個 round 按 weights 抽 sample、訓練 CiC_i、計 εi\varepsilon_i;如果 εi>0.5\varepsilon_i > 0.5,就 reset weights 再嚟過;(3) 計 αi\alpha_i,更新 weights;(4) 用 weighted vote 做 prediction。

9. AdaBoost Example(Slide 21–23)

用返同 bagging example 一樣嘅 1-D data 同 decision stump。Slide 22 顯示頭 3 個 boosting rounds 嘅 training sets:

  • Round 1:0.1, 0.4, 0.5, 0.6, 0.6, 0.7, 0.7, 0.7, 0.8, 1.0(uniform weights 抽樣)
  • Round 2:0.1, 0.1, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3(集中喺 Round 1 分錯嘅 0.1–0.3)
  • Round 3:0.2, 0.2, 0.4, 0.4, 0.4, 0.4, 0.5, 0.6, 0.6, 0.7(集中喺 Round 2 分錯嘅 0.4–0.7)

Summary(Slide 22):

RoundSplit pointLeft classRight classα\alpha
10.75−111.738
20.05112.7784
30.31−14.1195

Weights(Slide 23):

Roundx = 0.1–0.3x = 0.4–0.7x = 0.8–1.0
10.10.10.1
20.3110.010.01
30.0290.2280.009

逐步解釋:

  1. Round 1:stump x≤0.75⇒−1x \le 0.75 \Rightarrow -1,否則 1。喺 original data 上分錯 x = 0.1, 0.2, 0.3(真 class 係 1)。所以呢三個嘅 weight 由 0.1 升到 0.311,其餘 7 個跌到 0.01(3×0.311+7×0.01≈13 \times 0.311 + 7 \times 0.01 \approx 1)。
  2. Round 2:sample 幾乎全部係 0.1–0.3(class 1),學到「全部都係 1」(split 0.05,兩邊都係 1)。分錯 x = 0.4–0.7,所以佢哋嘅 weight 升到 0.228。
  3. Round 3:sample 集中喺 0.4–0.7,學到 x≤0.3⇒1x \le 0.3 \Rightarrow 1,否則 −1,分錯 x = 0.8–1.0。
⚠️ 數字同公式嘅細節

例子入面嘅 α\alpha 值,係用 εi=1N∑jwj δ(Ci(xj)≠yj)\varepsilon_i = \frac{1}{N}\sum_j w_j\,\delta(C_i(x_j) \ne y_j) 計出嚟(Tan 書嘅版本,比 Slide 18 多咗一個 1N\frac{1}{N})。例如 Round 1 分錯嘅 weights 加埋係 0.3,ε1=0.3/10=0.03\varepsilon_1 = 0.3/10 = 0.03,α1=12ln⁡(0.97/0.03)≈1.738\alpha_1 = \frac{1}{2}\ln(0.97/0.03) \approx 1.738。如果照 Slide 18 唔除 NN,ε1=0.3\varepsilon_1 = 0.3,α1=12ln⁡(0.7/0.3)≈0.424\alpha_1 = \frac{1}{2}\ln(0.7/0.3) \approx 0.424。兩個版本嘅最終 prediction 都一樣,但如果要計數,就要跟 lecturer 用嘅版本。另外 Round 3 嘅 0.009 應該係 0.0009,因為 weights 加埋要等於 1(3×0.029+4×0.228+3×0.0009≈13 \times 0.029 + 4 \times 0.228 + 3 \times 0.0009 \approx 1)。

Classification(Slide 23):計 ∑iαiCi(x)\sum_i \alpha_i C_i(x):

xRound 1 (α=1.738\alpha=1.738)Round 2 (α=2.778\alpha=2.778)Round 3 (α=4.120\alpha=4.120)SumSignTrue
0.1–0.3−111−1.738+2.778+4.120=5.16-1.738 + 2.778 + 4.120 = 5.1611
0.4–0.7−11−1−1.738+2.778−4.120≈−3.08-1.738 + 2.778 - 4.120 \approx -3.08−1−1
0.8–1.011−11.738+2.778−4.120≈0.401.738 + 2.778 - 4.120 \approx 0.4011

(Slide 嘅 Sum 係 5.16、−3.08、0.397。)只用 3 個 stumps,AdaBoost 已經做到 100% accuracy。留意 Round 3 嘅 α\alpha 最大,但佢喺 x = 0.8–1.0 投錯票,仍然被 Round 1 同 2 加埋嘅票蓋過,所以結果正確。

# 參考用 scikit-learn 示範(唔係 lecture code)
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import BaggingClassifier, AdaBoostClassifier

X = np.arange(1, 11).reshape(-1, 1) / 10
y = np.array([1, 1, 1, -1, -1, -1, -1, 1, 1, 1])
stump = DecisionTreeClassifier(max_depth=1)   # decision stump

print(stump.fit(X, y).score(X, y))            # 單一 stump:0.7
bag = BaggingClassifier(stump, n_estimators=10, random_state=1).fit(X, y)
ada = AdaBoostClassifier(stump, n_estimators=3).fit(X, y)
print(bag.score(X, y), ada.score(X, y))       # 通常可以去到 1.0(bagging 視乎 random_state)

10. Random Forest(Slide 24–25)

Construct an ensemble of decision trees by manipulating training set as well as features:

  • Use bootstrap sample to train every decision tree(similar to Bagging)
  • Tree induction:At every internal node, randomly sample pp attributes for selecting split criterion
  • Repeat until all leaves are pure(unpruned tree)

Characteristics(Slide 25):

  • Base classifiers are unpruned trees and hence are unstable classifiers(low bias、high variance)
  • Base classifiers are decorrelated(due to randomization in training set as well as features)
  • Random forests reduce variance without negatively impacting the bias
  • Selection of hyper-parameter pp:
    • Small values ensure lack of correlation(trees 之間更唔同)
    • High value promotes strong base classifiers(每棵 tree 更準)
    • Common default choices:d\sqrt{d}、log⁡2(d+1)\log_2(d+1)(dd 係 attributes 嘅總數)
⚠️ Slide 用字

Slide 25 寫 "pruned trees"、"lack of correction"、"reduce negatively impacting the bias"。根據 Slide 24(unpruned tree)同 Tan 書,原意應該係 unpruned trees、lack of correlation、reduce variance without negatively impacting the bias。上面已經用返原意。

💡 點解要隨機揀 attributes

如果有一個 attribute 特別強,bagging 嘅每棵 tree 都會用佢做 root split,咁 trees 就好相似(highly correlated),平均之後 variance 減得唔多。Random forest 每個 node 只畀 tree 睇 pp 個 attributes,強 attribute 唔係次次都揀得到,trees 就會唔同,即係滿足第 2 節嘅 independence 條件。例如 d=16d = 16:16=4\sqrt{16} = 4,log⁡217≈4.09\log_2 17 \approx 4.09,即係每個 node 隨機睇大約 4 個 attributes。

11. Gradient Boosting(Slide 26)

  • Constructs a series of models(sequential,同 boosting 一樣)
  • Models can be any predictive model that has a differentiable loss function;commonly, trees are the chosen model
  • XGBoost(extreme gradient boosting)is a popular package because of its impressive performance
  • Boosting can be viewed as optimizing the loss function by iterative functional gradient descent
  • Implementations are available in Python, R, Matlab, and more
💡 直覺

AdaBoost 用「加大分錯 records 嘅 weight」嚟改善下一個 model。Gradient boosting 就直接叫下一棵 tree 去 fit 而家 model 嘅錯誤(residuals / loss 嘅 negative gradient)。例如 regression:第一棵 tree 預測 70,真值係 80,第二棵 tree 就學個 +10;兩棵加埋就更接近真值。每一步都令 loss 下降,好似 gradient descent,不過係喺「function 空間」入面做。

# 參考用(唔係 lecture code)
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
rf  = RandomForestClassifier(n_estimators=100, max_features="sqrt")  # p = sqrt(d)
gbm = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, max_depth=3)
# XGBoost:pip install xgboost;from xgboost import XGBClassifier

自我測驗 Self-test

Q1. Ensemble method 嘅定義係咩?Ensemble 要比單一 base classifier 好,需要咩 necessary conditions?

由 training data 建立一組 classifiers,再合併佢哋嘅 predictions 嚟預測 test records 嘅 class label。條件:(1) base classifiers 之間互相 independent;(2) 每個 base classifier 都比 random guessing 好(binary classification 嘅 error rate <0.5< 0.5)。

Q2. 有 5 個 independent base classifiers,每個 ε=0.2\varepsilon = 0.2,用 majority vote。Ensemble 嘅 error rate 係幾多?

最少 3 個錯先會錯:(53)(0.2)3(0.8)2+(54)(0.2)4(0.8)+(0.2)5=0.0512+0.0064+0.00032≈0.058\binom{5}{3}(0.2)^3(0.8)^2 + \binom{5}{4}(0.2)^4(0.8) + (0.2)^5 = 0.0512 + 0.0064 + 0.00032 \approx 0.058,比 0.2 細好多。

Q3. 如果每個 base classifier 嘅 ε=0.6\varepsilon = 0.6,用 ensemble 會點?

Ensemble error 會比 0.6 更高(Slide 5 曲線喺 ε>0.5\varepsilon > 0.5 時喺 y=xy = x 上面),因為大多數 classifiers 都會錯,majority vote 會放大錯誤。

Q4. Training set 有 n=5n = 5 個 records。某個 record 喺一個 bootstrap sample 入面出現最少一次嘅機會係幾多?nn 好大時呢?

1−(1−1/5)5=1−0.85=1−0.32768≈0.6721 - (1 - 1/5)^5 = 1 - 0.8^5 = 1 - 0.32768 \approx 0.672。nn 好大時趨向 1−1/e≈0.6321 - 1/e \approx 0.632。

Q5. Ensemble methods 分邊三類?每類舉一個例子。

Manipulate data distribution(bagging、boosting);manipulate input features(random forests);manipulate class labels(error-correcting output coding)。

Q6. Bagging 同 boosting 有咩主要分別?

Bagging:每個 bootstrap sample 都係 uniform 抽樣,classifiers 互相獨立(可以 parallel),用 majority vote,主要減 variance。Boosting:iterative,每個 round 按 weights 抽樣,分錯嘅 records weight 增加,classifiers 有先後次序,用 weighted vote(αi\alpha_i),較易受 noise / outliers 影響。

Q7. 某個 AdaBoost round 嘅 εi=0.1\varepsilon_i = 0.1。計 αi\alpha_i。分啱同分錯嘅 records 嘅 weight(normalize 之前)會乘咩數?

αi=12ln⁡(0.9/0.1)=12ln⁡9≈1.099\alpha_i = \frac{1}{2}\ln(0.9/0.1) = \frac{1}{2}\ln 9 \approx 1.099。分啱乘 e−1.099≈0.333e^{-1.099} \approx 0.333,分錯乘 e1.099≈3e^{1.099} \approx 3,之後再除以 ZiZ_i,令 weights 加埋等於 1。

Q8. 點解 AdaBoost 要求每個 round 嘅 εi≤0.5\varepsilon_i \le 0.5?超過咗會點做?

εi=0.5\varepsilon_i = 0.5 時 αi=0\alpha_i = 0,εi>0.5\varepsilon_i > 0.5 時 αi<0\alpha_i < 0,即係 classifier 比亂估仲差,亦違反 ensemble 嘅 necessary condition。Slide 19:如果 error rate 高過 50%,weights 會 reset 返 1/n1/n,再重新 resampling。

Q9. 喺 AdaBoost example 入面,3 個 stumps 嘅 α\alpha 係 1.738、2.778、4.120。x = 0.8 時三個 stumps 分別預測 1、1、−1。Ensemble 預測咩?

1.738+2.778−4.120=0.396>01.738 + 2.778 - 4.120 = 0.396 > 0,所以預測 1(正確)。雖然 α\alpha 最大嘅 Round 3 投 −1,但另外兩個加埋嘅 weight 更大。

Q10. Random forest 同 bagging 有咩分別?Hyper-parameter pp 大細有咩 trade-off?

兩者都用 bootstrap samples,但 random forest 喺每個 internal node 只隨機揀 pp 個 attributes 嚟揀 split,而且 tree 唔做 pruning,所以 trees 更 decorrelated。pp 細:trees 之間 correlation 低,但每棵 tree 較弱;pp 大:每棵 tree 較強,但 trees 較相似(p=dp = d 就變返 bagging)。常用 d\sqrt{d} 或 log⁡2(d+1)\log_2(d+1)。

一頁總結 Cheat sheet

📋 速查(非 COM6004 考試範圍)
  • Ensemble:一組 base classifiers + 合併 predictions(majority / weighted majority vote)。
  • 25 個 independent classifiers、ε=0.35\varepsilon = 0.35:∑i=1325(25i)εi(1−ε)25−i≈0.06\sum_{i=13}^{25}\binom{25}{i}\varepsilon^i(1-\varepsilon)^{25-i} \approx 0.06。
  • Necessary conditions:base classifiers independent;每個都 better than random(ε<0.5\varepsilon < 0.5)。ε>0.5\varepsilon > 0.5 嘅話 ensemble 會更差。
  • 三類:data distribution(bagging、boosting)/ input features(random forest)/ class labels(ECOC)。
  • Bagging:bootstrap(with replacement,size nn),P(selected)=1−(1−1/n)n≈0.632P(\text{selected}) = 1-(1-1/n)^n \approx 0.632;majority vote;減 variance,適合 unstable classifiers。
  • Boosting:weights 初始 1/N1/N;分錯 ↑、分啱 ↓;sequential。
  • AdaBoost:εi=∑jwj δ(Ci(xj)≠yj)\varepsilon_i = \sum_j w_j\,\delta(C_i(x_j) \ne y_j);αi=12ln⁡1−εiεi\alpha_i = \frac{1}{2}\ln\frac{1-\varepsilon_i}{\varepsilon_i};wj(i+1)=wj(i)Zie∓αiw_j^{(i+1)} = \frac{w_j^{(i)}}{Z_i}e^{\mp\alpha_i}(分啱 −-、分錯 ++);εi>0.5\varepsilon_i > 0.5 就 reset 做 1/n1/n;C∗(x)=arg⁡max⁡y∑iαi δ(Ci(x)=y)C^*(x) = \arg\max_y \sum_i \alpha_i\,\delta(C_i(x) = y)。
  • 1-D 例子:單一 stump 最多 70%;bagging(10 rounds)同 AdaBoost(3 rounds)都做到 100%。
  • Random Forest:bootstrap + 每個 node 隨機揀 pp 個 attributes + unpruned trees;decorrelated;減 variance 而唔影響 bias;p=dp = \sqrt{d} 或 log⁡2(d+1)\log_2(d+1)。
  • Gradient Boosting:一連串 models(通常係 trees),differentiable loss,functional gradient descent;XGBoost。