XGBoost算法原理简介及调参

译文:Complete Guide to Parameter Tuning in XGBoost

简介

当模型没有达到预期效果的时候,XGBoost就是数据科学家的最终武器。XGboost是一个高度复杂的算法,有足够的能力去学习数据的各种各样的不规则特征。

用XGBoost建模很简单,但是提升XGBoost的模型效果却需要很多的努力。因为这个算法使用了多维的参数。为了提升模型效果,调参就不可避免,但是想要知道参数怎么调,什么样的参数能够得出较优的模型输出就很困难了。

这篇文章适用于XGBoost新手,教会新手学习使用XGBoost的一些有用信息来帮忙调整参数。

What should you know?

XGBoost(extreme Gradient Boosting) 是一个高级的梯度增强算法(gradient boosting algorithm),推荐看一下我前一篇翻译的自该作者的文章

XGBoost 的优势

  • Regularization: 
    • 标准的GBM并没有XGBoost的Regularization,这个能帮助减少过拟合问题
  • Parallel Processing: 
    • XGBoost实现了并行计算,与GBM相比非常快
    • 但是基于序列的模型,新模型的建立是基于前面已经建立好的模型,如何能实现并行计算呢?探索一下吧
    • XGBoost 支持在Hadoop上实现
  • High Flexibility 
    • XGBoost允许用户定制优化目标和评价标准
    • 这一点将会使得我们对模型有很多可以改进的地方
  • Handling Missing Values 
    • XGBoost有内嵌的处理缺失值的程序
    • 其他模型中用户被要求为缺失值提供相应的与其他值不同的值去填充缺失值,XGBoost会尝试对缺失值进行分类,并学习这种分类
  • Tree Pruning: 
    • GBM会停止对一个节点进行分裂,当其计算到这个节点的split的loss是负数时,GBM是一个贪婪算法
    • XGBoost的分类取决于max_depth,当树的深度达到max_depth时,开始进行剪枝,移除没有正基尼(no positive gain)节点的split
    • 另一个优点是一个节点被分裂的时候loss为-2,当其二次分裂的时候loss可能为+10,GBM会停止该节点的分裂,XGBoost会进入到第二步,然后结合两个分裂的影响,最终为+8
  • Built-in Cross-Validation 
    • XGBoost允许每一个交叉验证实现boosting过程,因而通过一次run就能获得boosting迭代的优化量
    • 与GBM需要运营grid-search且需要限时值的范围获得优化量不同
  • Continue on Existing Model
  • 用户可以通过上个版本的XGBoost模型训练新版本的模型
  • GBM的sklearn也有这个特性

加深对XGBoost的理解的文章: 
1.XGBoost Guide – Introduction to Boosted Trees 
2.Words from the Author of XGBoost

XGBoost Parameters

XGBoost的变量类型有三类:

  • General Parameters:调控整个方程
  • Booster Parameters:调控每步树的相关变量
  • Learning Task Parameters:调控优化表现的变量

1.General Parameters:

  • booster [default=gbtree]:

    • gbtree: tree-based models,树模型
    • gblinear: linear models,线性模型
  • silent [default=0]:

    • 设置成1表示打印运行过程中的相关信息
    • 通常选择默认值就好,打印出的信息能够帮助理解model
  • nthread [default to maximum number of threads available if not set] 
    • 主要用于并行计算,系统的内核数需要作为变量
    • 如果希望运行所有的内核,就不需要设置该参数,程序会自己检测到该值

2.Booster Parameters

