贝叶斯优化xgboost回归模型进行超参数寻优,训练好最优模型,并调用训练好的模型对预测集Dataset for Predict TC.CSV进行预测,给出详细代码
时间: 2024-03-03 13:51:16 浏览: 30
好的,以下是实现的详细代码:
```
import pandas as pd
import numpy as np
import xgboost as xgb
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from bayes_opt import BayesianOptimization
#读取数据集
df = pd.read_csv('Dataset for Train TC.CSV', index_col=0)
#划分训练集和验证集
train, valid = train_test_split(df, test_size=0.2, random_state=42)
#定义目标函数,即xgboost回归模型的交叉验证指标
def xgb_cv(max_depth, gamma, min_child_weight, subsample, colsample_bytree):
params = {
'nthread': -1,
'max_depth': int(max_depth),
'gamma': gamma,
'eta': 0.1,
'min_child_weight': min_child_weight,
'subsample': max(min(subsample, 1), 0),
'colsample_bytree': max(min(colsample_bytree, 1), 0),
'objective': 'reg:squarederror',
'eval_metric': 'rmse',
'seed': 42,
}
dtrain = xgb.DMatrix(train.drop('TC', axis=1), label=train['TC'])
cv_result = xgb.cv(params, dtrain, num_boost_round=1000, nfold=5, early_stopping_rounds=50)
return -cv_result['test-rmse-mean'].iloc[-1]
#定义超参数搜索空间
pbounds = {
'max_depth': (3, 7),
'gamma': (0, 1),
'min_child_weight': (1, 10),
'subsample': (0.5, 1),
'colsample_bytree': (0.5, 1),
}
#使用贝叶斯优化进行超参数寻优
optimizer = BayesianOptimization(f=xgb_cv,
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)