Decision trees (lower) -Xgboost

References (If insufficient for a thorough understanding of this article, you must read the following blog cognition, only better understand Xgboost):

1. understanding of xgboost of (references 1 and 4 are I think the understanding of Xgboost two articles summarize the most thorough, according to the authors summarize their paper!)

2. Manual reduction XGBoost instance process (An example is provided to allow readers to better understand the algorithm process)

3. Handwriting xgboost (python using handwriting implement XGB)

4. XGBoost ultra-detailed derivation (references 1 and 4 are I think the understanding of Xgboost two articles summarize the most thorough)


 

What is XGBoost?

XGBoost is Exterme Gradient Boosting (gradient limit lift) abbreviation, which is based on decision tree integrated machine learning algorithms , which improve the gradient (Gradient Boost) framework. XGBoost is developed by GBDT from, it is also a learning process to optimize the use of the additive model and algorithm step before , but GBDT there is a difference. The main differences include the following:

  • The objective function: XGBoost loss function adds complexity regularization term, using regular to control the model, regularization term contains the number of the leaf nodes of the tree, each leaf node weight (socre value leaf node) squared with.
  • Optimization: GBDT when optimizing the use of the first derivative of information only, XGBoost use of mono-, di- mediated derivative information when optimizing.
  • Missing values: XBGoost handling missing values, the optimum value of the default deletion direction by the slicing automatic learning model.
  • To prevent over-fitting: XGBoost addition to increasing the regularization term to prevent over-fitting, also supports the way the ranks of sampling to prevent over-fitting.
  • Results: It can get better results in the shortest time with less computing resources.

XGBoost base learner

XGBoost can be used Regression Tree (CART) as a learner group, it may also be used as a linear classifier learner group. When to CART as a base learners, its decision-making rules and decision trees are the same, but CART each leaf node has a weight that is scoring a leaf node or a leaf node of the predicted value. CART example below:

FIG regression trees for two (left and right), which is the output value of the right side of the tree leaf node weight (score), when the output of a prediction sample, divided according to the decision condition of each node of the internal node, the final right leaf node is divided into predictive output value is the weight of the sample.

XGBoost model

XGBoost model defined as follows: given a number n of m feature sample data set contains,  [official] predicted output of the integrated model is expressed as:

[official] (1)

Which  [official] represents a regression trees, [official] is the number of regression trees. Whole formula (1) represents a given input  [official] , the output value of  [official] particles obtained predicted value (i.e., according to the regression tree corresponding to decision rule, to the right of the divided leaf node weight) regression trees are added. So how are we going to learn this model?

Model of learning

Typically, a model of how to learn?

  1. Definition of the objective function, namely the loss of function as well as regular items.
  2. Optimization objective function.

The first step: Define the objective function XGBoost

[official] (2)

among them [official]

Equation (2) is right of the first portion of the loss function between the predicted value and the true value of the amount , the second portion represents a penalty term to the complexity of the model (regularization term),

  •  [official] ,  [official] It represents a penalty coefficient (hyper-parameters, set in advance),
  •  [official] To represent leaf nodes of a given tree,
  •  [official] Represents a fraction of the squared output every single leaf nodes (corresponding to a regular L2).

From the definition of objective function can be seen XGBoost of complexity (complexity of the tree) model considers the number of pieces of each leaf node of the tree, and the leaf node and every single output score worth and square.

Step two: the objective function.

In a typical model for such objective function may use gradient descent optimization, it is noted that  [official] represents the tree, instead of a numeric vector, it can not use gradient descent to optimize the objective function. So how to optimize this objective function? Before the algorithm to step! ! !

Before using the objective function to step algorithm. Setting  [official] a predicted value of the i-th sample at t-th iteration (t pieces of tree). then

[official] (3)

Equation (3) represents a predicted value of the sample after i iterations t = t i preceding samples - after the prediction value of a current iteration of t + tree particle predicted value. The objective function can be expressed as:

