Ensemble Method: Boosting

Ensemble Method: Boosting

Boosting can be described as a method where we combine several weak classifiers and transform them into one strong classifier sequentially.
模型
用一句话总结
类比
AdaBoost
盯着前面的错题一步步补丁式修
普通学生反复练错题
XGBoost
专业工程队按标准修复误差,有正则化、有优化
有计划的专业装修队
LightGBM
XGBoost 的竞速加强版,加速 + 压缩 + 大规模处理
工程队 + 挖掘机 + 压缩包

AdaBoost

先训练一个模型 → 找错的样本 → 提高它们的权重 → 下一个模型专门修这些错。
举个例子:
你想教学生做题。
第一轮:
  • 学生做错了 20 道题
    • 第二轮:
  • 你让他重点复习那 20 道
    • 第三轮:
  • 再专注新错的地方
最后把几轮结果加权组合成一个强模型。

特点

  • 简单
  • 对噪声敏感(因为一直盯着难题和异常点,会“越学越偏”)
  • 不适合大数据
  • 不灵活、不能自定义复杂损失函数
📌 AdaBoost = 老式 Boosting,补丁式逐个纠错。

XGBoost (Extreme Gradient Boosting)

AdaBoost 有缺点,所以后来出现 Gradient Boosting(GBDT)
用“残差(误差)”来引导下一棵树学习。
XGBoost = GBDT 的专业工程队版本,做了很多增强:

核心思想

每一棵树都在拟合上一棵树的“误差/残差”。
比如预测 default:
第一棵树:
预测不准 → 留下一堆 residual(残差)
第二棵树:
专门去学残差
第三棵树:
继续学剩下没学好的残差
最终:
很多棵小树叠加出一个非常强的模型。

XGBoost 为什么强?(简单到一听就懂)

正则化(L1/L2)
让树不要长太多叶子 → 防止过拟合。
Shrinkage (learning rate)
每棵树只贡献一点点力量,整体更稳。
Column subsampling
每次只用部分特征 → 样本更“多样” → 泛化更好。
支持并行(树的节点可并行计算)
速度很快。
Sparsity-aware(会自动跳过缺失)
不需要手动填补缺失值。
📌 XGBoost = 有正则化、有工程优化、有细节、有安全性的 GBDT 升级版。

Whole XGBoost pipeline

1. Import Necessary Libraries

import pandas as pd from sklearn.model_selection import train_test_split, GridSearchCV from sklearn.metrics import mean_squared_error from sklearn.feature_extraction import DictVectorizer from sklearn.pipeline import Pipeline import xgboost as xgb

2. Load and Prepare Data

# Example data loading data = { 'LotFrontage': [80, 81, 82, None, 84, 83, 85, 87, None, 89], 'OverallQual': [7, 6, 7, 8, 5, 6, 8, 9, 7, 5], 'YearBuilt': [2003, 1976, 2001, 1915, 2000, 2002, 1999, 1980, 2005, 1998], 'SalePrice': [200000, 150000, 180000, 130000, 175000, 165000, 210000, 220000, 190000, 160000] } df = pd.DataFrame(data) # Features and target X = df.drop('SalePrice', axis=1) y = df['SalePrice'] # Fill missing values X['LotFrontage'] = X['LotFrontage'].fillna(0)

3. Define and Fit the Pipeline

# Setup the pipeline steps steps = [("ohe_onestep", DictVectorizer(sparse=False)), ("xgb_model", xgb.XGBRegressor(objective='reg:squarederror', random_state=42))] # Create the pipeline xgb_pipeline = Pipeline(steps) # Convert DataFrame to dictionary format X_dict = X.to_dict("records") # Fit the pipeline xgb_pipeline.fit(X_dict, y)

4. Model Evaluation

# Split data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Convert training and testing sets to dictionary format X_train_dict = X_train.to_dict("records") X_test_dict = X_test.to_dict("records") # Fit the pipeline on the training data xgb_pipeline.fit(X_train_dict, y_train) # Predict on the test data y_pred = xgb_pipeline.predict(X_test_dict) # Calculate and print the Mean Squared Error mse = mean_squared_error(y_test, y_pred) print(f"Mean Squared Error: {mse}")

5. Hyperparameter Tuning Using Grid Search

