贝叶斯优化xgboost参数寻优代码
时间: 2023-07-27 21:07:27 浏览: 88
以下是使用Python中的`bayesian-optimization`库进行XGBoost参数寻优的示例代码:
```python
import pandas as pd
import xgboost as xgb
from sklearn.model_selection import cross_val_score
from bayes_opt import BayesianOptimization
# 加载数据
data = pd.read_csv('data.csv')
X, y = data.iloc[:, :-1], data.iloc[:, -1]
# 定义目标函数
def xgb_cv(max_depth, learning_rate, n_estimators, gamma, min_child_weight, subsample, colsample_bytree):
model = xgb.XGBClassifier(
max_depth=int(max_depth),
learning_rate=learning_rate,
n_estimators=int(n_estimators),
gamma=gamma,
min_child_weight=min_child_weight,
subsample=subsample,
colsample_bytree=colsample_bytree,
random_state=42
)
score = cross_val_score(model, X, y, scoring='accuracy', cv=5).mean()
return score
# 定义参数空间
pbounds = {
'max_depth': (3, 7),
'learning_rate': (0.01, 0.3),
'n_estimators': (50, 200),
'gamma': (0, 1),
'min_child_weight': (1, 10),
'subsample': (0.5, 1),
'colsample_bytree' :(0.5, 1)
}
# 构建贝叶斯优化模型
optimizer = BayesianOptimization(
f=xgb_cv,
pbounds=pbounds,
random_state=42
)
# 迭代优化
optimizer.maximize(init_points=10, n_iter=30)
# 输出最优参数组合和模型性能指标
print(optimizer.max)
```
在上述代码中,首先加载数据并定义目标函数`xgb_cv`,然后定义参数空间`pbounds`。接下来通过`BayesianOptimization`类构建贝叶斯优化模型,并通过`maximize`方法进行迭代优化。最后输出最优参数组合和模型性能指标。