[official]

Formula (4) represents a greedy added such that model and to promote maximum  [official] which constant represents the constant term, the constant term is the pre-exponential t - 1 iterations penalty term is a constant, i.e. formula  [official] section, when the t-th iteration, before t - t 1 times Diego generated - the one tree has been completely determined, the t - 1 and the right leaf node of the tree have been re-determined, it becomes constant. In formula (4) If we consider quadratic loss function, of formula (4) can be expressed as:

[official]

Formula 5  [official] represents the residual, i.e. through the front t - residual prediction concept behind a tree and the gap between the true value, which is used in GBDT. Proposed to use in the second order Taylor expansion approximate expression XGBoost 5 . The second order Taylor expansion form:

[official] (Equation 6)

Is defined  [official] based on Expression 6 may be expressed as formula 4:

[official] (Equation 7)

Note that Formula 7  [official] partially shows a front t - 1 iterations of the loss function, at t-th iteration the current is already determined constant a, the constant term is omitted in the following equation is obtained:

[official] (Equation 8)

则我们的目标函数变成了公式8.可以看出目标函数只依赖于数据点的一阶和二阶导数。其中 [official]

重新定义一颗树

包括两个部分:

  • 叶子结点的权重向量 ω ;
  • 实例 -> 叶子结点的映射关系q(本质是树的分支结构);

一棵树的表达形式定义如下:

定义树的复杂度

我们定义一颗树的复杂度 Ω,它由两部分组成:

  • 叶子结点的数量;
  • 叶子结点权重向量的L2范数;

定义完树以及树的复杂度之后,接下来针对公式8进行细分。首先定义集合 [official]为树的第j个叶节点上的所有样本点的集合,即给定一颗树,所有按照决策规则被划分到第j个叶节点的样本集合。则根据式2中对模型复杂度惩罚项的定义,将其带入式8有:

[official]

  • 式9就是最终的损失函数的形式了。不知道面试直接写这个行不行。T表示叶子结点个数,
  • 解释一下式9,以叶子节点为基础进行求和,每一个叶子节点中所有样本的的一阶梯度与叶节点的输出值的乘积 加上 0.5的这个叶子节点所有样本的二阶梯度与lambada正则化系数之和与叶子节点的输出值的平方的乘积,这一系列式子求和之后再加上gamma与这棵树的所有叶子节点的个数的和的成绩,就得到了最终的损失函数的形式了。

对式9进行求导:

[official]


插入一个小知识点:

这里我们和传统的gbdt做对比,传统的gbdt的叶节点值是这样的:

[official]

传统的gbdt的基树是cart tree,叶节点值的预测输出是这个叶节点值的所有样本的标签的平均值,单纯从单个cart tree的角度(不涉及gbm框架)来说,如果是回归问题,某个叶子节点的预测输出就是这个叶子节点所有样本的标签值的平均,比如说回归问题中某个叶子节点所有的样本的标签是[0.5,0.4,0.0],则预测输出就是三者的平均0.3,传统的gbdt也是一样的,只不过每一轮要拟合的标签值是前面所有所有tree预测结果与真实标签值计算出来的负梯度而已,比如第t轮,某个叶子节点的值为[0.3,0.4,0.2](这里的值是上一轮拟合得到的损失函数的负梯度),则预测输出就是简单的求平均0.3。也就是上面的这个式子:

[official]

而xgboost通过复杂的推导最后得出结论,叶子节点值不应该是简单的上一轮负梯度的均值,应该加入二阶负梯度和树的正则化系数,于是就得到了:

 


将(式10)带入(式9)中得到(式11):

[official] (式11)

令 [official] 则,(式11)可以简化为(式12)

[official] (式12)

