XGBoost 五拆交叉验证代码
时间: 2024-08-16 16:08:40 浏览: 54
XGBoost是一个流行的梯度提升库,它可以在Python中通过`xgboost`模块轻松应用。要进行五折交叉验证(5-Fold Cross Validation),首先你需要安装必要的库并导入它们:
```python
import xgboost as xgb
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
```
然后,你可以按照以下步骤编写代码:
```python
# 假设df_data是你加载的数据集,y是目标变量,X是特征
X = df_data.drop('target', axis=1)
y = df_data['target']
# 定义模型
model = xgb.XGBRegressor() # 如果是分类任务,可以改为XGBClassifier()
# 设置交叉验证
kf = KFold(n_splits=5, shuffle=True, random_state=42)
# 初始化评分列表用于记录每次验证的结果
scores = []
for train_index, val_index in kf.split(X):
# 划分训练集和验证集
X_train, y_train = X.iloc[train_index], y.iloc[train_index]
X_val, y_val = X.iloc[val_index], y.iloc[val_index]
# 训练模型
model.fit(X_train, y_train)
# 预测
predictions = model.predict(X_val)
# 计算得分(这里以均方误差为例)
score = mean_squared_error(y_val, predictions)
scores.append(score)
# 平均得分
mean_score = sum(scores) / len(scores)
# 打印平均交叉验证得分
print(f"5-Fold CV Mean Score: {mean_score}")
阅读全文