搜档网
当前位置:搜档网 › gboost算法原理与实战.doc

gboost算法原理与实战.doc

gboost算法原理与实战.doc
gboost算法原理与实战.doc

xgboost 算法原理与实战

前言:

xgboost 是大规模并行boosted tree 的工具,它是目前最快最好的开源boosted tree 工具包,

比常见的工具包快10 倍以上。在数据科学方面,有大量kaggle 选手选用它进行数据挖掘比

赛,其中包括两个以上kaggle 比赛的夺冠方案。在工业界规模方面,xgboost 的分布式版本

有广泛的可移植性,支持在YARN, MPI, Sungrid Engine 等各个平台上面运行,并且保留了单

机并行版本的各种优化,使得它可以很好地解决于工业界规模的问题。

花了几天时间粗略地看完了xgboost 原论文和作者的 slide 讲解,仅仅是入门入门入门笔记。

给我的感觉就是xgboost 算法

相关文献资料:Xgboost Slides XGBoost 中文版原理介绍原始论文 XGBoost: A Scalable Tree

Boosting System XGBoost Parameters (official guide)

精彩博文: XGBoost 浅入浅出—— wepon xgboost: 速度快效果好的boosting 模型 Complete

Guide to Parameter Tuning in XGBoost (with codes in Python)

一、 xgboost 基本原理介绍

1.提升方法是一种非常有效的机器学习方法,在前几篇笔记中介绍了提升树与GBDT基本原

理, xgboost( eXtreme Gradient Boosting )可以说是提升方法的完全加强版本。xgboost 算法

在各大比赛中展现了强大的威力,引用原论文中的一段描述:

The impact of the system has been widely recognized in a number of machine learning and data

mining challenges. Take the challenges hosted by the machine learning competition site Kaggle

for example. Among the 29 challenge winning solutions published at Kaggle ’ s blog during 2015, 17 solutions used XGBoost. Among these solutions, eight solely used XGBoost to train the

model,while most others combined XGBoost with neural nets in ensembles. For comparison, the

second most popular method,deep neural nets, was used in 11 solutions. The success of the

system was also witnessed in KDDCup 2015, where XGBoost was used by every winning team in

the top -10.Moreover, the winning teams reported that ensemble methods outperform a

well -configured XGBoost by only a small amount.

2.Regression Tree and Ensemble (What are we Learning,得到学习目标)

(1) .Regression Tree (CART)回归树

(2) .Regression Tree Ensemble 回归树集成

在上面的例子中,我们用两棵树来进行预测。我们对于每个样本的预测结果就是每棵树预测分

数的和。

(3) .Objective for Tree Ensemble 得到学习目标函数

这里是构造一个目标函数,然后我们要做的就是去尝试优化这个目标函数。读到这里,感觉

与gbdt 好像没有什么区别,确实如此,不过在后面就能看到他们的不同了(构造(学习)

模型参数)。

3.Gradient Boosting (How do we Learn ,如何学习 )

(1) .So How do we Learn? 目标函数:

(2) .Additive Training

(3) .Taylor Expansion Approximation of Loss 泰勒近似展开

把平方损失函数的一二次项带入原目标函数,你会发现与之前那张ppt 的损失函数是一致的(4) .Our New Goal 得到新的学习目标函数

从这里就可以看出xgboost 的特点了,目标函数保留了泰勒展开的二次项。

(5) .Refine the definition of tree重新定义每棵树

(6) .Define the Complexity of Tree 树的复杂度项

L2 正则化项,针对每个叶结点从图中可以看出, xgboost 算法中对树的复杂度项增加了一个

的得分增加 L2 平滑,目的也是为了避免过拟合。

(7) .Revisit the Objectives

(8).The Structure Score 这个score 你可以理解成类似于信息增益的一个指标,在切分点查找算法中用到。

(9)切分点查找算法(贪心算法)

上图中 G 都是各自区域内的 gi 总和,此外,作者针对算法设计对特征进行了排序,有兴趣的

可以阅读原始论文,这里不做详解。

二、 xgboost 特点(与 gbdt 对比)