到目前我们得到了(式12)[损失函数的最终形式],xgb通过二阶泰勒展开并且引入了树的正则化的概念将原始的简单的损失函数的形式转化为了上面的这个新的损失函数的形式。而传统的gbdt的损失函数只有上面的Gj一项而已。这个损失函数(式12)是可以做为得分值评价一颗树的好坏,那么评价一颗树的好坏有什么用呢?

  • 可以用于对的剪枝操作(防止过拟合),和决策树中的剪枝是一样的,给定一个损失函数,判断剪枝后,根据损失函数是否减小来决定是否执行剪枝,只是XGBoost是运用式12来作为损失函数判断的标准。注意到评价一颗树的还好的前提是我们能得到一颗树,上式也是基于给定一个树的前提下推导而来的,那么这颗树怎么来得到呢?

XGB模型的学习用一句话总结:xgb引入了树的正则化的概念,在原始的损失函数的基础上加入了正则项(类似与逻辑回归的损失函数加入了正则项)并且通过将带正则项的损失函数进行二阶泰勒展开推导出了叶子节点的新的计算方式。但每一轮要拟合的值还是负梯度。

 

树的生成

在决策树的生成中,我们用ID3、C4.5、Gini指数等指标去选择最优分裂特征、切分点(CART时),XGBoost同样定义了特征选择和切分点选择的指标:

[official] (式13)

XGBoost中使用过(式13)判断切分增益,Gain值越大,说明分裂后能使目标函数减少越多,就越好。其中 [official] 表示在某个节点按条件切分后左节点的得分, [official] 表示在某个节点按条件切分后右节点的得分, [official] 表示切分前的得分, [official] 表示切分后模型复杂度的增加量。现在有了判断增益的方法,就需要使用该方法去查找最优特征以及最优切分点。

分裂查找算法

关于最优特征以及最优切分点的选取XGBoost提供了两个算法。

  1. Basic Exact Greedy Algorithm (精确贪心算法)
  2. Approximate Algorithm(近似算法)

精确贪心算法类似于CART中最优特征与切分点的查找,通过遍历每个特征下的每个可能的切分点取值,计算切分后的增益,选择增益最大的特征及切分点。具体算法流程如下

因为精确贪心算法需要遍历所有特征和取值,当数据量非常大的时候,无法将所有数据同时加载进内存时,精确贪心算法会非常耗时,XGBoost的作者引进了近似算法。近似算法对特征值进行了近似处理,即根据每个特征k的特征值分布,确定出候选切分点 [official] ,即按特征分布将连续的特征值划分到 [official] 个候选点对应的桶(buckets)中,并且对每个桶中每个样本的 [official] 进行累加。候选切分点的划分以及 [official] 的累加过程如下:

划分好候选切分点之后,按照精确贪心算法描述的算法步骤进行选择最优切分特征与最优切分点,不同的是切分点被上述候选切分点所代替,但原理和操作过程是一样的。

在近似算法的伪代码图中,作者提到可以按照global的方式提出候选切分点,也可以按照local的方式提出候选切分点。

什么是global方式?什么是local方式?简单的说就是什么时候提取候选切分点,即在哪一个步骤进行候选切分点的提取。global表示在生成树之前进行候选切分点的提取,即开始之前为整颗树做一次提取即可,在每次的节点划分时都使用已经提取好的候选切分点。而local则是在每次节点划分时才进行候选切分点的提取。那么区别是什么呢?

  • global方式进行候选切分点提取的次数少。因为只是在初始化的阶段进行一次即可,以后的节点切分均使用同一个,而local方式是在每次节点切分时才进行,需要很多次的提取。
  • global方式需要更多的候选点,即对候选点提取数量比local更多,因为没有像local方式一样每次节点划分时,对当前节点的样本进行细化,local方式更适合树深度较大的情况。

上图是作者在Higgs boson 数据集上对两种方式的测试,可以看出local需要更少的候选切分点,当global方式有足够多的候选点时正确率与local相当。

下面对近似算法举个栗子做为说明。

