本文目录导读:

- 安装XGBoost
- 分类任务案例 - 葡萄酒质量预测
- 基础XGBoost分类器
- 超参数调优
- 回归任务案例 - 波士顿房价预测(使用内置数据)
- 特征重要性分析
- 高级功能 - 早停法和学习曲线
- 交叉验证评估
- 完整实战代码
- 常见XGBoost参数说明
我来给你一个完整的XGBoost实战案例,使用Scikit-learn接口。
安装XGBoost
pip install xgboost scikit-learn pandas numpy matplotlib
分类任务案例 - 葡萄酒质量预测
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from sklearn.datasets import load_wine
import xgboost as xgb
import warnings
warnings.filterwarnings('ignore')
# 加载数据集
data = load_wine()
X = data.data
y = data.target
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# 特征标准化
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
print("训练集大小:", X_train.shape)
print("测试集大小:", X_test.shape)
print("类别数量:", len(np.unique(y)))
基础XGBoost分类器
# 创建基础XGBoost分类器
xgb_clf = xgb.XGBClassifier(
n_estimators=100, # 树的数量
max_depth=3, # 树的最大深度
learning_rate=0.1, # 学习率
subsample=0.8, # 每棵树的样本采样比例
colsample_bytree=0.8, # 每棵树的特征采样比例
random_state=42
)
# 训练模型
xgb_clf.fit(X_train_scaled, y_train)
# 预测
y_pred = xgb_clf.predict(X_test_scaled)
# 评估模型
print("基础XGBoost模型结果:")
print(f"准确率: {accuracy_score(y_test, y_pred):.4f}")
print("\n分类报告:")
print(classification_report(y_test, y_pred, target_names=data.target_names))
超参数调优
# 定义参数网格
param_grid = {
'n_estimators': [50, 100, 150],
'max_depth': [3, 4, 5],
'learning_rate': [0.01, 0.1, 0.3],
'subsample': [0.6, 0.8, 1.0],
'colsample_bytree': [0.6, 0.8, 1.0]
}
# 创建XGBoost分类器
xgb_clf_gs = xgb.XGBClassifier(random_state=42, use_label_encoder=False, eval_metric='mlogloss')
# 网格搜索
grid_search = GridSearchCV(
xgb_clf_gs,
param_grid,
cv=5,
scoring='accuracy',
n_jobs=-1,
verbose=1
)
# 训练网格搜索
grid_search.fit(X_train_scaled, y_train)
# 最佳参数
print("最佳参数:")
print(grid_search.best_params_)
print(f"最佳交叉验证分数: {grid_search.best_score_:.4f}")
# 使用最佳模型预测
best_xgb = grid_search.best_estimator_
y_pred_best = best_xgb.predict(X_test_scaled)
print(f"\n优化后模型准确率: {accuracy_score(y_test, y_pred_best):.4f}")
回归任务案例 - 波士顿房价预测(使用内置数据)
from sklearn.datasets import load_diabetes
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
# 加载糖尿病数据集(作为回归示例)
diabetes = load_diabetes()
X_reg = diabetes.data
y_reg = diabetes.target
# 划分数据
X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(
X_reg, y_reg, test_size=0.2, random_state=42
)
# 创建XGBoost回归器
xgb_reg = xgb.XGBRegressor(
n_estimators=100,
max_depth=3,
learning_rate=0.1,
random_state=42
)
# 训练模型
xgb_reg.fit(X_train_reg, y_train_reg)
# 预测
y_pred_reg = xgb_reg.predict(X_test_reg)
# 评估
print("XGBoost回归模型结果:")
print(f"均方误差 (MSE): {mean_squared_error(y_test_reg, y_pred_reg):.4f}")
print(f"平均绝对误差 (MAE): {mean_absolute_error(y_test_reg, y_pred_reg):.4f}")
print(f"R² 分数: {r2_score(y_test_reg, y_pred_reg):.4f}")
特征重要性分析
# 获取特征重要性
feature_importance = xgb_clf.feature_importances_
feature_names = data.feature_names
# 创建特征重要性DataFrame
importance_df = pd.DataFrame({
'feature': feature_names,
'importance': feature_importance
}).sort_values('importance', ascending=False)
print("特征重要性排名:")
print(importance_df)
# 可视化特征重要性
plt.figure(figsize=(10, 6))
plt.barh(importance_df['feature'][:10], importance_df['importance'][:10])
plt.xlabel('重要性')
plt.ylabel('特征')'XGBoost特征重要性 (Top 10)')
plt.gca().invert_yaxis()
plt.tight_layout()
plt.show()
# 使用plot_importance功能
xgb.plot_importance(xgb_clf, max_num_features=10)
plt.tight_layout()
plt.show()
高级功能 - 早停法和学习曲线
# 使用早停法(Early Stopping)
xgb_clf_early = xgb.XGBClassifier(
n_estimators=1000,
max_depth=3,
learning_rate=0.1,
random_state=42,
early_stopping_rounds=20, # 如果20轮没有改善就停止
eval_metric=['mlogloss', 'merror']
)
# 训练带早停法的模型
xgb_clf_early.fit(
X_train_scaled,
y_train,
eval_set=[(X_test_scaled, y_test)],
verbose=True
)
print(f"实际训练的树的数量: {xgb_clf_early.best_iteration}")
交叉验证评估
# 使用交叉验证评估模型
cv_scores = cross_val_score(
xgb_clf, X_train_scaled, y_train,
cv=5, scoring='accuracy'
)
print("交叉验证结果:")
print(f"每次CV准确率: {cv_scores}")
print(f"平均准确率: {cv_scores.mean():.4f} (+/- {cv_scores.std() * 2:.4f})")
完整实战代码
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import classification_report, confusion_matrix
import xgboost as xgb
# 1. 数据加载与预处理
def load_and_prepare_data():
from sklearn.datasets import load_wine
data = load_wine()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = data.target
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
return X_train, X_test, y_train, y_test, data.target_names
# 2. 模型训练
def train_xgboost_model(X_train, y_train, X_test, y_test):
# 创建模型
model = xgb.XGBClassifier(
n_estimators=100,
max_depth=4,
learning_rate=0.1,
subsample=0.8,
colsample_bytree=0.8,
random_state=42
)
# 训练
model.fit(X_train, y_train)
# 预测
y_pred = model.predict(X_test)
return model, y_pred
# 3. 模型评估
def evaluate_model(model, y_test, y_pred, target_names):
print("=== 模型评估报告 ===")
print(f"准确率: {np.mean(y_pred == y_test):.4f}")
print("\n分类报告:")
print(classification_report(y_test, y_pred, target_names=target_names))
# 特征重要性
importance = pd.DataFrame({
'feature': model.get_booster().feature_names,
'importance': model.feature_importances_
}).sort_values('importance', ascending=False)
print("\nTop 5 重要特征:")
print(importance.head())
return importance
# 4. 主函数
def main():
# 加载数据
X_train, X_test, y_train, y_test, target_names = load_and_prepare_data()
print(f"训练集大小: {X_train.shape}")
print(f"测试集大小: {X_test.shape}")
# 训练模型
model, y_pred = train_xgboost_model(X_train, y_train, X_test, y_test)
# 评估模型
importance = evaluate_model(model, y_test, y_pred, target_names)
# 可视化
plt.figure(figsize=(10, 6))
plt.barh(importance['feature'][:10], importance['importance'][:10])
plt.xlabel('重要性')
plt.title('XGBoost特征重要性')
plt.gca().invert_yaxis()
plt.show()
if __name__ == "__main__":
main()
常见XGBoost参数说明
| 参数 | 说明 | 推荐值 |
|---|---|---|
| n_estimators | 树的数量 | 100-1000 |
| max_depth | 树的最大深度 | 3-10 |
| learning_rate | 学习率 | 01-0.3 |
| subsample | 样本采样比例 | 6-1.0 |
| colsample_bytree | 特征采样比例 | 6-1.0 |
| gamma | 分裂最小损失减少 | 0-5 |
| reg_alpha | L1正则化 | 0-1 |
| reg_lambda | L2正则化 | 0-1 |
这些案例涵盖了XGBoost的基本用法、参数调优、特征重要性分析等核心功能,你可以根据实际需求调整参数和数据集。