说明一下:这部分内容参考了知乎上的一个问答—机器学习算法中 GBDT 和 XGBOOST的区别有哪些?,答主是 wepon 大神,根据他的总结我自己做了一理解和补充。

1.传统 GBDT以 CART作为基分类器, xgboost 还支持线性分类器,这个时候xgboost 相当于带 L1 和 L2 正则化项的逻辑斯蒂回归(分类问题)或者线性回归(回归问题)。—可以通过booster [default=gbtree] 设置参数 :gbtree: tree -based models/gblinear: linear models

2.传统 GBDT在优化时只用到一阶导数信息,xgboost 则对代价函数进行了二阶泰勒展开,同时用到了一阶和二阶导数。顺便提一下,xgboost 工具支持自定义代价函数,只要函数可一

阶和二阶求导。—对损失函数做了改进(泰勒展开,一阶信息 g 和二阶信息 h,上一章节有做介绍)3.xgboost 在代价函数里加入了正则项,用于控制模型的复杂度。正则项里包含了树的叶子

节点个数、每个叶子节点上输出的 score 的 L2 模的平方和。从 Bias-variance tradeoff 角度来讲,正则项降低了模型 variance,使学习出来的模型更加简单,防止过拟合,这也是 xgboost 优于传统GBDT 的一个特性—正则化包括了两个部分,都是为了防止过拟合,剪枝是都有的,叶子结点输出

L2 平滑是新增的。

4.shrinkage and column subsampling还是为了—防止过拟合,论文2.3节有介绍,这里答主已概括的非常到位

(1)shrinkage 缩减类似于学习速率,在每一步tree boosting 之后增加了一个参数n(权重),通过这种方式来减小每棵树的影响力,给后面的树提供空间去优化模型。

(2)column subsampling 列 (特征 )抽样,说是从随机森林那边学习来的,防止过拟合的效果

比传统的行抽样还好(行抽样功能也有),并且有利于后面提到的并行化处理算法。

5.split finding algorithms( 划分点查找算法):—理解的还不够透彻,需要进一步学习

(1) exact greedy algorithm 贪—心算法获取最优切分点

(2) approximate algorithm —近似算法,提出了候选分割点概念,先通过直方图算法获得

候选分割点的分布情况,然后根据候选分割点将连续的特征信息映射到不同的buckets 中,并统计汇总信息。详细见论文 3.3 节

(3)Weighted Quantile Sketch 分—布式加权直方图算法,论文 3.4 节这里的算法( 2)、( 3)是为了解决数据无法一次载入内存或者在分布式情况下算法( 1 )效率低的问题,以下引用

的还是 wepon 大神的总结:

可并行的近似直方图算法。树节点在进行分裂时,我们需要计算每个特征的每个分割点对应

的增益,即用贪心法枚举所有可能的分割点。当数据无法一次载入内存或者在分布式情况下,

贪心算法效率就会变得很低,所以xgboost 还提出了一种可并行的近似直方图算法,用于高

效地生成候选的分割点。

6.对缺失值的处理。对于特征的值有缺失的样本, xgboost 可以自动学习出它的分裂方向。—稀疏感知算法,论文 3.4 节, Algorithm 3: Sparsity -aware Split Finding

7.Built -in Cross-Validation (内置交叉验证)

XGBoost allows user to run a cross -validation at each iteration of the boosting process and thus

it is easy to get the exact optimum number of boosting iterations in a single run. This is unlike GBM where we have to run a grid -search and only a limited values can be tested.

8.continue on Existing Model (接着已有模型学习)

User can start training an XGBoost model from its last iteration of previous run. This can be of significant advantage in certain specific applications. GBM implementation of sklearn also has this feature so they are even on this point.

9.High Flexibility (高灵活性)

**XGBoost allow users to define custom optimization objectives and evaluation criteria. This

adds a whole new dimension to the model and there is no limit to what we can do.**

10.并行化处理—系统设计模块 ,块结构设计等