图示中特征值被切分成三个候选切分点,位于0~ [official] 处的样本被划分到第一个桶中, 位于[official] 之间的样本分别被划分到第二个和第三个桶中。那么在计算最优切分点时,就有两种划分方式,第一种方式,即第一个桶的样本被切分到左节点中,第二、三个桶的样本被切分到右节点中。其增益gain为max函数中第二项。第二种划分方式为,第一、二个桶中的样本切分到左节点中,第三个桶中的样本切分到右节点中,其增益为max函数中第三项。那么max函数第一项表示什么呢?第一项表示的是其他特征切分点确认后的最大增益,与当前特征的两种切分方式比较,选择最优的特征及其切分点。

注意到栗子在对样本进行划分时,考虑的是样本的个数,即每个桶中样本个数相同为出发点来划分的,如果样本有权重呢?直观上的想法是,那就考虑权重而不是个数呗。那么每个样本应该赋予什么样的权重?又怎样去处理这个权重?

加权分位数略图(Weighted Quantile Sketch)

为了处理带权重的候选切分点的选取,作者提出了Weighted Quantile Sketch算法。加权分位数略图算法提出一种数据结构,这种数据结构支持merge和prune操作。作者在论文中给出了该算法的详细描述和证明链接(ps:已经失效),这里不做详细介绍。可以参考链接加权分位数略图定义及证明说明。简单介绍加权分位数略图侯选点的选取方式。

设数据集 [official] 表示每个样本的第k个特征值( [official] )和二阶导数( [official] )的集合。定义排名函数 [official] :

[official] (式14)

(式14)表示数据集中第k个特征值小于z的样本所在比例(公式看起来貌似有点奇怪,简单来说就是特征值小于z的样本的权重和,占所有样本权重总和的百分比)。我们的目标是找到一个候选切分点(也就是说怎么去划分候选点的问题),可以根据下式进行侯选点的选取 [official] (式15)

简单的说(式15)表示落在两个相邻的候选切分点之间样本占比小于某个值 [official] (很小的常数),那么我们就有 [official] 个候选切分点。

由(式14)看的样本是以二阶导数作为加权考虑占比的,那么问题来了,为什么使用二阶导数作为加权呢?

对目标函数(式8)进行改写,过程如下:

[official]

(式16)可以看成权重 为[official] 的label为 [official] (ps:这个值比作者论文中多出一个负号,有文章说作者论文里面少写了负号。个人觉得这个正负号不是关注重点,它不是一种定量的计算,而是表示一种以二阶导数作为加权的合理性说明。)的平方损失函数,其权重 [official] 则为二阶导数。由此表明将二阶导数作为样本权重的考虑是合理的。

其他优化方法

  • 稀疏值处理(Sparsity-aware Split Finding)。实际工程中一般会出现输入值稀疏的情况。比如数据的缺失、one-hot编码都会造成输入数据稀疏。论文中作者提出了关于稀疏值的处理,思路是:对于缺失数据让模型自动学习默认的划分方向。算法具体的方法如下:

从算法中可以看出,作者采用的是在每次的切分中,让缺失值分别被切分到左节点以及右节点,通过计算得分值比较两种切分方法哪一个更优,则会对每个特征的缺失值都会学习到一个最优的默认切分方向。乍一看这个算法会多出相当于一倍的计算量,但其实不是的。因为在算法的迭代中只考虑了非缺失值数据的遍历,缺失值数据直接被分配到左右节点,所需要遍历的样本量大大减小。

作者通过在Allstate-10K数据集上进行了实验,从结果可以看到稀疏算法比普通算法在处理数据上快了超过50倍。

  • 分块并行(Column Block for Parallel Learning)。在树生成过程中,需要花费大量的时间在特征选择与切分点选择上,并且这部分时间中大部分又花费在了对特征值得排序上。那么怎么样减小这个排序时间开销呢?作者提出通过按特征进行分块并排序,在块里面保存排序后的特征值及对应样本的引用,以便于获取样本的一阶、二阶导数值。具体方式如图:

通过顺序访问排序后的块遍历样本特征的特征值,方便进行切分点的查找。此外分块存储后多个特征之间互不干涉,可以使用多线程同时对不同的特征进行切分点查找,即特征的并行化处理。

 

  • XGBoost的并行,指的是特征维度的并行:在训练之前,每个特征按特征值对样本进行预排序,并存储为Block结构,在后面查找特征分割点时可以重复使用,而且特征已经被存储为一个个block结构,那么在寻找每个特征的最佳分割点时,可以利用多线程对每个block并行计算。

注意到,在顺序访问特征值时,访问的是一块连续的内存空间,但通过特征值持有的索引(样本索引)访问样本获取一阶、二阶导数时,这个访问操作访问的内存空间并不连续,这样可能造成cpu缓存命中率低,影响算法效率。那么怎么解决这个问题呢?缓存访问 Cache-aware Access 。

  • 缓存访问(Cache-aware Access)。为了减小非连续内存的访问带来缓存命中率低问题,作者提出了缓存访问优化机制。解决思路是:既然是非连续内存访问带来问题,那么去掉非连续内存访问就可以解决。那么怎么能去掉非连续内存空间的访问呢?转非连续为连续----缓存预取。即提起将要访问的非连续内存空间中的梯度统计信息(一阶、二阶导数),放置到连续的内存空间中。具体的操作上就是为每个线程在内存空间中分配一个连续的buffer缓存区,将需要的梯度统计信息存放在缓冲区中。这种方式对数据量大的时候很有用,因为大数据量时,不能把所有样本都加入到内存中,因此可以动态的将相关信息加入到内存中。

上图给出了在Higgs数据集上使用缓存访问和不使用缓存访问的式样对比。可以发现在数据量大的时候,基于精确的贪心算法使用缓存预取得处理速度几乎是普通情况下的两倍。那么对于block块应该选择多大才合理呢?作者通过实验证明选择每个块存放 [official] 个样本时效率最高。

  • "核外"块计算(Blocks for Out-of-core Computation)。当数据量非常大的是时候我们不能把所有数据都加载内存中,因为装不下。那么就必须的将一部分需要加载进内存的数据先存放在硬盘中,当需要时在加载进内存。这样操作具有很明显的瓶颈,即硬盘的IO操作速度远远低于内存的处理速度,那么肯定会存在大量等待硬盘IO操作的情况。针对这个问题作者提出了“核外”计算的优化方法。具体操作为,将数据集分成多个块存放在硬盘中,使用一个独立的线程专门从硬盘读取数据,加载到内存中,这样算法在内存中处理数据就可以和从硬盘读取数据同时进行。为了加载这个操作过程,作者提出了两种方法。
  • Compression block (Block Compression). When paper is used to compress columns, read unzip it with another thread. For row index, only the first index value saved, then save the difference between the first index of the block with a 16-bit integer. On the block is provided by testing  [official] when the sample size, the compression ratio of nearly 26%  [official] 29% (seemingly did not say what compression method is used .......).
  • Block partitions (Block Sharding). Wherein the block partition block is stored in different partitions of the hard disk, in order to increase the throughput of the hard disk IO.
  • To prevent over-fitting. XGBoost can see from the model, in order to prevent over-fitting join two penalty terms  [official] , [official] except XGBoost there are two methods to prevent over-fitting. 1. The learning rate. And GBDT XGBoost also used as learning rate (step size) to prevent over-fitting, as follows:  [official] where  [official] is the learning rate, usually take 0.1.2 row, column sample. And random forest as XGBoost sample support and sample characteristics, and features of the sample after the sample is taken as training data, to further prevent over-fitting.

Guess you like

Origin www.cnblogs.com/XDU-Lakers/p/11912265.html