虽然XGBoost有两种boosters,作者在参数这一块只讨论了tree booster,原因是tree booster的表现总是好于 linear booster

  • eta [default=0.3] 
    • 与GBM中学习率的概念相似
    • 通过减小每一步的权重能够使得建立的模型更鲁棒
    • 通常最终的数值范围在[0.01-0.2]之间
  • min_child_weight [default=1] 
    • 定义观测样本生成的孩子节点的权重最小和
    • 这个概念与GBM中的min_child_leaf概念类似,但是又不完全一样,这个概念指的是某观测叶子节点中所有样本权重之和的最小值,而GBM指的是叶子节点的最少样本量
    • 用于防止过拟合问题:较大的值能防止过拟合,过大的值会导致欠拟合问题
    • 需要通过CV调参
  • max_depth [default=6] 
    • 树的最大深度
    • 用于防止过拟合问题
    • 通过CV调参
    • 通常值的范围:[3-10]
  • max_leaf_nodes 
    • 一棵树最多的叶子节点的数目
    • 与max_depth定义一个就好
  • gamma [default=0] 
    • 一个节点分裂的条件是其分裂能够起到降低loss function的作用,gamma 定义loss function降低多少才分裂
    • 这个变量使得算法变得保守,它的值取决于 loss function需要被调节
  • max_delta_step [default=0] 
    • 此变量的设置使得我们定义每棵树的权重估计值的变化幅度。如果值为0,值的变化没有限制,如果值>0,权重的变化将会变得相对保守
    • 通常这个参数不会被使用,但如果是极度不平衡的逻辑回归将会有所帮助
  • subsample [default=1]: 
    • 与GBM的subsample定义一样,指的是没有每棵树的样本比例
    • 低值使得模型更保守且能防止过拟合,但太低的值会导致欠拟合
    • 通常取值范围[0.5-1]
  • colsample_bytree [default=1] 
    • 与GBM中的max_features类似,指的是每棵树随机选取的特征的比例
    • 通常取值范围[0.5-1]
  • colsample_bylevel [default=1] 
    • 指的是树的每个层级分裂时子样本的特征所占的比例
    • 作者表示不用这个参数,因为subsample和colsample_bytree组合做的事与之类似
  • lambda [default=1] 
    • l2正则化权重的术语(同 Ridge regression)
    • 用于处理XGBoost里的正则化部分,虽然很多数据科学家不怎么使用这个参数,但是它可以用于帮助防止过拟合
  • alpha [default=0] 
    • l1正则化的权重术语(同Lasso regression)
    • 当特征量特别多的时候可以使用,这样能加快算法的运行效率
  • scale_pos_weight [default=1] 
    • 当样本不平衡时,需要设置一个大于0的数帮助算法尽快收敛

3.Learning Task Parameters

此类变量用于定义优化目标每一次计算的需要用到的变量

  • objective [default=reg:linear] 
    • 用于定义loss function,通常有以下几类
    • binary:logistic-用于二分类,返回分类的概率而不是类别(class)
    • multi:softmax-多分类问题,返回分类的类别而不是概率
    • multi:softprob-与softmax类似,但是返回样本属于每一类的概率
  • eval_metric [ default according to objective ] 
    • 这个变量用于 测试数据(validation data.)
    • 默认值:回归-rmse;分类-error
    • 通常值如下: 
      • rmse – root mean square error
      • mae – mean absolute error
      • logloss – negative log-likelihood
      • error – Binary classification error rate (0.5 threshold)
      • merror – Multiclass classification error rate
      • mlogloss – Multiclass logloss
      • auc: Area under the curve
  • seed [default=0] 
    • 随机种子的值

有些变量在Python的sklearn的接口中对应命名如下: 
1. eta -> learning rate 
2. lambda ->reg_lambda 
3. alpha -> reg_alpha

可能感到困惑的是这里并没有像GBM中一样提及n_estimators,这个参数实际存在于XGBClassifier中,但实际是通过num_boosting_rounds在我们调用fit函数事来体现的。

作者推荐以下链接,进一步加深对XGBOOST的了解: 
1.XGBoost Parameters (official guide) 
2.XGBoost Demo Codes (xgboost GitHub repository) 
3.Python API Reference (official guide)

XGBoost 调参步骤

#导入需要的数据和库
#Import libraries:
import pandas as pd
import numpy as np
import xgboost as xgb
from xgboost.sklearn import XGBClassifier
from sklearn import cross_validation, metrics   #Additional scklearn functions
from sklearn.grid_search import GridSearchCV   #Perforing grid search

import matplotlib.pylab as plt
%matplotlib inline
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 12, 4

train = pd.read_csv('train_modified.csv')
target = 'Disbursed'
IDcol = 'ID'
  •  

此处作者调用了两种类型的XGBoost: 
1.xgb:xgboost直接的库,可以调用cv函数 
2.XGBClassifier: sklearn对XGBoost的包装,可以允许使用sklearn的网格搜索功能进行并行计算