xgboost 工具支持并行。 boosting 不是一种串行的结构吗?怎么并行的?注意xgboost 的并行不是 tree 粒度的并行, xgboost 也是一次迭代完才能进行下一次迭代的(第t 次迭代的代价函数里包含了前面 t-1 次迭代的预测值)。xgboost 的并行是在特征粒度上的。我们知道,决

策树的学习最耗时的一个步骤就是对特征的值进行排序(因为要确定最佳分割点),xgboost 在训练之前,预先对数据进行了排序,然后保存为block 结构,后面的迭代中重复地使用这

个结构,大大减小计算量。这个block 结构也使得并行成为了可能,在进行节点的分裂时,

需要计算每个特征的增益,最终选增益最大的那个特征去做分裂,那么各个特征的增益计算

就可以开多线程进行。

此外 xgboost 还设计了高速缓存压缩感知算法,这是系统设计模块的效率提升。当梯度统计不适合于处理器高速缓存和高速缓存丢失时,会大大减慢切分点查找算法的速度。( 1)针对 exact greedy algorithm 采用缓存感知预取算法(2)针对approximate algorithms 选择合适的块大小

我觉得关于 xgboost 并行化设计仅仅从论文PPT博客上学习是远远不够的,有时间还要从代码层面去学习分布式xgboost 的设计理念。

三、 xgboost 参数详解

官方参数介绍看这里:Parameters (official guide)

General Parameters(常规参数) 1.booster [default=gbtree] :选择基分类器, gbtree: tree -based models/gblinear: linear models 2.silent [default=0]: 设置成 1 则没有运行信息输出,最好是设置为 0. 3.nthread [default to maximum number of threads available if not set] :线程数

Booster Parameters (模型参数) 1.eta [default=0.3]:shrinkage 参数,用于更新叶子节点权重

时,乘以该系数,避免步长过大。参数值越大,越可能无法收敛。把学习率eta 设置的小

一些,小学习率可以使得后面的学习更加仔细。 2.min_child_weight [default=1]: 这个参数默

认是 1,是每个叶子里面h 的和至少是多少,对正负样本不均衡时的0-1 分类而言,假设

h 在 0.01 附近, min_child_weight 为1 意味着叶子节点中最少需要包含100 个样本。这

个参数非常影响结果,控制叶子节点中二阶导的和的最小值,该参数值越小,越容易

overfitting 。 3.max_depth [default=6]: 每颗树的最大深度,树高越深,越容易过拟合。

4.max_leaf_nodes: 最大叶结点数,与max_depth 作用有点重合。

5.gamma [default=0] :后剪

枝时,用于控制是否后剪枝的参数。 6.max_delta_step [default=0] :这个参数在更新步骤中

起作用,如果取 0 表示没有约束,如果取正值则使得更新步骤更加保守。可以防止做太大的

更新步子,使更新更加平缓。7.subsample [default=1] :样本随机采样,较低的值使得算法

更加保守,防止过拟合,但是太小的值也会造成欠拟合。8.colsample_bytree [default=1] :

列采样,对每棵树的生成用的特征进行列采样.一般设置为: 0.5-1 https://www.sodocs.net/doc/0711483300.html,mbda [default=1] :控

制模型复杂度的权重值的L2 正则化项参数,参数越大,模型越不容易过拟合。10.alpha [default=0]: 控制模型复杂程度的权重值的L1 正则项参数,参数值越大,模型越不容易过拟

合。 11.scale_pos_weight [default=1] :如果取值大于 0 的话,在类别样本不平衡的情况下有

助于快速收敛。

Learning Task Parameters(学习任务参数) 1.objective [default=reg:linear] :定义最小化损失

函数类型,常用参数: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.

2.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. Typical values are: rmse

mean square error mae – meanabsolute 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 3.seed [default=0] : The random number seed. 随

机种子,用于产生可复现的结果Can be used for generating reproducible results and also for

parameter tuning.

注意 : Python sklearn style 参数名会有所变化eta –> learning_rate lambda –> reg_lambda alpha – > reg_alpha

四、实战

官方样例:XGBoost Python API Reference (official guide) XGBoost Demo Codes (xgboost GitHub repository)

xgboost 参数设置代码示例:

相关主题