大杀器xgboost指南

来源:互联网 发布:校园网络需求规格说明 编辑:程序博客网 时间:2024/05/28 11:29

本文不做深入探讨,仅供自己备忘

原文:http://www.analyticsvidhya.com/blog/2016/03/complete-guide-parameter-tuning-xgboost-with-codes-python/

一.xgboost的优点

1.正则化

xgboost在代价函数里加入了正则项,用于控制模型的复杂度。正则项里包含了树的叶子节点个数、每个叶子节点上输出的score的L2模的平方和。从Bias-variance tradeoff角度来讲,正则项降低了模型的variance,使学习出来的模型更加简单,防止过拟合,这也是xgboost优于传统GBDT的一个特性。

2.并行处理

xgboost工具支持并行。boosting不是一种串行的结构吗?怎么并行的?注意xgboost的并行不是tree粒度的并行,xgboost也是一次迭代完才能进行下一次迭代的(第t次迭代的代价函数里包含了前面t-1次迭代的预测值)。xgboost的并行是在特征粒度上的。我们知道,决策树的学习最耗时的一个步骤就是对特征的值进行排序(因为要确定最佳分割点),xgboost在训练之前,预先对数据进行了排序,然后保存为block结构,后面的迭代中重复地使用这个结构,大大减小计算量。这个block结构也使得并行成为了可能,在进行节点的分裂时,需要计算每个特征的增益,最终选增益最大的那个特征去做分裂,那么各个特征的增益计算就可以开多线程进行。

3.灵活性

xgboost支持用户自定义目标函数和评估函数,只要目标函数二阶可导就行。

4.缺失值的处理

对于特征的值有缺失的样本,xgboost可以自动学习出它的分裂方向

5.剪枝

XGBoost 先从顶到底建立所有可以建立的子树,再从底到顶反向进行剪枝。比起GBM,这样不容易陷入局部最优解

6.内置交叉验证

xgb.cv() 是不是感觉很方便

二.xgboost的参数 

转自http://blog.csdn.net/wzmsltw/article/details/50994481

XGBoost的参数可以分为三种类型:通用参数、booster参数以及学习目标参数

  • General parameters:参数控制在提升(boosting)过程中使用哪种booster,常用的booster有树模型(tree)和线性模型(linear model)。
  • Booster parameters:这取决于使用哪种booster。
  • Learning Task parameters:控制学习的场景,例如在回归问题中会使用不同的参数控制排序。
  • 除了以上参数还可能有其它参数,在命令行中使用

General Parameters

  • booster [default=gbtree] 
    • 有两中模型可以选择gbtree和gblinear。gbtree使用基于树的模型进行提升计算,gblinear使用线性模型进行提升计算。缺省值为gbtree
  • silent [default=0] 
    • 取0时表示打印出运行时信息,取1时表示以缄默方式运行,不打印运行时信息。缺省值为0
    • 建议取0,过程中的输出数据有助于理解模型以及调参。另外实际上我设置其为1也通常无法缄默运行。。
  • nthread [default to maximum number of threads available if not set] 
    • XGBoost运行时的线程数。缺省值是当前系统可以获得的最大线程数
    • 如果你希望以最大速度运行,建议不设置这个参数,模型将自动获得最大线程
  • num_pbuffer [set automatically by xgboost, no need to be set by user] 
    • size of prediction buffer, normally set to number of training instances. The buffers are used to save the prediction results of last boosting step.
  • num_feature [set automatically by xgboost, no need to be set by user] 
    • boosting过程中用到的特征维数,设置为特征个数。XGBoost会自动设置,不需要手工设置

Booster Parameters

From xgboost-unity, the bst: prefix is no longer needed for booster parameters. Parameter with or without bst: prefix will be equivalent(i.e. both bst:eta and eta will be valid parameter setting) .

