xgboost参数调节

在做kaggle比赛的时候,尝试着使用xgboost模型来处理回归问题,效果还不错。这里来总结一下这个模型的调参过程。

文章也是对 Complete Guide to Parameter Tuning in XGBoost (with codes in Python) 的一个总结梳理。

1 集成学习

除了课本上的定义,我分享一下我个人对于集成学习的理解。集成学习就是通过构建相对简单的个体学习器来处理问题,再找到某种方式将这些个体学习器得到的结果结合起来。通过这种方式希望得到泛化能力不错的模型。

集成学习的效果之所以不错,是因为每个个体学习器(也称为基学习器)相对简单,所以单个基学习器不太容易出现过拟合现象。同时由于采取多个学习器也可以一定程度上弥补单个学习器精度不够的情况。当然这对于基学习器的个数以及每个学习器的能力,以及最后如何整合都有很大关系。源引《机器学习》所说的, “基学习器要好而不同”。

常见的集成学习方式主要有(但不限于):Bagging和Boosting。Bagging是并行集成学习的代表,Boosting是串行集成学习的代表。

2 XGBoost简介

Boosting又有很多种实现和变种。应用较多的有GradientBoost, XGBoost。XGBoost为什么被应用的这么多呢,其实主要是因为速度快而且效果也不错。XGBoost实现了并发运行。这点确实让人印象深刻。作为一种串行的继承学习,竟然实现了并发。这点我也正在努力学习,尝试理解他实现的内部机理,期待以后补充这块的知识。

3 XGBoost调参

本文针对的是XGBoosting调参。其实调参的大致思想是类似的。首先是模型关键参数的介绍。

3.1 XGBoost的参数主要分为三种(这里我就不翻译了):

  1. General Parameters: 控制总体的功能
  2. Booster Parameters: 控制单个学习器的属性
  3. Learning Task Parameters: 控制调优的步骤

3.2 XGBoost的参数详情

(1)General Parameters:

  • booster [default=gbtree]
    • 选择每一次迭代中,模型的种类. 有两个选择:
      • gbtree: 基于树的模型
      • gblinear: 线性模型
  • silent [default=0]:
    • 设为1 则不打印执行信息
    • I设为0打印信息
  • nthread [default to maximum number of threads available if not set]
    • 这个是设置并发执行的信息,设置在几个核上并发
    • 如果你希望在机器的所有可以用的核上并发执行,则采用默认的参数

(2)Booster Parameters

有2种booster,线性的和树的,一般树的booster较为常用。

  • eta [default=0.3]
    • 类似于GBM里面的学习率
    • 通过在每一步中缩小权重来让模型更加鲁棒
    • 一般常用的数值: 0.01-0.2
  • min_child_weight [default=1]
    • Defines the minimum sum of weights of all observations required in a child.
    • Used to control over-fitting. Higher values prevent a model from learning relations which might be highly specific to the particular sample selected for a tree.
    • 这个参数用来控制过拟合
    • Too high values can lead to under-fitting hence, it should be tuned using CV.
    • 如果数值太大可能会导致欠拟合
  • max_depth [default=6]
    • The maximum depth of a tree, same as GBM.
    • 控制子树中样本数占总的样本数的最低比例
    • 设置树的最大深度
    • Used to control over-fitting as higher depth will allow model to learn relations very specific to a particular sample.
    • 控制过拟合,如果树的深度太大会导致过拟合
    • Should be tuned using CV.
    • 应该使用CV来调节。
    • Typical values: 3-10
  • max_leaf_nodes
    • The maximum number of terminal nodes or leaves in a tree.
    • 叶子节点的最大值
    • Can be defined in place of max_depth. Since binary trees are created, a depth of ‘n’ would produce a maximum of 2^n leaves.
    • 也是为了通过树的规模来控制过拟合
    • If this is defined, GBM will ignore max_depth.
    • 如果叶子树确定了,对于2叉树来说高度也就定了,此时以叶子树确定的高度为准
  • gamma [default=0]
    • A node is split only when the resulting split gives a positive reduction in the loss function. Gamma specifies the minimum loss reduction required to make a split.
    • 如果分裂能够使loss函数减小的值大于gamma,则这个节点才分裂。gamma设置了这个减小的最低阈值。如果gamma设置为0,表示只要使得loss函数减少,就分裂
    • Makes the algorithm conservative. The values can vary depending on the loss function and should be tuned.
    • 这个值会跟具体的loss函数相关,需要调节
  • max_delta_step [default=0]
    • In maximum delta step we allow each tree’s weight estimation to be. If the value is set to 0, it means there is no constraint. If it is set to a positive value, it can help making the update step more conservative.
    • 如果参数设置为0,表示没有限制。如果设置为一个正值,会使得更新步更加谨慎。(关于这个参数我还是没有完全理解透彻。。。)
    • Usually this parameter is not needed, but it might help in logistic regression when class is extremely imbalanced.
    • 不是很经常用,但是在逻辑回归时候,使用它可以处理类别不平衡问题。
  • subsample [default=1]
    • Same as the subsample of GBM. Denotes the fraction of observations to be randomly samples for each tree.
    • 对原数据集进行随机采样来构建单个树。这个参数代表了在构建树时候 对原数据集采样的百分比。eg:如果设为0.8表示随机抽取样本中80%的个体来构建树。
    • Lower values make the algorithm more conservative and prevents overfitting but too small values might lead to under-fitting.
    • 相对小点的数值可以防止过拟合,但是过小的数值会导致欠拟合(因为采样过小)。
    • Typical values: 0.5-1
    • 一般取值 0.5 到 1
  • colsample_bytree [default=1]
    • Similar to max_features in GBM. Denotes the fraction of columns to be randomly samples for each tree.
    • 创建树的时候,从所有的列中选取的比例。e.g:如果设为0.8表示随机抽取80%的列 用来创建树
    • Typical values: 0.5-1
  • colsample_bylevel [default=1]
    • Denotes the subsample ratio of columns for each split, in each level.
    • I don’t use this often because subsample and colsample_bytree will do the job for you. but you can explore further if you feel so.
  • lambda [default=1]
    • L2 regularization term on weights (analogous to Ridge regression)
    • L2正则项,类似于Ridge Regression
    • This used to handle the regularization part of XGBoost. Though many data scientists don’t use it often, it should be explored to reduce overfitting.
    • 可以用来考虑降低过拟合,L2本身可以防止过分看重某个特定的特征。尽量考虑尽量多的特征纳入模型。
  • alpha [default=0]
    • L1 regularization term on weight (analogous to Lasso regression)
    • L1正则。 类似于lasso
    • Can be used in case of very high dimensionality so that the algorithm runs faster when implemented
    • L1正则有助于产生稀疏的数据,这样有助于提升计算的速度
  • scale_pos_weight [default=1]
    • A value greater than 0 should be used in case of high class imbalance as it helps in faster convergence.

