XGBoost/LightGBM 梯度提升
一句话概述:XGBoost 和 LightGBM 是两大梯度提升框架,在表格数据上的表现碾压深度学习,是 Kaggle 竞赛和工业界结构化数据建模的首选。XGBoost 3.2(2026)、LightGBM 4.6(2025)。
核心知识点
| 概念 | 白话解释 |
|---|
| Gradient Boosting | 梯度提升 = 多棵弱决策树串联,每棵修正前一棵的错误 |
| XGBoost | 极端梯度提升 = GBDT 的高效实现(精度高) |
| LightGBM | 轻量梯度提升 = 微软开发(训练更快,内存更省) |
| Learning Rate | 学习率 = 每棵树的贡献权重(越小越稳但需更多树) |
| Early Stopping | 早停 = 验证集不再提升时自动停止(防过拟合) |
| Feature Importance | 特征重要性 = 每个特征对预测的贡献度 |
安装配置
pip install xgboost lightgbm # 安装两个库
pip install catboost # 也可装 CatBoost(第三个选择)
基本使用
XGBoost
import xgboost as xgb # 导入 XGBoost
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 准备数据
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Scikit-learn API(推荐入门)
model = xgb.XGBClassifier(
n_estimators=200, # 树的数量
max_depth=6, # 最大深度
learning_rate=0.1, # 学习率
subsample=0.8, # 行采样比例
colsample_bytree=0.8, # 列采样比例
eval_metric='logloss', # 评估指标
random_state=42,
n_jobs=-1 # 使用所有 CPU
)
# 训练(带早停)
model.fit(
X_train, y_train,
eval_set=[(X_test, y_test)], # 验证集
verbose=10 # 每 10 轮打印
)
# 预测
y_pred = model.predict(X_test) # 预测类别
y_prob = model.predict_proba(X_test)[:, 1] # 预测概率
print(f"准确率: {accuracy_score(y_test, y_pred):.4f}")
LightGBM
import lightgbm as lgb # 导入 LightGBM
# Scikit-learn API
model = lgb.LGBMClassifier(
n_estimators=200, # 树的数量
max_depth=-1, # 不限深度(靠叶子数控制)
num_leaves=31, # 叶子节点数(关键参数)
learning_rate=0.1, # 学习率
subsample=0.8, # 行采样
colsample_bytree=0.8, # 列采样
random_state=42,
n_jobs=-1
)
# 训练
model.fit(
X_train, y_train,
eval_set=[(X_test, y_test)], # 验证集
callbacks=[lgb.early_stopping(50), # 50 轮不提升则停
lgb.log_evaluation(10)] # 每 10 轮打印
)
y_pred = model.predict(X_test)
print(f"准确率: {accuracy_score(y_test, y_pred):.4f}")
特征重要性
import matplotlib.pyplot as plt
# XGBoost 特征重要性
xgb.plot_importance(model, max_num_features=20, importance_type='gain')
plt.tight_layout()
plt.savefig('xgb_importance.png', dpi=300)
# LightGBM 特征重要性
lgb.plot_importance(model, max_num_features=20, importance_type='gain')
plt.tight_layout()
plt.savefig('lgb_importance.png', dpi=300)
高级用法
Optuna 自动调参
import optuna # 导入 Optuna
def objective(trial):
params = {
'n_estimators': trial.suggest_int('n_estimators', 100, 500),
'max_depth': trial.suggest_int('max_depth', 3, 12),
'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True),
'subsample': trial.suggest_float('subsample', 0.6, 1.0),
'colsample_bytree': trial.suggest_float('colsample_bytree', 0.6, 1.0),
'num_leaves': trial.suggest_int('num_leaves', 20, 100),
}
model = lgb.LGBMClassifier(**params, random_state=42, n_jobs=-1)
model.fit(X_train, y_train, eval_set=[(X_test, y_test)],
callbacks=[lgb.early_stopping(50), lgb.log_evaluation(0)])
y_pred = model.predict(X_test)
return accuracy_score(y_test, y_pred)
study = optuna.create_study(direction='maximize') # 最大化准确率
study.optimize(objective, n_trials=100) # 搜索 100 次
print(f"最优参数: {study.best_params}")
常见报错
| 报错信息 | 原因 | 解决方法 |
|---|
XGBoostError: value 0 for Parameter | 参数值无效 | 检查参数范围 |
LightGBMError: label must be integer | 标签类型错误 | 转换为 int |
GPU 相关错误 | GPU 配置问题 | 安装 GPU 版本或用 CPU |
速查表
# === XGBoost vs LightGBM 选择 ===
# XGBoost: 精度略高、社区更大、GPU 支持好
# LightGBM: 训练更快、内存更省、大数据更适合
# CatBoost: 类别特征自动处理、最不容易过拟合
# === 关键参数 ===
# n_estimators: 树的数量(100-500)
# learning_rate: 学习率(0.01-0.3)
# max_depth: 最大深度(XGB: 3-10, LGB: -1 不限)
# num_leaves: 叶子数(LGB 专有, 20-100)
# subsample: 行采样(0.6-1.0)
# colsample_bytree: 列采样(0.6-1.0)
# early_stopping: 早停轮数(30-100)
# === 核心流程 ===
# 1. 数据划分 → 2. 创建模型 → 3. fit(早停) → 4. predict → 5. 调参
参考:XGBoost v3.2 | LightGBM v4.6 | 更新于 2026 年