#定义一个函数帮助产生xgboost模型及其效果
def modelfit(alg, dtrain, predictors,useTrainCV=True, cv_folds=5, early_stopping_rounds=50):

    if useTrainCV:
        xgb_param = alg.get_xgb_params()
        xgtrain = xgb.DMatrix(dtrain[predictors].values, label=dtrain[target].values)
        cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params()['n_estimators'], nfold=cv_folds,
            metrics='auc', early_stopping_rounds=early_stopping_rounds, show_progress=False)
        alg.set_params(n_estimators=cvresult.shape[0])

    #Fit the algorithm on the data
    alg.fit(dtrain[predictors], dtrain['Disbursed'],eval_metric='auc')

    #Predict training set:
    dtrain_predictions = alg.predict(dtrain[predictors])
    dtrain_predprob = alg.predict_proba(dtrain[predictors])[:,1]

    #Print model report:
    print "\nModel Report"
    print "Accuracy : %.4g" % metrics.accuracy_score(dtrain['Disbursed'].values, dtrain_predictions)
    print "AUC Score (Train): %f" % metrics.roc_auc_score(dtrain['Disbursed'], dtrain_predprob)

    feat_imp = pd.Series(alg.booster().get_fscore()).sort_values(ascending=False)
    feat_imp.plot(kind='bar', title='Feature Importances')
    plt.ylabel('Feature Importance Score')

#xgboost’s sklearn没有feature_importances,但是#get_fscore() 有相同的功能
  •  

General Approach for Parameter Tuning

通常的做法如下: 
1.选择一个相对高一点的学习率(learning rate):通常0.1是有用的,但是根据问题的不同,可以选择范围在[0.05,0.3]之间,根据选好的学习率选择最优的树的数目,xgboost有一个非常有用的cv函数可以用于交叉验证并能返回最终的最优树的数目 
2.调tree-specific parameters(max_depth, min_child_weight, gamma, subsample, colsample_bytree) 
3.调regularization parameters(lambda, alpha) 
4.调低学习率并决定优化的参数

step1:Fix learning rate and number of estimators for tuning tree-based parameters

1.设置参数的初始值:

  • max_depth = 5 : [3,10],4-6都是不错的初始值的选择
  • min_child_weight = 1 : 如果数据是不平衡数据,初始值设置最好小于1
  • gamma = 0 : 初始值通常设置在0.1-0.2范围内,并且在后续的调参中也会经常被调节
  • subsample, colsample_bytree = 0.8 : 通常使用0.8作为调参的开始参数,调整范围为[0.5-0.9]
  • scale_pos_weight = 1:因为作者的数据为高度不平衡数据
#通过固定的学习率0.1和cv选择合适的树的数量
#Choose all predictors except target & IDcols
predictors = [x for x in train.columns if x not in [target, IDcol]]
xgb1 = XGBClassifier(
 learning_rate =0.1,
 n_estimators=1000,
 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)
modelfit(xgb1, train, predictors)
#作者调整后得到的树的值为140,如果这个值对于当前的系统而言太大了,可以调高学习率重新训练
  •  

step2:Tune max_depth and min_child_weight

先调这两个参数的原因是因为这两个参数对模型的影响做大

