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 考試範圍只包括 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 個 嘅 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-D decision stump 例子。
- Boosting / AdaBoost:weights 點樣隨每個 round 改變,error rate 、importance 、weight update 同 weighted vote 嘅公式;1-D 例子。
- Random Forest 同 Gradient Boosting(XGBoost) 嘅基本概念。
🧭 三種主要方法比較
| Bagging | Boosting (AdaBoost) | Random Forest | |
|---|---|---|---|
| 點樣整唔同嘅 training sets | Bootstrap(uniform,sampling with replacement) | 按 weights 抽樣,weights 每 round 更新 | Bootstrap + 每個 node 隨機揀 個 attributes |
| Base classifiers 之間 | 互相獨立,可以 parallel 訓練 | 有先後次序(sequential),後一個專攻前一個錯嘅 records | 獨立,而且 decorrelated |
| 點樣合併 | Majority vote | Weighted vote(weight ) | 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 都係
- 用 majority vote 做 classification
- 假設各個 classifiers 犯嘅錯誤 uncorrelated
Ensemble 要錯,即係 25 個入面最少 13 個錯(過半數)。每個 classifier 錯唔錯可以當係一次 Bernoulli trial,所以「錯嘅數目」跟 Binomial distribution :
由單一 classifier 嘅 0.35 跌到 ensemble 嘅 0.06,改善好明顯。
呢個計法就係 COM6005 Module 2 嘅 Binomial distribution: 次 trials,success(呢度指「錯」)probability ,求 。,遠低於 13,所以 好細。
Slide 5 嘅圖顯示 25 個 base classifiers(errors uncorrelated)時,base classifier error(x 軸)同 ensemble classifier error(y 軸)嘅關係。紅色曲線係 ensemble,藍色虛線係 (即係同單一 classifier 一樣):
- :紅線喺虛線下面,ensemble 比單一 classifier 好,例如 時大約 0.06。
- :兩條線相交,ensemble 冇幫助。
- :紅線喺虛線上面,ensemble 反而更差,好快去到接近 1。
所以 Slide 5 列出 ensemble 比單一 base classifier 好嘅 necessary conditions:
- All base classifiers are independent of each other
- All base classifiers perform better than random guessing(binary classification 嘅 error rate )
實際上 base classifiers 用同一份 data 訓練,好難完全 independent。所以 bagging、boosting、random forest 嘅設計目標,就係用唔同方法令 base classifiers 盡量唔同,令佢哋嘅錯誤唔會集中喺同一啲 records。即使只係 slightly correlated,ensemble 通常都會有改善。
3. General Approach(Slide 6)
Slide 6 嘅流程圖分三步:
- Step 1: Create Multiple Data Sets:由 original training data 產生
- Step 2: Build Multiple Classifiers:每份 data set 訓練一個 classifier
- Step 3: Combine Classifiers:合併成 ,using majority vote or weighted majority vote(weighted according to their accuracy or relevance)
Slide 6 嘅流程圖係圖片。上面三個 step 名同 呢類標籤係由圖入面嘅文字記錄抽出嚟,箭咀嘅細節就冇解釋。
4. Ensemble Methods 嘅種類(Slide 7)
| 做法 | 例子 | 點樣令 base classifiers 唔同 |
|---|---|---|
| Manipulate data distribution | Bagging、Boosting | 每個 classifier 用唔同嘅 training sample(resampling) |
| Manipulate input features | Random forests | 每個 classifier(或者每個 split)只用部分 attributes |
| Manipulate class labels | Error-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 一樣( 個 records),所以有啲 records 會重複出現,有啲就冇被抽中。
- Build classifier on each bootstrap sample。
每抽一次,某個 instance 冇被抽中嘅機會係 。抽 次都冇被抽中嘅機會係 。所以:
例如 :; 越大就越接近 0.632。即係每個 bootstrap sample 大約包含原本 63.2% 嘅 distinct records,其餘大約 36.8% 冇被抽中。
Chapter 4 講 model evaluation 時提過 bootstrap(.632 bootstrap)。冇被抽中嘅 records 叫 out-of-bag (OOB) records,可以當 test set 用。COM6005 講 sampling with / without replacement 嘅分別,亦適用喺呢度。
Bagging Algorithm(Slide 9),用文字描述:
- 設 為 bootstrap samples 嘅數目。
- For to :由 用 sampling with replacement 抽一個 size 為 嘅 bootstrap sample ,再用 訓練 base classifier 。
- Test record 嘅 prediction:,即係 majority vote。( 條件成立時等於 1,否則等於 0)
Slide 9 嘅 algorithm 係一張圖片,冇抽到文字。上面係 bagging 嘅標準步驟(Tan 書 Algorithm 4.5),符號未必同 slide 完全一樣。
Bagging 主要減低 variance。對 unstable classifiers(例如 decision tree,data 少少改變 tree 就變好多)效果最好。對 stable classifiers(例如 k-NN 用大 、naive Bayes)就幫助有限。因為每個 sample 都係 uniform 抽樣,bagging 唔會特別針對某啲 records,所以比 boosting 較少 overfit noise。
6. Bagging Example(Slide 10–15)
1-dimensional data set(10 個 records):
| x | 0.1 | 0.2 | 0.3 | 0.4 | 0.5 | 0.6 | 0.7 | 0.8 | 0.9 | 1.0 |
|---|---|---|---|---|---|---|---|---|---|---|
| y | 1 | 1 | 1 | −1 | −1 | −1 | −1 | 1 | 1 | 1 |
Base classifier 係 decision stump(decision tree of size 1,即係得一個 split 嘅 tree)。Decision rule: 就預測 , 就預測 。Split point 用 entropy 揀。
呢份 data 中間係 −1、兩邊係 1。任何一個 stump 都只可以切一刀,所以最好嘅 accuracy 都只係 70%(例如 ,否則 −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,學到 ,。Round 2 抽到 0.1, 0.2, 0.3, 0.4, 0.5, 0.5, 0.9, 1.0, 1.0, 1.0,學到 ,兩邊都預測 1。
Summary of training sets(Slide 14):
| Round | Split point | Left class | Right class |
|---|---|---|---|
| 1 | 0.35 | 1 | −1 |
| 2 | 0.7 | 1 | 1 |
| 3 | 0.35 | 1 | −1 |
| 4 | 0.3 | 1 | −1 |
| 5 | 0.35 | 1 | −1 |
| 6 | 0.75 | −1 | 1 |
| 7 | 0.75 | −1 | 1 |
| 8 | 0.75 | −1 | 1 |
| 9 | 0.75 | −1 | 1 |
| 10 | 0.05 | 1 | 1 |
Slide 15:假設 test set 同 original data 一樣,用 majority vote。將每個 round 嘅 prediction(+1 / −1)加埋,睇個 sum 嘅正負號(Sign):
| x | 0.1 | 0.2 | 0.3 | 0.4 | 0.5 | 0.6 | 0.7 | 0.8 | 0.9 | 1.0 |
|---|---|---|---|---|---|---|---|---|---|---|
| Sum | 2 | 2 | 2 | −6 | −6 | −6 | −6 | 2 | 2 | 2 |
| Predicted (Sign) | 1 | 1 | 1 | −1 | −1 | −1 | −1 | 1 | 1 | 1 |
| True class | 1 | 1 | 1 | −1 | −1 | −1 | −1 | 1 | 1 | 1 |
點計:x = 0.1 時,Round 1–5 同 10 都預測 1(+6),Round 6–9 預測 −1(−4),sum 。x = 0.4 時,只有 Round 2 同 10 預測 1(+2),其餘 8 個預測 −1,sum 。x = 0.8 時,Round 2、6–10 預測 1(+6),Round 1、3、4、5 預測 −1(−4),sum 。
結果 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 records are assigned equal weights()
- 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:。
Error rate(weighted):
即係將所有被 分錯嘅 records 嘅 weights 加埋。 條件成立時等於 1,否則等於 0。
Importance of a classifier:
Slide 18 嘅圖係 對 嘅曲線:
- 接近 0 時, 好大(準嘅 classifier 投票 weight 大)。
- 時,(同亂估一樣,冇投票權)。
- 時,。所以 AdaBoost 會避免呢種情況(見下面 weight update)。
Slide 18 嘅 公式係 MathType 圖片。圖入面嘅文字記錄有 、、ln、1 同 2,同上面嘅標準 AdaBoost 公式吻合,而且下面例子嘅 值都用呢條公式計到。
Weight update(Slide 19):
係 normalization factor,令新 weights 加埋等於 1。分啱嘅乘 (weight 減少),分錯嘅乘 (weight 增加)。
如果中途任何一個 round 嘅 error rate 高過 50%,weights 會 reset 返 ,再重新做 resampling。
Classification(Slide 19):
即係 weighted majority vote:每個 classifier 嘅一票,價值等於佢嘅 。對 ,等同計 嘅正負號。
Slide 20 嘅 AdaBoost Algorithm 係一張圖片,冇抽到文字。流程同上面嘅公式一致:(1) weights 初始化為 ;(2) 每個 round 按 weights 抽 sample、訓練 、計 ;如果 ,就 reset weights 再嚟過;(3) 計 ,更新 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):
| Round | Split point | Left class | Right class | |
|---|---|---|---|---|
| 1 | 0.75 | −1 | 1 | 1.738 |
| 2 | 0.05 | 1 | 1 | 2.7784 |
| 3 | 0.3 | 1 | −1 | 4.1195 |
Weights(Slide 23):
| Round | x = 0.1–0.3 | x = 0.4–0.7 | x = 0.8–1.0 |
|---|---|---|---|
| 1 | 0.1 | 0.1 | 0.1 |
| 2 | 0.311 | 0.01 | 0.01 |
| 3 | 0.029 | 0.228 | 0.009 |
逐步解釋:
- Round 1:stump ,否則 1。喺 original data 上分錯 x = 0.1, 0.2, 0.3(真 class 係 1)。所以呢三個嘅 weight 由 0.1 升到 0.311,其餘 7 個跌到 0.01()。
- Round 2:sample 幾乎全部係 0.1–0.3(class 1),學到「全部都係 1」(split 0.05,兩邊都係 1)。分錯 x = 0.4–0.7,所以佢哋嘅 weight 升到 0.228。
- Round 3:sample 集中喺 0.4–0.7,學到 ,否則 −1,分錯 x = 0.8–1.0。
例子入面嘅 值,係用 計出嚟(Tan 書嘅版本,比 Slide 18 多咗一個 )。例如 Round 1 分錯嘅 weights 加埋係 0.3,,。如果照 Slide 18 唔除 ,,。兩個版本嘅最終 prediction 都一樣,但如果要計數,就要跟 lecturer 用嘅版本。另外 Round 3 嘅 0.009 應該係 0.0009,因為 weights 加埋要等於 1()。
Classification(Slide 23):計 :
| x | Round 1 () | Round 2 () | Round 3 () | Sum | Sign | True |
|---|---|---|---|---|---|---|
| 0.1–0.3 | −1 | 1 | 1 | 1 | 1 | |
| 0.4–0.7 | −1 | 1 | −1 | −1 | −1 | |
| 0.8–1.0 | 1 | 1 | −1 | 1 | 1 |
(Slide 嘅 Sum 係 5.16、−3.08、0.397。)只用 3 個 stumps,AdaBoost 已經做到 100% accuracy。留意 Round 3 嘅 最大,但佢喺 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 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 :
- Small values ensure lack of correlation(trees 之間更唔同)
- High value promotes strong base classifiers(每棵 tree 更準)
- Common default choices:、( 係 attributes 嘅總數)
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。上面已經用返原意。
如果有一個 attribute 特別強,bagging 嘅每棵 tree 都會用佢做 root split,咁 trees 就好相似(highly correlated),平均之後 variance 減得唔多。Random forest 每個 node 只畀 tree 睇 個 attributes,強 attribute 唔係次次都揀得到,trees 就會唔同,即係滿足第 2 節嘅 independence 條件。例如 :,,即係每個 node 隨機睇大約 4 個 attributes。
Chapter 4 講 decision tree 會 overfit,所以要 pruning。Random forest 唔做 pruning,每棵 tree 都 overfit,但靠大量 decorrelated trees 平均,抵消 high variance。呢度用 entropy / Gini 揀 split 嘅方法同 Chapter 4 完全一樣。
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 )。
Q2. 有 5 個 independent base classifiers,每個 ,用 majority vote。Ensemble 嘅 error rate 係幾多?
最少 3 個錯先會錯:,比 0.2 細好多。
Q3. 如果每個 base classifier 嘅 ,用 ensemble 會點?
Ensemble error 會比 0.6 更高(Slide 5 曲線喺 時喺 上面),因為大多數 classifiers 都會錯,majority vote 會放大錯誤。
Q4. Training set 有 個 records。某個 record 喺一個 bootstrap sample 入面出現最少一次嘅機會係幾多? 好大時呢?
。 好大時趨向 。
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(),較易受 noise / outliers 影響。
Q7. 某個 AdaBoost round 嘅 。計 。分啱同分錯嘅 records 嘅 weight(normalize 之前)會乘咩數?
。分啱乘 ,分錯乘 ,之後再除以 ,令 weights 加埋等於 1。
Q8. 點解 AdaBoost 要求每個 round 嘅 ?超過咗會點做?
時 , 時 ,即係 classifier 比亂估仲差,亦違反 ensemble 嘅 necessary condition。Slide 19:如果 error rate 高過 50%,weights 會 reset 返 ,再重新 resampling。
Q9. 喺 AdaBoost example 入面,3 個 stumps 嘅 係 1.738、2.778、4.120。x = 0.8 時三個 stumps 分別預測 1、1、−1。Ensemble 預測咩?
,所以預測 1(正確)。雖然 最大嘅 Round 3 投 −1,但另外兩個加埋嘅 weight 更大。
Q10. Random forest 同 bagging 有咩分別?Hyper-parameter 大細有咩 trade-off?
兩者都用 bootstrap samples,但 random forest 喺每個 internal node 只隨機揀 個 attributes 嚟揀 split,而且 tree 唔做 pruning,所以 trees 更 decorrelated。 細:trees 之間 correlation 低,但每棵 tree 較弱; 大:每棵 tree 較強,但 trees 較相似( 就變返 bagging)。常用 或 。
一頁總結 Cheat sheet
- Ensemble:一組 base classifiers + 合併 predictions(majority / weighted majority vote)。
- 25 個 independent classifiers、:。
- Necessary conditions:base classifiers independent;每個都 better than random()。 嘅話 ensemble 會更差。
- 三類:data distribution(bagging、boosting)/ input features(random forest)/ class labels(ECOC)。
- Bagging:bootstrap(with replacement,size ),;majority vote;減 variance,適合 unstable classifiers。
- Boosting:weights 初始 ;分錯 ↑、分啱 ↓;sequential。
- AdaBoost:;;(分啱 、分錯 ); 就 reset 做 ;。
- 1-D 例子:單一 stump 最多 70%;bagging(10 rounds)同 AdaBoost(3 rounds)都做到 100%。
- Random Forest:bootstrap + 每個 node 隨機揀 個 attributes + unpruned trees;decorrelated;減 variance 而唔影響 bias; 或 。
- Gradient Boosting:一連串 models(通常係 trees),differentiable loss,functional gradient descent;XGBoost。