800 words
4 minutes

Machine Learning : Decision Tree

Intro#

a

G(x)=c=1C[b(x)=c]Gc(x)G(x) = \sum_{c=1}^{C} [b(x) = c] G_c(x)

Basic Decision Tree Algorithm#

當還未滿足終止條件時,執行 :

  1. 學一個分割準則 b(x)b(x)
  2. 當前節點分割 : D\mathcal{D} to CC parts Dc={(xn,yn):b(xn)=c}\mathcal{D}_c = \{(x_n, y_n): b(x_n) = c\}
  3. 分割出來的子集再做遞迴呼叫 : GcDecisionTree(Dc)G_c \leftarrow DecisionTree(\mathcal{D}_c)
  4. 返回當前節點的預測函數 : G(x)=c=1C[b(x)=c]Gc(x)G(x) = \sum_{c=1}^{C} [b(x) = c] G_c(x)

C&RT (Classification and Regression Tree) Algorithm#

Branching#

  • 選擇最佳分割準則
  • 總是創造二元樹
  • 基於leaf的最佳常數 : gt(x)=Eyoptimal constantg_t(x) = E_{y|\text{optimal constant}}

Purifying#

  • 用一個簡單的閾值做二元分類
  • 純化節點 : b(x)=argmindecision stumps h(x)[Dc with h]impurity(Dc with h)b(x) = \underset{\text{decision stumps } h(x)}{\operatorname{argmin}} [D_c \text{ with } h] \cdot \text{impurity}(D_c \text{ with } h)

Some Metrics#

  • Regression Error : impurity(D)=1Nn=1N(ynyˉ)2\text{impurity}(\mathcal{D}) = \frac{1}{N} \sum_{n=1}^{N} (y_n - \bar{y})^2
  • Classification Error : 1max1kKn=1NI(yn=k)N1 - \max_{1 \le k \le K} \frac{\sum_{n=1}^{N} \mathbb{I}(y_n = k)}{N}
  • Gini Index : 1k=1K(n=1NI(yn=k)N)21 - \sum_{k=1}^{K} \left( \frac{\sum_{n=1}^{N} \mathbb{I}(y_n = k)}{N} \right)^2

Algorithm#

  • 邊界條件 : 不能再做分支
  1. 學一個分割準則 : b(x)=argmindecision stumps h(x)[Dc with h]impurity(Dc with h)b(x) = \underset{\text{decision stumps } h(x)}{\operatorname{argmin}} [D_c \text{ with } h] \cdot \text{impurity}(D_c \text{ with } h)
  2. 考慮最佳的樹樁 : Dc={(xn,yn):b(xn)=c}\mathcal{D}_c = \{(\mathbf{x}_n, y_n) : b(\mathbf{x}_n) = c\} ,切出兩個子集
  3. 遞迴
  4. 返回組合之後的模型 : G(x)=c=12I(b(x)=c)Gc(x)G(\mathbf{x}) = \sum_{c=1}^{2} \mathbb{I}(b(\mathbf{x}) = c) G_c(\mathbf{x})

Regularize#

  • Pruning
  • 做決策樹時,同時把複雜度加入懲罰項
  • 先長出完全樹,再去每一步選擇能最小化 (Error + 複雜度) 的node

Branching on Categorial Features#

  • 可以根據出現率 / label相關性排序,再做類似數值特徵的閾值分類

Surrogate Branch#

  • 關鍵的特徵剛好缺值
  • 在訓練時對每個 b(x)b(x) 就先算一次次要分裂,想辦法用其他特徵構造出效果差不多的樹樁
  • 遺失資料時可以用替代路徑做分類

Properties#

決策樹優勢#

  • 易於理解和視覺化
  • 能處理數值和類別特徵
  • 自動特徵選擇

複雜資料集處理#

  • 多維特徵空間分割
  • 複雜決策邊界學習

Adaptive Boosting#

  • 結合弱分類器最後集成一個強大的分類器

Boostrap (Re-Weighting)#

  • 對原始資料做抽樣(有重複,不平均)
  • 每個Learner想辦法最小化誤差

如果每次只是隨機Boostrap,Base Learner會太相似,重採樣強迫學習不同的解

Optimal Re-Weighting#

  1. 假設當前Base Learner的錯誤率是 : ϵt=nun(t)I[yngt(xn)]nun(t)\epsilon_t = \frac{\sum_{n} u_n^{(t)} \mathbb{I}[y_n \neq g_t(x_n)]}{\sum_{n} u_n^{(t)}}
  2. 我們希望更新權重之後對錯各半,因此 :
    • 錯分的資料乘 (1ϵt)(1 - \epsilon_t)
    • 分好的資料乘 ϵt\epsilon_t
  3. Normalization
  4. Scaling Factor : ϕt=1ϵtϵt\phi_t = \sqrt{\frac{1 - \epsilon_t}{\epsilon_t}}

依照Scaling Factor的定義,假如當前Base Learner的效果很好,則會把錯分的資料加權到超大,把注意力轉移到更難分類的樣本

AdaBoost Algorithm#

  1. 先初始化權重(一開始大家都一樣) : un(1)=1/Nu_n^{(1)} = 1/N
  2. 進行迭代 :
    • 訓練弱分類器 gtg_t ,最小化 ϵt=n=1Nun(t)I[yngt(xn)]n=1Nun(t)\epsilon_t = \frac{\sum_{n=1}^{N} u_n^{(t)} \mathbb{I}[y_n \neq g_t(x_n)]}{\sum_{n=1}^{N} u_n^{(t)}}
    • 計算 ϕt=(1ϵt)/ϵt\phi_t = \sqrt{(1 - \epsilon_t) / \epsilon_t},更新權重 u(t)u(t+1)u^{(t)} \rightarrow u^{(t+1)} :
    • 分類錯誤的權重乘以 ϕt\phi_t
    • 分類正確的權重除以 ϕt\phi_t
    • αt=lnϕt\alpha_t = \ln \phi_t (越準確的 gtg_tαt\alpha_t 越大),越大代表在最終決策有更大權重
  3. 最後輸出分類器 : G(x)=sign(t=1Tαtgt(x))G(x) = \text{sign}\left(\sum_{t=1}^{T} \alpha_t g_t(x)\right)

Decision Stump as Base Learner#

  • 做一個簡單的樹樁來二元分類 : h(x)=ssign(xiθ)h(x) = s \cdot \text{sign}(x_i - \theta)
  • 目標是找最佳的分裂方向,可以先對特徵值進行排序
  • 暴力搜索參數((θ,i,s)(\theta, i, s)),總複雜度是 O(dNlogN)O(d \cdot N \log N)

Reference#

Machine Learning : Decision Tree
https://blog.cyberangel.work/posts/machine-learning-decision-tree/
Author
Ethan
Published at
2025-06-01
License
CC BY-NC-SA 4.0
Last updated on 2025-06-01,186 days ago

Some content may be outdated

Table of Contents