param_test1 = {
 'max_depth':range(3,10,2),
 'min_child_weight':range(1,6,2)
}
gsearch1 = GridSearchCV(estimator = XGBClassifier( learning_rate =0.1, n_estimators=140, 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=4,iid=False, cv=5)
gsearch1.fit(train[predictors],train[target])
gsearch1.grid_scores_, gsearch1.best_params_, gsearch1.best_score_
  •  

最优的 max_depth=5,min_child_weight=5 
因为之前的步长是2,在最优参数的基础上,在上调下调各一步,看是否能得到更好的参数

param_test2 = {
 'max_depth':[4,5,6],
 'min_child_weight':[4,5,6]
}
gsearch2 = GridSearchCV(estimator = XGBClassifier( learning_rate=0.1, n_estimators=140, max_depth=5,
 min_child_weight=2, gamma=0, subsample=0.8, colsample_bytree=0.8,
 objective= 'binary:logistic', nthread=4, scale_pos_weight=1,seed=27), 
 param_grid = param_test2, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch2.fit(train[predictors],train[target])
gsearch2.grid_scores_, gsearch2.best_params_, gsearch2.best_score_
  •  

以上结果跑出来的最优参数为:max_depth=4,min_child_weight=6,另外从作者跑出来的cv结果看,再提升结果比较困难,可以进一步对min_child_weight试着调整看一下效果:

param_test2b = {
 'min_child_weight':[6,8,10,12]
}
gsearch2b = GridSearchCV(estimator = XGBClassifier( learning_rate=0.1, n_estimators=140, max_depth=4,
 min_child_weight=2, gamma=0, subsample=0.8, colsample_bytree=0.8,
 objective= 'binary:logistic', nthread=4, scale_pos_weight=1,seed=27), 
 param_grid = param_test2b, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch2b.fit(train[predictors],train[target])
modelfit(gsearch3.best_estimator_, train, predictors)
gsearch2b.grid_scores_, gsearch2b.best_params_, gsearch2b.best_score_
  •  

step3:Tune gamma

param_test3 = {
 'gamma':[i/10.0 for i in range(0,5)]
}
gsearch3 = GridSearchCV(estimator = XGBClassifier( learning_rate =0.1, n_estimators=140, max_depth=4,
 min_child_weight=6, gamma=0, subsample=0.8, colsample_bytree=0.8,
 objective= 'binary:logistic', nthread=4, scale_pos_weight=1,seed=27), 
 param_grid = param_test3, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch3.fit(train[predictors],train[target])
gsearch3.grid_scores_, gsearch3.best_params_, gsearch3.best_score_
  •  

基于以上调好参数的前提下,可以看一下模型的特征的表现:

xgb2 = XGBClassifier(
 learning_rate =0.1,
 n_estimators=1000,
 max_depth=4,
 min_child_weight=6,
 gamma=0,
 subsample=0.8,
 colsample_bytree=0.8,
 objective= 'binary:logistic',
 nthread=4,
 scale_pos_weight=1,
 seed=27)
modelfit(xgb2, train, predictors)
  •  

step4: Tune subsample and colsample_bytree

param_test4 = {
 'subsample':[i/10.0 for i in range(6,10)],
 'colsample_bytree':[i/10.0 for i in range(6,10)]
}
gsearch4 = GridSearchCV(estimator = XGBClassifier( learning_rate =0.1, n_estimators=177, max_depth=4,
 min_child_weight=6, gamma=0, subsample=0.8, colsample_bytree=0.8,
 objective= 'binary:logistic', nthread=4, scale_pos_weight=1,seed=27), 
 param_grid = param_test4, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch4.fit(train[predictors],train[target])
gsearch4.grid_scores_, gsearch4.best_params_, gsearch4.best_score_
  •  
#上一步发现最优值均为0.8,这一步做的事情是在附近以0.05的步长做调整
param_test5 = {
 'subsample':[i/100.0 for i in range(75,90,5)],
 'colsample_bytree':[i/100.0 for i in range(75,90,5)]
}
gsearch5 = GridSearchCV(estimator = XGBClassifier( learning_rate =0.1, n_estimators=177, max_depth=4,
 min_child_weight=6, gamma=0, subsample=0.8, colsample_bytree=0.8,
 objective= 'binary:logistic', nthread=4, scale_pos_weight=1,seed=27), 
 param_grid = param_test5, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch5.fit(train[predictors],train[target])
  •  

Step 5: Tuning Regularization Parameters

这一步的作用是通过使用过regularization 来降低过拟合问题,大部分的人选择忽略这个参数,因为gamma 有提供类似的功能

param_test6 = {
 'reg_alpha':[1e-5, 1e-2, 0.1, 1, 100]
}
gsearch6 = GridSearchCV(estimator = XGBClassifier( learning_rate =0.1, n_estimators=177, max_depth=4,
 min_child_weight=6, gamma=0.1, subsample=0.8, colsample_bytree=0.8,
 objective= 'binary:logistic', nthread=4, scale_pos_weight=1,seed=27), 
 param_grid = param_test6, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch6.fit(train[predictors],train[target])
gsearch6.grid_scores_, gsearch6.best_params_, gsearch6.best_score_
  •  

这一步调参之后结果可能会变差,方法是在获得的最优的参数0.01附近进行微调,看能否获得更好的结果

param_test7 = {
 'reg_alpha':[0, 0.001, 0.005, 0.01, 0.05]
}
gsearch7 = GridSearchCV(estimator = XGBClassifier( learning_rate =0.1, n_estimators=177, max_depth=4,
 min_child_weight=6, gamma=0.1, subsample=0.8, colsample_bytree=0.8,
 objective= 'binary:logistic', nthread=4, scale_pos_weight=1,seed=27), 
 param_grid = param_test7, scoring='roc_auc',n_jobs=4,iid=False, cv=5)
gsearch7.fit(train[predictors],train[target])
gsearch7.grid_scores_, gsearch7.best_params_, gsearch7.best_score_
  •  

然后基于获得的更好的值,我们再看一下模型的整体表现

xgb3 = XGBClassifier(
 learning_rate =0.1,
 n_estimators=1000,
 max_depth=4,
 min_child_weight=6,
 gamma=0,
 subsample=0.8,
 colsample_bytree=0.8,
 reg_alpha=0.005,
 objective= 'binary:logistic',
 nthread=4,
 scale_pos_weight=1,
 seed=27)
modelfit(xgb3, train, predictors)
  •  

Step 6: Reducing Learning Rate

最后一步就是 降低学习率并增加更多的树

xgb4 = XGBClassifier(
 learning_rate =0.01,
 n_estimators=5000,
 max_depth=4,
 min_child_weight=6,
 gamma=0,
 subsample=0.8,
 colsample_bytree=0.8,
 reg_alpha=0.005,
 objective= 'binary:logistic',
 nthread=4,
 scale_pos_weight=1,
 seed=27)
modelfit(xgb4, train, predictors)
  •  

最后作者分享了两条经验: 
1.仅仅通过调参来提升模型的效果是很难的 
2.想要提升模型的效果,还可以通过特征工程、模型融合以及stacking方法

### XGBoost 算法原理及实现概述 #### 三、XGBoost 的基本概念与背景 XGBoost 是一种基于梯度提升决策树(Gradient Boosting Decision Tree, GBDT)的机器学习算法,旨在提高预测精度的同时保持较高的计算效率。作为一种增强版的 GBDT 实现,XGBoost 不仅继承了 GBDT 的核心思想,还在多个方面进行了改进和优化[^2]。 #### 四、XGBoost 的工作原理 XGBoost 使用加法模型逐步逼近真实目标值的过程可以描述为一系列弱学习器(通常是决策树)的组合。具体来说,每棵树都会尝试拟合前一棵树所遗留下来的残差误差,从而不断降低整体损失函数。以下是其主要组成部分: 1. **目标函数** XGBoost 的目标是最小化训练数据上的预测误差并加入正则项防止过拟合: \[ Obj(\theta) = L(\theta) + \Omega(\theta) \] 其中 \(L\) 表示损失函数,用于衡量当前模型输出与实际标签之间的差距;\(\Omega\) 则是对模型复杂性的惩罚因子,通常由叶子节点数量以及权重平方和构成[^3]。 2. **分裂准则** 在每次寻找最佳切分点时,XGBoost 计算增益来决定是否值得继续划分某个特征维度。该增益公式综合考虑了分裂前后样本分布变化带来的收益减去新增分支引入的成本开销: \[ Gain = \frac{1}{2} \left[\frac{G_L^2}{H_L+\lambda} + \frac{G_R^2}{H_R+\lambda} - \frac{(G_L+G_R)^2}{H_L+H_R+\lambda}\right]-\gamma \] 如果最终得到的结果大于零,则说明此次操作有效应予以保留[^6]。 3. **近似直方图方法** 为了进一步加快查找全局最优解的速度,XGBoost 引入了一种称为“近似直方图”的技术手段。这种方法先将连续型变量离散成若干区间桶再统计各范围内累计贡献值作为代表参与后续运算过程,极大地减少了不必要的重复比较次数[^5]。 #### 五、XGBoost 的特性亮点 - 支持多种目标任务类型,包括但不限于回归、分类乃至排序等问题; - 提供丰富的超参调节选项以便适应不同的业务场景需求; - 内置交叉验证功能方便快速评估模型表现好坏程度; - 天然具备处理缺失值的能力无需额外预处理步骤即可正常运作; - 可充分利用多核 CPU 并行加速整个迭代周期内的各项任务执行进度[^7]。 ```python import xgboost as xgb from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error # Load dataset and split into training/testing sets. data = load_boston() X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42) dtrain = xgb.DMatrix(X_train,label=y_train) dtest = xgb.DMatrix(X_test,label=y_test) params={'objective':'reg:squarederror', 'max_depth':3} bst=xgb.train(params,dtrain,num_boost_round=10) preds=bst.predict(dtest) rmse=np.sqrt(mean_squared_error(y_test,preds)) print("RMSE:", rmse) ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值