Decision tree+Random forest

Decision tree+Random forest

Decision Tree

Decision Tree definition

Decision Tree 是一种通过不断问问题(feature split),把数据一步步分开的模型,直到做出预测。
本质:if–else 规则的自动学习版
Regression Tree(回归树)
  • 每个 leaf:用 目标变量的平均值(mean) 作为预测
Classification Tree(分类树)
  • 每个 leaf(叶子节点):用 多数投票(majority vote) 决定预测类别

Regression Tree vs Classification Tree

Regression Tree 预测的是“数值”
Classification Tree 预测的是“类别”

1️⃣ 预测目标(最本质区别)

项目
Regression Tree
Classification Tree
预测的 y
连续数值(continuous)
离散类别(categorical)
示例
房价、销量、收益率
是否违约、是否欺诈、类别 A/B

2️⃣ 叶子节点(Leaf)在干什么?

🔹 Regression Tree(数值)

Leaf 输出 = 该叶子里所有 y 的平均值
例子:
Leaf 里 y = [5, 7, 9]
→ 预测值 = (5+7+9)/3 = 7

🔹 Classification Tree(分类)

Leaf 输出 = 多数投票(majority vote)
例子:
Leaf 里:
  • Cat: 6
  • Dog: 2
→ 预测类别 = Cat
(也可以输出概率:Cat 75%,Dog 25%)

Decision Tree:如何选问题 / 做分裂

Decision Tree 每一层都在做一件事:
找一个问题(feature + threshold),让数据变得“最不混杂”。
什么叫 Impurity(不纯度)?
Impurity = 一个节点里的数据有多“乱”

极端情况(非常重要)

情况
Impurity
节点里全是同一类
0(最纯)
各类比例差不多
高(很混)
👉 Tree 的目标:
从“混”的节点 → 拆成“更纯”的节点

两个最常见的 Impurity 指标(只记本质)

🔹 Gini Impurity(最常用,尤其在 sklearn)

直觉理解:
随机从这个节点里抽两个样本,它们属于不同类别的概率
  • 值越大 → 越混
  • 值越小 → 越纯
  • 最小值 = 0(全是同一类)
📌 你不需要记公式,只记:
Gini = “混乱概率”

🔹 Entropy(信息熵,来自信息论)

直觉理解:
我对这个节点的“不确定性”有多大
  • 类别越平均 → 不确定性越大
  • 类别越集中 → 不确定性越小
📌 你不需要记公式,只记:
Entropy = “不确定性”
 
Information Gain(信息增益)是什么?
Information Gain = 你这个问题“值不值”
notion image
Tree 是如何“选问题”的?(完整流程)
在某一个节点,Tree 会做👇

Step 1:枚举所有可能的问题

  • 所有 feature
  • 所有可能的切分点(threshold)

Step 2:对每一个候选切分

  • 计算 split 前的 impurity
  • 计算 split 后左右子节点的 impurity
  • 计算 Information Gain

Step 3:选 Information Gain 最大的那个切分

👉 这是“最好的问题”

Decision Tree Pros and Cons

Pros

  • Do not need to scale and normalize data
  • Handles missing values very well
  • Less effort in regards to preprocessing

Cons

  • Very prone to overfitting
  • Sensitive to outliers and changes in the data
  • Takes a long time to train and expensive complexity wise
  • Weak in terms of regression

Random Forest

Random Forest definition

1.Random Forest 通过“行随机 + 列随机 + 多棵树投票”,来显著降低过拟合(variance)

  • Random forest is an ensemble learning method that works by constructing a multitude of decision trees. A random forest can be constructed for both classification and regression tasks.
  • Random forest outperforms decision trees, and it also does not have the habit of overfitting the data as decision trees do.
  • A decision tree trained on a specific dataset will become very deep and cause overfitting. To create a random forest, decision trees can be trained on different subsets of the training dataset, and then the different decision trees can be averaged with the goal of decreasing the variance.

2.Random Forest 用于 Classification vs Regression

Classification Random Forest
  • 每棵树输出一个 class
  • 最终结果:
    • 多数投票
Regression Random Forest
  • 每棵树输出一个数值
  • 最终结果:
    • 取平均
📌 回忆 Tree Regression:
Leaf 的预测值 = 该 leaf 中 target 的平均值

Random Forest 的三大核心机制

① Bootstrap Sampling(行随机)

英文名:Bootstrap Aggregating(Bagging)
  • 从原始数据中:
    • 有放回抽样(sampling with replacement)
  • 每棵树:
    • 看到的是一份 不同的样本子集
  • 结果:
    • 每棵树学到的模式略有不同
📌 作用:
➡️ 降低模型之间的相关性

② Feature Subsampling(列随机)

这是 Random Forest 相比普通 Bagging Tree 的关键升级
  • 每棵树:
    • 不是使用全部 features
    • 只随机选一部分 features
  • 每次 split:
    • 也是只在部分 features 中选最优切分
📌 作用:
➡️ 进一步降低树与树之间的相似度

③ Ensemble Aggregation(模型集成)

  • Classification:
    • Majority Vote
  • Regression:
    • Average
📌 核心思想:
多个“过拟合但方向不同”的模型
→ 平均之后更稳

Random Forest 中你必须知道的 3 类超参数

一、Number of Trees

英文:n_estimators
  • 树越多:
    • variance 越低
    • 计算成本越高
  • 一般:
    • 几百棵就已经很稳定
📌 记忆点:
➡️ 树多 ≠ 过拟合(RF 对树数量不敏感)

二、Sampling Strategy(Bagging 策略)

1️⃣ 行采样比例

  • 每棵树用多少 rows
  • 常见:
    • 与原数据量相同(有放回)

2️⃣ 列采样比例

  • 每次 split 可用多少 features
  • 常见经验法则:
    • Classification:sqrt(p)
    • Regression:p / 3
📌 目的:
➡️ 降低树之间的 correlation

三、Tree Complexity(树复杂度)

这是 控制 overfitting 的关键

常见参数

  • max_depth
  • min_samples_leaf
  • min_samples_split
📌 核心逻辑:
设置
结果
树太浅
underfitting
树太深
单棵树 overfit
RF
用“多棵稍微 overfit 的树”来平均

Random Forest Pros and Cons

Pros

  • Performs well on imbalanced data
  • Works well with high dimensionality and handling a large amount of data
  • Decorrelates trees, can deal with problem of variance
  • Solves classification and regression issues

Cons

  • Need to have predictive power with the features otherwise it is inefficient
  • Can be a black box, hard to interpret