機器學習面試考點 (1)— Decision Tree & Ensemble Method

吳定群
8 min readAug 9, 2022

--

架構

Decision Tree

  • Basic Concept — Q1, Q2
  • Purity, Impurity Measurement — Q3
  • Overfitting & Purning — Q4
  • Information Gain & Entropy — Q5
  • Gini Index — Q6

Ensemble Method

  • Weak Learner — Q1
  • Random Forest — Q2, Q3
  • Bagging vs Boosting vs Stacking — Q4
  • 不同模型比較 — Q5, 未來更新
  • AdaBoost, GradientBoost, XGBoost — 未來更新

Decision Tree

Q1 What are some advantages of using Decision Trees?

  1. 可以解決分類與回歸問題
  2. if-else結構具有很好的解釋性
  3. 可處理數值型和類別型資料
  4. 不需要做feature scaling

更多資料

Q2 Explain the structure of a Decision Tree

Decision Tree 由內部節點、葉子節點、還有branch組成

  1. 內部節點:代表一個test feature
  2. 外部節點(葉子):代表一個class(我們要預測的class,像是硬幣正反面)
  3. branch : feature的不同可能性
圖片:Tommy Huang‘s Medium

更多資料

Q3 What type of node is considered Pure?

簡單來說,若leaf node中全部的資料都是同一個class,代表純度相當高。衡量純度(不純度)有兩個方法如下:

  1. Entropy : 當entropy為0,代表資料的比例為 0或1,全都是class1或全都是class2,此時純度非常高。
公式

2.Gini Index : 當Gini index為0,代表純度相當高。公式為1-p²-q²

Q4 How would you deal with an Overfitted Decision Tree?

我們可以對樹做pruning,其中又分為兩種:

  1. Pre-Pruning : 限制樹的高度、切下去的information gain < 閾值、sample< 閾值..等,可以理解為early stoping
  2. Post-Pruning : 先建立完整的樹,再來把不必要的子樹砍掉,較為有名的方法是Cost Complexity Pruning,另外還有Reduced-Error Pruning、Pesimistic-Error Pruning..等方法。

Cost Complexity Pruning 簡單來說就是加入一個懲罰項,葉子越多懲罰越高。α是參數,決定了penalty的大小,可由validation求得。R(T)為Sum Squared of Error。

Edward Krueger’s Medium

Pruning YT Post-Pruning

Q5 What is Entropy & Information Gain

當我們要建一棵決策樹時,決定要選哪個feature把資料切開是很重要的課題,我們希望切開後的兩堆資料「純度很高」,Entropy=亂度、不純度,也可以想成是data有多大的變異(Variance),C代表class總數

假設現在有一個藍球b,兩個綠球g,三個紅球r,p代表class佔的比例。此時的Entropy為

Victor Zhou’s site

Information Gain說的就是這一刀切下去的品質,計算方式是用切之前的E(Before) -E(After),而E(After)=E(left branch)+E(right branch)

更多資訊

Q6 What is Gini Index(Gini Impurity)?

Gini Index是另一種衡量feature好壞的工具。我們可以將它理解為隨機抽一個資料點,他被分錯的機率,就是Gini Index。

舉例來說,假設現在有一個袋子裡面有五個綠球g五個紅球r(兩個class),那麼隨機抽一個,猜錯它顏色的機率是0.5 (叉叉相加0.25+0.25),所以Gini Index=0.5。

Victor Zhou’s site

所以如果一個set裡面只有一個class,那麼猜錯的機率自然就是0,Gini Index=0。

更多資訊

Ensemble Method

Q1 What are Weak Learners (ex:Weak Classifier)? What is Ensemble Method?

弱分類器代表其accuracy只比隨便亂猜好一點的模型。將很多弱分類器組合在一起變成一個強的模型,就叫Ensemble Method。

常見的弱分類器有:Decision Stump(只有root的決策樹)

更多資訊

Q2 How is a Random Forest related to Decision Trees?

將很多棵樹合在一起就會變成Random Forest,他解決了單一一棵樹容易Overfitting的問題。實際生成的方式如下:

  1. Boostrap: 用抽後放回的方式,訓練出許多有些微差異的數據集
  2. 再用這些數據集訓練出很多棵樹,訓練的過程為了實現Diversity,每一棵樹不會考慮所有的feature。所以每棵樹都來自不同的data和features
  3. Aggregation : 用Majority Vote,也就是一人一票的方式,將所有tree的結果合在一起

更多資料

Q3 What is the difference between OOB score and validation score?

OOB Score : 在訓練很多決策樹時,有一個步驟是boostrap,也就是透過抽後放回的方式產生非常多個資料集。這個過程中,會有一些數據是沒有被選到的,OOB代表的就是沒有被選到的數據,OOB score就是拿這些沒選到的數據丟進去給沒有用這筆數據訓練的樹,看判斷的對不對。

Navnina Bhatia’s Medium

比較:OOB使用部分的樹去計算score(只有那些沒有用到該筆資料的樹),Validation score用全部的樹去計算。一般來說當數據量很小的時候會用OOB,因為Validation還要切一塊出去就顯得數據更不夠用了。

更多資料 OOB OOB比較 比較

Q4 What are the differences between Bagging, Boosting and Staking?

Bagging : 可以拆成bootstrap + aggregation。bootstrap就是透過抽後放回產生很多小資料集,aggregation就是用Majority Vote一人一票的方式把分類器組起來。

Bagging VS Boosting VS Staking:

  1. Bagging在每個分類器的在每個分類器的訓練及不太依樣(抽後放回),Boosting則是訓練集不便,但是對資料做加權
  2. Bagging分類器可以平行(並行)生成,Boosting必須一個一個來,因為資料加權是依賴上一個分類器做錯的,把權重提高。
  3. Bagging傾向降低variance(單一決策樹容易overfitting),Boosting, Staking傾向降低bias
  4. Staking用不同的ML模型去組合(heterogeneous),Bagging跟Boosting用相同的Weak Leaner去組合(homogeneous)

同質性

Q5 Give some reasons to choose Random Forests or Neural Networks

  1. RF只適用於tabular data,NN可以應用的資料範圍較廣,像是圖片、音檔、文字..
  2. RF來自於很多棵樹,相比NN具有較佳的解釋性
  3. NN需要很多數據,計算上的要求較高(可能要GPU),RF相對來說不用那麼多數據

更多資料

AdaBoost, Gradient Boost, XGBoost未來再補上,累了累了

About Me

清華大學 工業工程碩士 2022~2024 | Kryi-Practice 自動生成單字填空題APP創辦人 | 透過Medium紀錄所學、所思、所想

LinkedIn

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

吳定群
吳定群

Written by 吳定群

Tsing Hua University Master for Engineering | ML & statistic | Data Science

No responses yet

Write a response