版本比较
XGBoost 指南(Python)
正文
增加 45 行删除 87 行
11234526 行未改动3233343536363738394041404142434445460 行未改动全部展开50650750850951051151251351451551651051851952052152251151252452552651351451551651751851952852953052052153253353453553653753852252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455598 行未改动654655656657646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700658659660661491 行未改动全部展开11531154115511561200120112021157115811591160249 行未改动全部展开14101411141214131414141514161417# Python XGBoost 完全指南:从简单到高级# Python XGBoost 指南
## 目录
1. [安装与基础概念](#1-安装与基础概念)26 行未改动```bashpip install xgboost```
### XGBoost 核心概念### XGBoost 是什么?
XGBoost,全称 e**X**treme **G**radient **B**oosting,极限梯度提升。
- **Boosting(提升)**:集成多个弱模型(通常是决策树),每棵新树修正前面所有树的错误- **梯度提升**:通过拟合残差的负梯度方向来逐步优化损失函数- **正则化**:XGBoost 独有的正则化项(L1/L2),防止过拟合- **二阶优化**:使用梯度和 Hessian(二阶导数),比传统 GBDT 更精确
### 两套 API
| API | 说明 | 适用场景 |460 行未改动全部展开## 10. 超参数调优
### 10.1 关键超参数速查表
```python"""=== 树结构参数 ===max_depth : 树的最大深度 (default=6, 范围: 3~10)min_child_weight : 叶子节点最小样本权重和 (default=1, 越大越保守)gamma : 节点分裂所需最小增益 (default=0, 越大越保守)max_delta_step : 每棵树权重更新最大步长 (default=0, 不平衡数据时可用1~10)
=== 采样参数 ===subsample : 训练每棵树时的子采样比例 (default=1, 常用: 0.6~0.9)colsample_bytree : 构建每棵树时的列采样比例 (default=1, 常用: 0.6~0.9)colsample_bylevel : 树每层的列采样比例colsample_bynode : 每个节点的列采样比例### 树结构参数
=== 学习参数 ===learning_rate (eta): 学习率/步长收缩 (default=0.3, 常用: 0.01~0.3)n_estimators : 树的数量 (常用: 100~1000)| 参数名 | 说明 | 默认值 / 常用范围 || :--- | :--- | :--- || `max_depth` | 树的最大深度 | default=6, 范围: 3~10 || `min_child_weight` | 叶子节点最小样本权重和 | default=1, 越大越保守 || `gamma` | 节点分裂所需最小增益 | default=0, 越大越保守 || `max_delta_step` | 每棵树权重更新最大步长 | default=0, 不平衡数据时可用 1~10 |
=== 正则化参数 ===reg_alpha (alpha) : L1 正则化 (default=0)reg_lambda (lambda): L2 正则化 (default=1)### 采样参数
=== 其他参数 ===tree_method : 树构建算法 (auto/exact/approx/hist/gpu_hist)scale_pos_weight : 正样本权重(处理不平衡数据)objective : 目标函数eval_metric : 评估指标"""```| 参数名 | 说明 | 默认值 / 常用范围 || :--- | :--- | :--- || `subsample` | 训练每棵树时的子采样比例 | default=1, 常用: 0.6~0.9 || `colsample_bytree` | 构建每棵树时的列采样比例 | default=1, 常用: 0.6~0.9 || `colsample_bylevel` | 树每层的列采样比例 | - || `colsample_bynode` | 每个节点的列采样比例 | - |
### 学习参数
| 参数名 | 说明 | 默认值 / 常用范围 || :--- | :--- | :--- || `learning_rate (eta)` | 学习率/步长收缩 | default=0.3, 常用: 0.01~0.3 || `n_estimators` | 树的数量 | 常用: 100~1000 |
### 正则化参数
| 参数名 | 说明 | 默认值 / 常用范围 || :--- | :--- | :--- || `reg_alpha (alpha)` | L1 正则化 | default=0 || `reg_lambda (lambda)` | L2 正则化 | default=1 |
### 其他参数
| 参数名 | 说明 | 默认值 / 常用范围 || :--- | :--- | :--- || `tree_method` | 树构建算法 | auto / exact / approx / hist / gpu_hist || `scale_pos_weight` | 正样本权重(处理不平衡数据) | - || `objective` | 目标函数 | - || `eval_metric` | 评估指标 | - |
### 10.2 GridSearchCV
```pythonfrom sklearn.model_selection import GridSearchCV98 行未改动# 用最佳参数训练最终模型best_model = xgb.XGBClassifier(**study.best_params, random_state=42)best_model.fit(X_train, y_train)```
### 10.5 手动分步调参策略
```python"""推荐调参顺序:
Step 1: 固定学习率,确定树的数量 - 设 learning_rate=0.1,用 cv 确定最佳 n_estimators Step 2: 调 max_depth 和 min_child_weight - 这两个参数对模型影响最大 Step 3: 调 gamma - 控制分裂的保守程度 Step 4: 调 subsample 和 colsample_bytree - 引入随机性,防止过拟合 Step 5: 调正则化参数 reg_alpha 和 reg_lambda - 进一步控制过拟合 Step 6: 降低学习率,增加树的数量 - 最终微调,提升模型性能"""
# Step 1: 确定最佳轮数dtrain = xgb.DMatrix(X_train, label=y_train)params_base = {'max_depth': 5, 'eta': 0.1, 'objective': 'binary:logistic'}cv_result = xgb.cv(params_base, dtrain, num_boost_round=1000, nfold=5, early_stopping_rounds=50)best_rounds = len(cv_result)print(f"最佳轮数: {best_rounds}")
# Step 2: 调 max_depth 和 min_child_weightbest_score = 0for md in [3, 5, 7, 9]: for mcw in [1, 3, 5, 7]: params = { **params_base, 'max_depth': md, 'min_child_weight': mcw } cv_result = xgb.cv(params, dtrain, num_boost_round=best_rounds, nfold=5, as_pandas=False) score = cv_result[-1]['test-auc-mean'] if score > best_score: best_score = score best_md, best_mcw = md, mcw
print(f"Best max_depth={best_md}, min_child_weight={best_mcw}, AUC={best_score:.4f}")
# 继续后续步骤...```
---
## 11. 类别特征处理
491 行未改动全部展开
## 20. 完整实战项目模板
```python"""XGBoost 完整实战模板 - 二分类问题"""import numpy as npimport pandas as pdimport xgboost as xgbfrom sklearn.model_selection import train_test_split, StratifiedKFold249 行未改动全部展开| `rank:ndcg` | 排序(NDCG) || `rank:map` | 排序(MAP) || `reg:gamma` | Gamma 回归 || `reg:tweedie` | Tweedie 回归 |
---
本文由千问 AI 辅助创作。