Parameter for Tree Booster

  • eta [default=0.3] 
    • 为了防止过拟合,更新过程中用到的收缩步长。在每次提升计算之后,算法会直接获得新特征的权重。 eta通过缩减特征的权重使提升计算过程更加保守。缺省值为0.3
    • 取值范围为:[0,1]
    • 通常最后设置eta为0.01~0.2
  • gamma [default=0] 
    • minimum loss reduction required to make a further partition on a leaf node of the tree. the larger, the more conservative the algorithm will be.
    • range: [0,∞]
    • 模型在默认情况下,对于一个节点的划分只有在其loss function 得到结果大于0的情况下才进行,而gamma 给定了所需的最低loss function的值
    • gamma值是的算法更conservation,且其值依赖于loss function ,在模型中应该进行调参。
  • max_depth [default=6] 
    • 数的最大深度。缺省值为6
    • 取值范围为:[1,∞]
    • 指树的最大深度
    • 树的深度越大,则对数据的拟合程度越高(过拟合程度也越高)。即该参数也是控制过拟合
    • 建议通过交叉验证(xgb.cv ) 进行调参
    • 通常取值:3-10
  • min_child_weight [default=1] 
    • 孩子节点中最小的样本权重和。如果一个叶子节点的样本权重和小于min_child_weight则拆分过程结束。在现行回归模型中,这个参数是指建立每个模型所需要的最小样本数。该成熟越大算法越conservative。即调大这个参数能够控制过拟合。
    • 取值范围为: [0,∞]
  • max_delta_step [default=0] 
    • 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. Usually this parameter is not needed, but it might help in logistic regression when class is extremely imbalanced. Set it to value of 1-10 might help control the update
    • 取值范围为:[0,∞]
    • 如果取值为0,那么意味着无限制。如果取为正数,则其使得xgboost更新过程更加保守。
    • 通常不需要设置这个值,但在使用logistics 回归时,若类别极度不平衡,则调整该参数可能有效果
  • subsample [default=1] 
    • 用于训练模型的子样本占整个样本集合的比例。如果设置为0.5则意味着XGBoost将随机的冲整个样本集合中随机的抽取出50%的子样本建立树模型,这能够防止过拟合。
    • 取值范围为:(0,1]
  • colsample_bytree [default=1] 
    • 在建立树时对特征随机采样的比例。缺省值为1
    • 取值范围:(0,1]
  • colsample_bylevel[default=1]
    • 决定每次节点划分时子样例的比例
    • 通常不使用,因为subsample和colsample_bytree已经可以起到相同的作用了
  • scale_pos_weight[default=0]
    • A value greater than 0 can be used in case of high class imbalance as it helps in faster convergence.
    • 大于0的取值可以处理类别不平衡的情况。帮助模型更快收敛

Parameter for Linear Booster

  • lambda [default=0] 
    • L2 正则的惩罚系数
    • 用于处理XGBoost的正则化部分。通常不使用,但可以用来降低过拟合
  • alpha [default=0] 
    • L1 正则的惩罚系数
    • 当数据维度极高时可以使用,使得算法运行更快。
  • lambda_bias 
    • 在偏置上的L2正则。缺省值为0(在L1上没有偏置项的正则,因为L1时偏置不重要)

Task Parameters

  • objective [ default=reg:linear ] 
    • 定义学习任务及相应的学习目标,可选的目标函数如下:
    • “reg:linear” –线性回归。
    • “reg:logistic” –逻辑回归。
    • “binary:logistic” –二分类的逻辑回归问题,输出为概率。
    • “binary:logitraw” –二分类的逻辑回归问题,输出的结果为wTx。
    • “count:poisson” –计数问题的poisson回归,输出结果为poisson分布。
    • 在poisson回归中,max_delta_step的缺省值为0.7。(used to safeguard optimization)
    • “multi:softmax” –让XGBoost采用softmax目标函数处理多分类问题,同时需要设置参数num_class(类别个数)
    • “multi:softprob” –和softmax一样,但是输出的是ndata * nclass的向量,可以将该向量reshape成ndata行nclass列的矩阵。没行数据表示样本所属于每个类别的概率。
    • “rank:pairwise” –set XGBoost to do ranking task by minimizing the pairwise loss
  • base_score [ default=0.5 ] 
    • the initial prediction score of all instances, global bias
  • eval_metric [ default according to objective ] 
    • 校验数据所需要的评价指标,不同的目标函数将会有缺省的评价指标(rmse for regression, and error for classification, mean average precision for ranking)
    • 用户可以添加多种评价指标,对于Python用户要以list传递参数对给程序,而不是map参数list参数不会覆盖’eval_metric’
    • The choices are listed below:
    • “rmse”: root mean square error
    • “logloss”: negative log-likelihood
    • “error”: Binary classification error rate. It is calculated as #(wrong cases)/#(all cases). For the predictions, the evaluation will regard the instances with prediction value larger than 0.5 as positive instances, and the others as negative instances.
    • “merror”: Multiclass classification error rate. It is calculated as #(wrong cases)/#(all cases).
    • “mlogloss”: Multiclass logloss
    • “auc”: Area under the curve for ranking evaluation.
    • “ndcg”:Normalized Discounted Cumulative Gain
    • “map”:Mean average precision
    • “ndcg@n”,”map@n”: n can be assigned as an integer to cut off the top positions in the lists for evaluation.
    • “ndcg-“,”map-“,”ndcg@n-“,”map@n-“: In XGBoost, NDCG and MAP will evaluate the score of a list without any positive samples as 1. By adding “-” in the evaluation metric XGBoost will evaluate these score as 0 to be consistent under some conditions. 
      training repeatively
  • seed [ default=0 ] 
    • 随机数的种子。缺省值为0
    • 可以用于产生可重复的结果(每次取一样的seed即可得到相同的随机划分)

参考:http://2hwp.com/2016/05/07/XGBoost%E6%B5%85%E5%85%A5%E6%B5%85%E5%87%BA/

2 0
原创粉丝点击