GridSearchCV实例:对Xgboost回归任务进行网格调参

在Xgboost调参过程中,可以使用GridSearchCV()进行网格调参,不用很麻烦的进行手动调参。
下面这个例子是使用Xgboost进行回归任务时使用GridSearchCV().

import pandas as pd 
import numpy as np
from sklearn.model_selection import GridSearchCV #网格搜索
from sklearn.model_selection import train_test_split,KFold #数据集划分,交叉验证
import xgboost as xgb      #xgboost
import warnings
 
warnings.filterwarnings('ignore')
goal = 'people_index' #要预测的值
myid = 'ID'

#读入数据
train = pd.read_csv('D:/Xgboost/train.csv',index_col=0)
y_train = np.log1p(train[goal])
train.drop([myid,goal], axis=1, inplace=True)

x = train
y = y_train
 
seed = 7 
test_size = 0.10 
X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size=test_size, random_state=seed)
model = xgb.XGBRegressor()               
learning_rate = [0.01,0.05,0.1]   #学习率
n_estimators = [700, 900, 1100,1300]
max_depth = [6,10,15,20]
param_grid = dict(learning_rate = learning_rate,n_estimators = n_estimators,max_depth=max_depth)#转化为字典格式
 
kflod=KFold(n_splits=5,shuffle=True) #五折交叉验证
print("Begin Train")
grid_search = GridSearchCV(model,param_grid,scoring = 'neg_mean_squared_error',n_jobs = -1,cv = kflod)
#scoring是损失函数类型
grid_result = grid_search.fit(X_train, Y_train) 
print("Best: %f using %s" % (grid_result.best_score_,grid_search.best_params_))

means = grid_result.cv_results_['mean_test_score']
params = grid_result.cv_results_['params']
for mean,param in zip(means,params):
    print("%f  with:   %r" % (mean,param))
  • 1
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值