Skip to content

Machine Learning › 監督式學習 › 集成方法 › XGBoost

XGBoost (Extreme Gradient Boosting) ​

Gradient Boosting 的高效能實作,在表格資料與競賽中最常見。相較 sklearn 的經典版:

特色說明
正則化目標函數損失之外加上葉節點數與葉權重的 L1 / L2 懲罰(gamma、reg_alpha、reg_lambda)
二階近似用梯度與 Hessian 決定分裂與葉值
缺失值每個分裂學習缺失值該往哪邊走
效能直方圖分裂(tree_method="hist",預設)、多執行緒、GPU(device="cuda")
sklearn 相容XGBClassifier / XGBRegressor 可放進 pipeline、GridSearchCV

用法 ​

python
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score

# 承乳癌範例(1 = 惡性)
# 1. 基本
model = XGBClassifier(n_estimators=300, max_depth=3, learning_rate=0.05,
                      subsample=0.8, colsample_bytree=0.8,
                      eval_metric="logloss", random_state=42)
# CV AUC 0.992

# 2. Early stopping:從訓練資料再切出驗證集(不要用測試集!)
X_tr, X_val, y_tr, y_val = train_test_split(X_train, y_train, test_size=0.2,
                                            stratify=y_train, random_state=0)
model = XGBClassifier(n_estimators=2000, learning_rate=0.05, max_depth=3,
                      subsample=0.8, colsample_bytree=0.8, eval_metric="logloss",
                      early_stopping_rounds=50, random_state=42)
model.fit(X_tr, y_tr, eval_set=[(X_val, y_val)], verbose=False)
model.best_iteration          # 326
roc_auc_score(y_test, model.predict_proba(X_test)[:, 1])      # 0.996

# 3. 不平衡:scale_pos_weight ≈ 陰性數 / 陽性數
(y_train == 0).sum() / (y_train == 1).sum()                   # 1.69

新版 XGBoost:early_stopping_rounds 與 eval_metric 放在建構子;3.x 的 fit() 已不接受這兩個參數(舊教學的寫法會報 TypeError)。GPU 改用 device="cuda"(舊寫法 tree_method="gpu_hist")。本筆記以 XGBoost 3.4 驗證。

參數 ​

參數作用常用起點
learning_rate (eta)每棵樹的貢獻0.05–0.1
n_estimators樹數上限,搭配 early stopping1000+
max_depth樹深3–8
min_child_weight葉節點的最小 Hessian 總和(≈ 最少樣本數)1–10
subsample / colsample_bytree樣本 / 特徵抽樣比例0.7–0.9
reg_lambda / reg_alpha葉權重的 L2 / L1 正則化1 / 0
gamma分裂需要的最小損失下降0
scale_pos_weight不平衡時加重陽性陰性 / 陽性比

調參順序與 Gradient Boosting 相同。

提醒 ​

提醒說明
不是免調參魔法降低 learning_rate 要搭配更多樹;只調 max_depth 而忽略 subsampling 與正則化常不夠
Early stopping 的驗證集不能是測試集否則測試分數偏樂觀
分數很高時先查洩漏與不平衡見 資料洩漏、類別不平衡
重要性不是因果內建 feature_importances_(gain)有偏誤;用 SHAP 或 permutation importance,見 可解釋性
先和 sklearn HistGradientBoosting 比小到中型資料常差不多,後者不需額外安裝
小資料不一定贏乳癌資料上 XGBoost(0.992)和邏輯斯迴歸(0.992)打平