# Define the parameter grid param_grid = { 'xgb_model__n_estimators': [50, 100, 200], 'xgb_model__learning_rate': [0.01, 0.1, 0.2], 'xgb_model__max_depth': [3, 5, 7], 'xgb_model__subsample': [0.6, 0.8, 1.0], 'xgb_model__colsample_bytree': [0.6, 0.8, 1.0] } # Initialize GridSearchCV grid_search = GridSearchCV(estimator=xgb_pipeline, param_grid=param_grid, cv=3, scoring='neg_mean_squared_error', n_jobs=-1, verbose=2) # Fit GridSearchCV grid_search.fit(X_train_dict, y_train) # Print the best parameters and score print("Best Parameters:", grid_search.best_params_) print("Best Score:", -grid_search.best_score_)

Summary

  1. Data Preparation: We handled missing values in the LotFrontage column.
  1. Pipeline Setup: Created a pipeline with DictVectorizer for one-hot encoding and XGBRegressor for regression.
  1. Model Training: Split the data into training and testing sets, trained the model, and evaluated it using Mean Squared Error.
  1. Hyperparameter Tuning: Used GridSearchCV to find the best hyperparameters for the XGBoost model.

LightGBM

LightGBM 的目标只有一个:极致加速 + 极致省内存

核心思想

和 XGBoost 一样是 Gradient Boosting,但使用了两大“黑科技”:

关键黑科技 1:Histogram Binning(直方图分箱)

不再对连续变量逐点比较
→ 先把数值压缩成 255 个 bins
→ 再在 bins 上找分裂点
→ 速度提升几十倍
→ 内存减少大量
📌 用一句话形容:
把长长的连续数值文件压缩成小档案,加速阅读。

关键黑科技 2:Leaf-wise growth(叶节点优先扩展)

XGBoost:
  • “深度优先”长树
  • 每一层要平衡左右
LightGBM:
  • 找到 信息增益最高 的叶子 → 直接往下长
  • 树长得“不均匀”,但更精准
📌 用一句话形容:
LightGBM 每次都在最有价值的地方继续挖,模型更强但更容易过拟合。

其他 LightGBM 优势

  • 原生支持 categorical features
  • 数据量越大越占优势
  • 内存极低
  • 训练速度非常快(XGBoost 的 5〜20 倍)

什么时候用 AdaBoost?(基本不会用,除非特殊情况)

✔ 使用场景极少,只有两种情况:

(1) 特征非常干净 + 噪声极低

比如:
  • 手写数字识别(MNIST)
  • 标准小数据集(Iris)
因为 AdaBoost 对噪声特别敏感。
如果数据里有 outliers,它会被“盯着死命学错题”,越学越差。

(2) 你需要一个非常简单、快速理解的模型

AdaBoost 是 boosting 算法中结构最简单的。

⭐ 总结一句话

AdaBoost 只在 “干净小数据” 中还可以,其余场景几乎被 XGBoost 完全替代。

什么时候用 XGBoost?(默认强者,稳健、安全)

✔ 当你需要 强健、可控、可解释 的模型

XGBoost 的特点:
  • 正则化(L1/L2)
  • 可控的深度
  • 对噪声不敏感
  • 可调参细
  • Bug 最少
  • 适合中等规模数据(几十万〜几百万样本)

✔ 特别适用于:

  • 信用风险(PD、LGD)
  • 风险模型(信用评分、违约概率)
  • Fraud detection
  • Tabular data
  • 金融风控(需要可解释性和稳定性)

⭐ 一句话总结

当你希望模型“稳、准、可解释、可控” → 用 XGBoost。

什么时候用 LightGBM?(大数据王者,速度最快)

LightGBM 是速度怪兽,适合“大规模 + 高维 + 大量特征”。

✔ 使用场景:

(1) 数据非常大(百万级、千万级、上亿)

因为它是 histogram + leaf-wise →
速度远超 XGBoost。

(2) 特征非常多(几千〜几万个 columns)

LightGBM 对高维数据很友好。

(3) 你需要快速迭代和调参

比如 Kaggle、生产环境中实时调参。

(4) 有大量 categorical features(高基数)

因为 LGBM 原生支持 categorical,有天然优势。

⭐ 一句话总结

数据大 → LightGBM
数据中等但需要稳健 → XGBoost。