数据挖掘竞赛预测模型——贝叶斯参数搜索

bayes_opt库实现贝叶斯自动调参

本案例使用xgboost算法实现bayes_opt自动搜索调参

import pandas as pd
import xgboost as xgb
from sklearn.model_selection import cross_val_score
from bayes_opt import BayesianOptimization

train_data = pd.read_csv('...')
train_label = pd.read_csv('...')
test_data = pd.read_csv('...')

def xgb_cv(n_estimators,learning_rate,max_depth,min_child_weigth,gamma,subsample,colsample_bytree,reg_alpha,reg_lambda):
    val = cross_val_score(    xgb.XGBRegressor( boosting_type='gbdt',
                                            objective='regression',
                                            n_estimators=int(n_estimators),
                                            learning_rate=learning_rate,
                                            max_depth=int(max_depth),
                                            min_child_weigth=min_child_weigth,
                                            gamma=gamma,
                                            subsamples=subsample,
                                            colsample_bytree=colsample_bytree,
                                            reg_alpha=reg_alpha,
                                            reg_lambda=reg_lambda)
                               train_data, 
                               train_label,
                               scoring='neg_mean_absolute_error',
                               cv=5,
                               n_jobs=4).mean()
    return val
xgb_bayes = BayesianOptimization(   xgb_cv,
                                    {'n_estimators':(20,500),
                                     'learning_rate':(0,0.5),
                                     'max_depth':(3,20),
                                     'min_child_weigth':(1,10),
                                     'gamma':(0,3),
                                     'subsample':(0.5,1),
                                     'colsample_bytree':(0.6,1),
                                     'reg_alpha':(0,5),
                                     'reg_lambda':(0,5)})
xgb_bayes.maximize()     #开始训练
result = xgb_bayes.max     #得到最佳分数以及最佳分数对应的参数,为字典类型
result_params = result['params']     #提取最佳参数,为字典类型

 

你可能感兴趣的:(数据挖掘竞赛预测模型——贝叶斯参数搜索)