xgboost的调优
我想要给大家说明的是:不要幻想仅仅通过参数调优或者换一个稍微更好的模型使得最终结果有巨大的飞跃。要想最后的结果有巨大的提升,可以通过特征工程、模型集成来实现。
基本的
model2 = xgb.XGBRegressor(max_depth=6,learning_rate=0.05,n_estimators=100,min_child_weight=1,randam_state=42)
max_depth :每棵二叉树的最大深度,默认是6; 值越大,越容易过拟合,越小,容易欠拟合
learning_rate: 学习率
n_estumators: 基学习器个数
min_child_weight:默认值为1,。值越大,越容易欠拟合;值越小,越容易过拟合(值较大时,避免模型学习到局部的特殊样本)
gamma:系统默认为0,我们也常用0。 在节点分裂时,只有分裂后损失函数的值下降了,才会分裂这个节点。gamma指定了节点分裂所需的最小损失函数下降值。 这个参数的值越大,算法越保守。因为gamma值越大的时候,损失函数下降更多才可以分裂节点。所以树生成的时候更不容易分裂节点。范围: [0,∞]
subsample [default=1]:样本的采样率,如果设置成0.5,那么Xgboost会随机选择一般的样本作为训练集。
colsample_bytree [default=1]: 构造每棵树时,列采样率(一般是特征采样率)。
alpha [default=0, alias: reg_alpha]: L1正则化(与lasso回归中的正则化类似:传送门)这个主要是用在数据维度很高的情况下,可以提高运行速度。
调整max_depth 和 min_child_weight
使用网格搜索
GridSearchCV:
estimator: 分类器
param_grid: 参数值
cv: 交叉验证参数,默认None,使用三折交叉验证。指定fold数量,默认为3,也可以是yield训练/测试数据的生成器。
njobs: 线程为4
verbose:10输出进度
jupter 在n_jobs=4 不知为啥不输出进度 设置成1 即可输出
from sklearn.model_selection import GridSearchCV param_test1 = { 'max_depth':list(range(3,10,2)), 'min_child_weight':list(range(1,6,2)) } gsearch1 = GridSearchCV( estimator = XGBClassifier( learning_rate =0.1, n_estimators=20, max_depth=5,min_child_weight=1, gamma=0, subsample=0.8, colsample_bytree=0.8, objective= 'binary:logistic', nthread=4, scale_pos_weight=1, seed=27), param_grid = param_test1,# scoring='roc_auc',n_jobs=1,verbose=10,cv=5)gsearch1.fit(train[predictors],train[target]) gsearch1.best_params_
输出为: {'max_depth': 3, 'min_child_weight': 5}
因此 在 max_depth: 2,3,4. 及 min_child_weight:4,5,6 中搜索
进一步搜索
param_test2 = { 'max_depth':list(range(2,4,1)), 'min_child_weight':list(range(4,6,1)) } gsearch2 = GridSearchCV( estimator =xgb.XGBRegressor(max_depth=6,learning_rate=0.05,n_estimators=100,randam_state=42), param_grid = param_test2,n_jobs=1, verbose=10, cv=5)gsearch2.fit(df6.drop(['label','cust_wid'], axis=1), df6['label']) gsearch2.best_params_
搜索下来 最优的依然是 {'max_depth': 3, 'min_child_weight': 5}
调整gamma
gamma 从 0 到0.5
param_test3 = { 'gamma':[i/10.0 for i in range(0,5)] } gsearch3 = GridSearchCV( estimator =xgb.XGBRegressor(max_depth=3,learning_rate=0.05,min_child_weight=5,n_estimators=100,randam_state=42), param_grid = param_test3,n_jobs=1, verbose=10, cv=5)gsearch3.fit(df6.drop(['label','cust_wid'], axis=1), df6['label'])
最优Gamma:0.4
调整subsample 和colsample_bytree
param_test4 = { 'subsample':[i/100.0 for i in range(75,100,5)], 'colsample_bytree':[i/100.0 for i in range(75,100,5)] } gsearch4 = GridSearchCV( estimator =xgb.XGBRegressor(max_depth=3,learning_rate=0.05,min_child_weight=5,n_estimators=100,randam_state=42), param_grid = param_test4,n_jobs=1, verbose=10, cv=5)gsearch4.fit(df6.drop(['label','cust_wid'], axis=1), df6['label'])
输出最优{'colsample_bytree': 0.85, 'subsample': 0.75}
调整正则化参数
param_test5 = { 'reg_alpha':[1e-5, 1e-2, 0.1, 1, 100] } gsearch5 = GridSearchCV( estimator =xgb.XGBRegressor(max_depth=3,learning_rate=0.05,min_child_weight=5,n_estimators=100,randam_state=42,colsample_bytree=0.85, subsample=0.75), param_grid = param_test5,n_jobs=1, verbose=10, cv=5)gsearch5.fit(df6.drop(['label','cust_wid'], axis=1), df6['label'])
输出{'reg_alpha': 100}
调整学习率
param_test6 = { 'learning_rate':[0.005,0.01, 0.05, 0.1,0.5,1] } gsearch6 = GridSearchCV( estimator =xgb.XGBRegressor(max_depth=3,learning_rate=0.05,min_child_weight=5,n_estimators=100,randam_state=42,colsample_bytree=0.85, subsample=0.75), param_grid = param_test6,n_jobs=1, verbose=10, cv=5)gsearch6.fit(df6.drop(['label','cust_wid'], axis=1), df6['label'])
学习率最优: {'learning_rate': 0.05}
得到最优的模型 -- 结果
最终 结果提升了0.001 哈哈哈哈
名次从103到96上升了7名 感觉还是这句话
我想要给大家说明的是:不要幻想仅仅通过参数调优或者换一个稍微更好的模型使得最终结果有巨大的飞跃。要想最后的结果有巨大的提升,可以通过特征工程、模型集成来实现。
pred_y=model2.predict(df7.drop(['label','cust_wid'], axis=1)) y_pred=pred_y.astype(int) np.save("xgboost_best",y_pred)
xgboost的调优相关推荐
- ML之XGBoost:XGBoost参数调优的优秀外文翻译—《XGBoost中的参数调优完整指南(带python中的代码)》(四)
ML之XGBoost:XGBoost参数调优的优秀外文翻译-<XGBoost中的参数调优完整指南(带python中的代码)>(四) 目录 Step 3: Tune gamma步骤3:伽马微 ...
- ML之XGBoost:XGBoost参数调优的优秀外文翻译—《XGBoost中的参数调优完整指南(带python中的代码)》(三)
ML之XGBoost:XGBoost参数调优的优秀外文翻译-<XGBoost中的参数调优完整指南(带python中的代码)>(三) 目录 3. 参数微调案例/Parameter Tunin ...
- ML之XGBoost:XGBoost参数调优的优秀外文翻译—《XGBoost中的参数调优完整指南(带python中的代码)》(二)
ML之XGBoost:XGBoost参数调优的优秀外文翻译-<XGBoost中的参数调优完整指南(带python中的代码)>(二) 目录 2. xgboost参数/XGBoost Para ...
- ML之XGBoost:XGBoost参数调优的优秀外文翻译—《XGBoost中的参数调优完整指南(带python中的代码)》(一)
ML之XGBoost:XGBoost参数调优的优秀外文翻译-<XGBoost中的参数调优完整指南(带python中的代码)>(一) 目录 概述/Overview 介绍/Introducti ...
- XGBoost参数调优完全指南(附Python代码)
XGBoost参数调优完全指南(附Python代码) 译注:文内提供的代码和运行结果有一定差异,可以从这里下载完整代码对照参考.另外,我自己跟着教程做的时候,发现我的库无法解析字符串类型的特征,所以只 ...
- XGBoost参数调优完全指南(附Python代码)——转载
转载自https://www.cnblogs.com/mfryf/p/6293814.html 译注:文内提供的代码和运行结果有一定差异,可以从这里下载完整代码对照参考.另外,我自己跟着教程做的时候, ...
- [机器学习] XGBoost参数调优完全指南(附Python代码)
1. 简介 如果你的预测模型表现得有些不尽如人意,那就用XGBoost吧.XGBoost算法现在已经成为很多数据工程师的重要武器.它是一种十分精致的算法,可以处理各种不规则的数据. 构造一个使用XGB ...
- 决策树(十三)--XGBoost参数调优完全指南
转自:https://blog.csdn.net/han_xiaoyang/article/details/52665396 1. 简介 如果你的预测模型表现得有些不尽如人意,那就用XGBoost吧. ...
- python 分类变量xgboost_【转】XGBoost参数调优完全指南(附Python代码)
xgboost入门非常经典的材料,虽然读起来比较吃力,但是会有很大的帮助: 英文原文链接:https://www.analyticsvidhya.com/blog/2016/03/complete-g ...
最新文章
- OpenCV 笔记(06)— Mat 结构、像素值存储方法、创建 Mat 对象各种方法、Mat 对象的运算
- 改善AI性别偏见的4种方法 | CSDN博文精选
- 带动画效果的卷积神经网络的讲解.pptx
- C++_STL——deque and vector
- XML实现Android动画效果anim
- 台湾大学林轩田机器学习技法课程学习笔记4 -- Soft-Margin Support Vector Machine
- 参数--argumengs
- 韩国企业百强排行榜!
- (五)cobbler自定义系统安装
- BAT都不让多表 join?这是为什么?
- php递归函数的用法,PHP递归函数用法
- CVBS视频信号解析
- 自定义菜单 微信公众平台开发教程(2)
- Java 亚马逊Amazon spapi对接开发,java Sdk,授权和接口访问步骤详细说明
- [剑指Offer]-矩阵中的路径
- 基于SpringBoot的ERP系统,自带进销存+财务+生产功能
- 健康上链——医疗健康行业数字化转型的关键路径|链塔智库
- 使用beautifulsoup爬取丁香网评论
- 蓝桥杯书的笔记(二:接上篇蓝桥云课里的内容,C++)
- null、undefined、void、never