ML-Ensembles

jupyter对于复制粘贴的图片 不友好,在github ipynb上展示不出来

random forest

▶️Youtube-random forest
决策树对训练数据很好,easy to build / use / intercpt, 但是对新测试样本Inaccuracy

图片.png

Build

STEP1: create a bootstrapped dataset. randomly choose from original dataset. 图片.png 图片.png

STEP2 : Create a decision tree using the bootstrapped dataset 图片.png 图片.png 图片.png

Back to STEP 1 and repeat, we can make many decision trees. This a forest.!

Use: How to use this forest: 图片.png 图片.png

Bagging : the strategy - plus the vote.

Estimate :How to know this good ?: Out-of-Bag Error. (incorrect proportion on outof-bag samples.) 图片.png 图片.png 图片.png 图片.png 图片.png

Adaboost

▶️Youtube-Adaboost

Key view: Stump, Vote weight, order

图片.png

Stump 图片.png

Vote weight 图片.png

Order
Stumps in Adaboost : order is important.Every stump influence how to build next stump.!
Otherwise, trees in RandomForest is independent

Build first stump

图片.png 图片.png 图片.png Choose the min Gini index stump as the first stump in the forest

Gini 指数(Gini Index):衡量节点纯度。 值越小越纯 \(Gini(t) = 1 - \sum_{i=1}^C p_i^2\)

  • C 是类别总数;

  • $p_i$ 是样本中属于第 $i$ 类的比例。 假设某个节点包含两个类别,正类和负类,正类占 70%,负类占 30%: \(Gini = 1 - (0.7^2 + 0.3^2) = 1 - (0.49 + 0.09) = 0.42\)

Update sample weight : Amount of say

图片.png 图片.png 图片.png 图片.png Total Error = sum incorrect sample weight!

Increase incorrect sample Weight 图片.png 图片.png 图片.png

Decrease correct sample weight! 图片.png 图片.png

Normalize sample weight 图片.png 图片.png

Build next stump:

Method1 - If We have weighted Gini Function
We use original dataset with the new sample weight.
Big sample weight will get bigger gini-index!?

Method2 - don’t
Use original dataset with the new sample weight to make a new dataset, than same every sample weight.

轮盘赌创建一个 新dataset

图片.png 图片.png

How to use

图片.png

Gradient-Boosted Trees

迭代构建多个弱学习器(如树),每次纠正之前模型的错误!

🌟 1. 基础思想:Boosting

Boosting 是一种 加法模型 + 前向分步优化 的策略:

我们希望学得一个函数 $F(x)$,它由多个基模型(通常是树)组成: \(F(x) = \sum_{m=1}^{M} \gamma_m h_m(x)\)

其中:

  • $h_m(x)$ 是第 $m$ 个基学习器(比如决策树);
  • $\gamma_m$ 是每个学习器的权重;
  • 最终的输出是所有学习器的加权和。

📉 2. 梯度提升(Gradient Boosting)

目标是最小化某个损失函数 $L(y, F(x))$,比如平方误差或交叉熵。

由于直接优化很难,我们采用 前向分步策略

第 $m$ 步时,我们固定前面 $m-1$ 步的结果 $F_{m-1}(x)$,让新模型 $h_m(x)$ 去拟合当前模型的负梯度方向: \(r_i^{(m)} = - \left[ \frac{\partial L(y_i, F(x_i))}{\partial F(x_i)} \right]_{F = F_{m-1}}\)

这些 $r_i^{(m)}$ 被称为 伪残差(pseudo residuals)

然后我们训练一棵树 $h_m(x)$ 去拟合这些伪残差。

🔄 3. 模型更新

新模型的更新方式是:

$F_m(x) = F_{m-1}(x) + \eta \cdot \gamma_m h_m(x)$

  • $\eta \in (0, 1]$ 是学习率(learning rate),控制每次纠正的“步长”;
  • $\gamma_m$ 是通过一维搜索确定的最佳系数(最小化当前损失);
  • 加上这一项后,整体模型变得更好。

🧠 举个例子(回归,MSE)

  1. 初始化:用训练集的均值作为初始预测 $F_0(x)$
  2. 第 $m$ 步:
    • 计算残差:$r_i = y_i - F_{m-1}(x_i)$
    • 拟合一棵树:用 $r_i$ 作为目标
    • 得到新树 $h_m(x)$
    • 更新模型: $F_m(x) = F_{m-1}(x) + \eta h_m(x)$

这样不断迭代,每一步都在“纠正”上一轮的误差.

results matching ""

    No results matching ""