(3)Learning Task Parameters

These parameters are used to define the optimization objective the metric to be calculated at each step.

  • objective [default=reg:linear]
    • This defines the loss function to be minimized. Mostly used values are:
      • binary:logistic –logistic regression for binary classification, returns predicted probability (not class)
      • multi:softmax –multiclass classification using the softmax objective, returns predicted class (not probabilities)
        • you also need to set an additional num_class (number of classes) parameter defining the number of unique classes
      • multi:softprob –same as softmax, but returns predicted probability of each data point belonging to each class.


  • eval_metric [ default according to objective ]
    • The metric to be used for validation data.
    • The default values are rmse for regression and error for classification.
    • 对于回归问题默认采用rmse,对于分类问题一般采用error
    • Typical values are:
      • 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]
    • The random number seed.
    • Can be used for generating reproducible results and also for parameter tuning.
    • 为了产生能过重现的结果。因为如果不设置这个种子,每次产生的结果都会不同。


If you’ve been using Scikit-Learn till now, these parameter names might not look familiar. A good news is that xgboost module in python has an sklearn wrapper called XGBClassifier. It uses sklearn style naming convention. The parameters names which will change are:

短语参数命名规则。现在在xgboost的module中,有一个sklearn的封装。在这个module中命名规则和sklearn的命名规则一致。

  1. eta –> learning_rate
  2. lambda –> reg_lambda
  3. alpha –> reg_alpha



3.3 调参

这里只是记录一下调参的步骤。

问题1:如果数据量不大的话可否用parm_grid罗列所有可能的参数,使用GridSearchCV来验证。将所有可能的组合都尝试一下?

我的思考:时间通常无法忍受,即使数据量不大,但是参数的排列组合也是很多中的。这就好比暴力遍历所有可能的参数组合空间。首先每个参数的取值范围是多少,每一步参数变化的粒度有多大?这都没法确定。当然如果我们指定了每个参数的取值范围而且粒度也定了,假设计算资源无限,并且样本和测试同分布,这个理论上是可行的。

这里面主要是说的一般调booster参数的思路

step1:设置一些初始值。

step2:保持learning rate和其他booster相关的参数不变,调节和estimators的参数。、

step3:保证estimator和其他的booster参数不变,调节learning rate

step4:保持estimator和learning rate不变,调节booste相关的参数。可以从影响最大的max_depth 和min_child_weight开始。逐步调节所有可能有影响的booster参数

step5: 缩小learning rate,得到最佳的learning rate值

step6:得到一组效果还不错的参数组合

编辑于 2017-08-25 11:26