xgboost调参函数(GridSearchCV的使用)

xgboost调参函数(GridSearchCV的使用)

转并整理自[xgboost调参](https://segmentfault.com/a/1190000014040317),非常实用

1.简介

调参用的是sklearn的GridSearchCV,其中,x_train和y_train分别指的训练集的特征和标签。

在调参之前,有一个点需要注意。如果直接导入x_train和y_train,会报错,报too many indices in the array,所以需要:

# 链接:https://stackoverflow.com/questions/42928855/gridsearchcv-error-too-many-indices-in-the-array
# 
c, r = y_train_array.shape
y_train_ = y_train_array.reshape(c,)  # 只能识别(shape,)而不能(shape,1)

之后可运行函数,进行调参了。首先呈现函数,详细代码如下:

from sklearn.model_selection import GridSearchCV

def Tuning(cv_params, other_params):
    model2 = XGBClassifier(**other_params)
    optimized_GBM = GridSearchCV(estimator=model2, 
                                 param_grid=cv_params, 
                                 cv=5, 
                                 n_jobs=4)
    optimized_GBM.fit(x_train_array, y_train_)
    evalute_result = optimized_GBM.grid_scores_
    print('每轮迭代运行结果:{0}'.format(evalute_result))
    print('参数的最佳取值:{0}'.format(optimized_GBM.best_params_))
    print('最佳模型得分:{0}'.format(optimized_GBM.best_score_))

2.调参

此后,只需要往Tuning中输入两个参数,cv_params,和other_params,前面指的你要调优的参数,后面指的其他的参数。
xgboost参数简介见xgboost文档,常用参数

如对n_estimators进行调节,则:

cv_params = {'n_estimators':[700,800,900,1000,1100,1200,1300,1400]}
other_params = {
    'scale_pos_weight':scale_pos_weight,
    'eta':0.3,
    'learning_rate':0.07,
    'n_estimators':700,# 需调参 
    'max_depth':6, # 需调参 
    'min_child_weight':1, # 需调参 
    'gamma':0.2, # 需调参 
    'subsample':0.639, # 需调参 
    'colsample_bytree':0.2, # 需调参 
    'objective':'binary:logistic'
}
Tuning(cv_params, other_params)  # 一个调节完毕之后,相应改动other_params 中的参数设置即可

一个参数调节完毕后,相应改变other_params中的最优参数设置即可
代码中打#的都可以做类似过程进行调参

3. 一个问题

GridSearchCV调参好慢啊好慢,不知道有什么好的方法能快一点,待补充!

  • 3
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值