• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

XGboost参数调优完整指南和预测附完整代码

武飞扬头像
噜噜啦啦咯
帮助1

目录

导入所需要的库

导入数据集并对其进行训练集和测试集的划分

调参步骤

树的最大深度以及最小叶子节点样本权重

gamma

subsample 和 colsample_bytree

调整正则项

学习速率

打印出最优模型

绘制 XGBoost 模型的 feature_importance 图像

 用此模型进行预测

训练模型

 得出预测图像并打印出RMSE


XGBoost 的模型建立将主要依靠陈天奇的 XGBoost 类库,参数的调优主要基于 python sklearn 类库的网格搜索方法选择最优的超参数。

导入所需要的库

  1.  
    from xgboost import XGBRegressor as XGBR
  2.  
    from sklearn.model_selection import KFold, cross_val_score as CVS, train_test_split as TTS
  3.  
    import matplotlib.pyplot as plt
  4.  
    from sklearn.metrics import mean_squared_error as MSE
  5.  
    from sklearn.model_selection import GridSearchCV
  6.  
    import pandas as pd
  7.  
    from numpy import nan as NA
  8.  
    import pickle

导入数据集并对其进行训练集和测试集的划分

  1.  
    data = pd.read_excel(r'C:\Users\HUAWEI\Desktop\pollution.xlsx')
  2.  
    X = data.iloc[:,1:7]
  3.  
    Y = data.iloc[:,0]
  4.  
    Xtrain,Xtest,Ytrain,Ytest = TTS(X,Y,test_size=0.1,random_state=420)

调参步骤

树的最大深度以及最小叶子节点样本权重

首先对这个值为树的最大深度以及最小叶子节点样本权重和这个组合进行调整。最大深度控 制了树的结构,最小叶子节点样本权重这个参数用于避免过拟合。当它的值较大时,可以避免模 型学习到局部的特殊样本。但是如果这个值过高,会导致欠拟合。

  1.  
    param_test1 = {'max_depth':range(3,10,2),'min_child_weight':range(2,7,2)}
  2.  
     
  3.  
    gsearch1 = GridSearchCV(estimator =XGBR( learning_rate =0.1, n_estimators=140, max_depth=5,
  4.  
    min_child_weight=1, gamma=0, subsample=0.8, colsample_bytree=0.8, objective= 'reg:linear',
  5.  
    nthread=4, scale_pos_weight=1, seed=27),
  6.  
    param_grid = param_test1, scoring='r2',n_jobs=4, cv=5)
  7.  
    gsearch1.fit(Xtrain,Ytrain)
  8.  
    gsearch1.best_params_, gsearch1.best_score_

gamma

再对参数 gamma 进行调整。在 XGBoost 节点分裂时,只有分裂后损失函数的值下降了,才 会分裂这个节点。gamma 指定了节点分裂所需的最小损失函数下降值。这个参数的大小决定了 模型的保守程度。参数越高,模型越不保守。

param_test3 = {'gamma':[i/100.0 for i in range(0,100)]}

subsample 和 colsample_bytree

再对参数 subsample 和 colsample_bytree 进行调整。subsample 控制对于每棵树的随机采样的 比例。减小这个参数的值,算法会更加保守,避免过拟合。但是,如果这个值设置得过小,它可 能会导致欠拟合。colsample_bytree 用来控制每棵随机采样的列数的占比(每一列是一个特征)。

param_test4 = {'subsample':[i/10.0 for i in range(1,10)],'colsample_bytree':[i/10.0 for i in range(1,10)]}

调整正则项

接着再对模型的 gamma 参数进行调整,控制模型的正则项,防止出现过拟合的现象。

param_test5 = {'reg_alpha':[0, 0.001, 0.005, 0.01, 0.05]}

学习速率

最后进行学习速率的调整,选择最优的学习速率最终确定适合的模型。

param_test6 = {'learning_rate':[0, 0.001, 0.005, 0.01, 0.05,0.1,0.5,1]}

打印出最优模型

  1.  
    XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
  2.  
    colsample_bynode=1, colsample_bytree=0.8, enable_categorical=False,
  3.  
    gamma=0, gpu_id=-1, importance_type=None,
  4.  
    interaction_constraints='', learning_rate=0.1, max_delta_step=0,
  5.  
    max_depth=3, min_child_weight=2, missing=nan,
  6.  
    monotone_constraints='()', n_estimators=140, n_jobs=4, nthread=4,
  7.  
    num_parallel_tree=1, objective='reg:linear', predictor='auto',
  8.  
    random_state=27, reg_alpha=0, reg_lambda=1, scale_pos_weight=1,
  9.  
    seed=27, subsample=0.6, tree_method='exact', validate_parameters=1, ...)

绘制 XGBoost 模型的 feature_importance 图像

importance [0.19813064 0.13919306 0.09813585 0.04205703 0.02167364 0.50080985]

学新通

 用此模型进行预测

训练模型

  1.  
    params = { 'base_score':0.5, 'booster':'gbtree', 'colsample_bylevel':1,
  2.  
    'colsample_bynode':1, 'colsample_bytree':0.8, 'enable_categorical':False,
  3.  
    'gamma':0, 'gpu_id':-1, 'importance_type':None,
  4.  
    'interaction_constraints':'','learning_rate':0.1, 'max_delta_step':0,
  5.  
    'max_depth':3, 'min_child_weight':2,
  6.  
    'monotone_constraints':'()', 'n_estimators':140,'n_jobs':4, 'nthread':4,
  7.  
    'num_parallel_tree': 1, 'objective':'reg:linear', 'predictor:':'auto',
  8.  
    'random_state':27, 'reg_alpha':0,'reg_lambda':1,'scale_pos_weight':1,
  9.  
    'seed':27, 'subsample':0.6, 'tree_method':'exact','validate_parameters':1}
  10.  
     
  11.  
    dtrain = xgb.DMatrix(X_train, y_train)
  12.  
    num_rounds = 300
  13.  
    plst = list(params.items())
  14.  
    model = xgb.train(plst, dtrain, num_rounds)

 得出预测图像并打印出RMSE

学新通

得出RMSE:9.473,拟合效果不错

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcffif
系列文章
更多 icon
同类精品
更多 icon
继续加载