You must know the principles of XGBoost
"TLDR: This article deduces the principle of XGBoost in detail, including the definition of loss function, optimization of objective function and regularization method. The article also introduces XGBoost’s training methods and common difficulties, such as slow training speed, parallelization issues, high-dimensional feature processing and over-fitting mitigation, etc."
Principle derivation
The general form of defining the loss function is . Although many tutorials default to , this is more general. The base learner is , where is the parameter of the learner
Then:
Note that although the formulas of and are ugly, once the form of is determined, and can derive a definite value or a concise form.
For example, assuming , then ,
Here, the first-order derivative and the second-order derivative are recorded as and respectively to facilitate mathematics. The objective function is:
It is definitely necessary to do some operations to prevent overfitting of the model. We assume that the base learner is the most commonly used Cart tree, then the complexity of the tree is represented by the number of leaf nodes and the output value of each leaf node .
Because XGBoost is an additive model, the sum of the trained N trees is the final output. Therefore, if the output result of a certain tree is very large, it will dominate the overall output result and is prone to overfitting. Therefore, we hope that the output value of the leaf node of each tree should be as small as possible.
Therefore, the base model uses a regularization formula: , where represents the th leaf node of the tree, and represents the output value of the th leaf node.
The objective function is rewritten as:
Anyway, each sample will always be output to a certain leaf node , and the output result is . Then the part of the above formula is not as good as directly changed to be represented by leaf nodes. In addition, is a fixed value and does not affect optimization, so the formula is updated as:
To optimize , it is equivalent to optimizing . This is a simple quadratic equation of one variable. The position and size of the optimal value can be obtained through the discriminant formula
Training method
The conclusion has been derived: the output value of the leaf node of a tree needs to be , and the value of the optimization target becomes . The question now is how to construct such a Cart tree.
First, suppose there are three features :
| Characteristics | Value range |
|---|---|
| Feature A | [1, 3, 6, 8, 10] |
| Feature B | [3, 5, 9, 89, 49] |
| Feature c | [2, 4, 5, 7, 10, 22] |
Traverse each feature, traverse all possible split nodes for each feature, and calculate before splitting, of the left subtree after splitting, and of the right subtree respectively. Since we have calculated the formula of , these can directly obtain specific values. We compare the gain of the split , and determine the best split feature and split value by finding the maximum gain.
Common difficulties
The training speed is too slow, what should I do?
In engineering implementation, XGBoost adopts the pre-sorting idea to accelerate training. For example, for feature A, we sort all possible values of feature A (the above table is already the sorted result)
When we select a certain value split node, we will get the left subtree and the right subtree, and calculate respectively. If we continue to calculate the next feature value as the split node at this time, we only need to move some samples of the right subtree to the left subtree, greatly reducing the amount of calculation.
How to parallelize?
XGboost itself is a serial model and cannot be parallelized. The generation of the next tree must wait for the completion of the previous tree. But the generation inside the tree can be parallelized.
XGBoost internally stores data according to the Block structure. Specifically, it stores data according to characteristic columns. The block structure allows independent processing of each feature, for example, one thread calculates the split gain on feature A, and another thread calculates the split gain on feature B. In addition, Block block structure takes advantage of the locality principle of computers, because when traversing all possible values of a feature, samples need to be frequently classified (classified into the left subtree or the right subtree). Block data directly puts the feature column and sample data together, and the cache hit rate is higher.
Summary: Use Block to store the data of an entire feature column, and the information gain calculation for different features can be parallelized by multiple threads. Within Block, the early version of XGBoost used a pre-sorting idea, because it is faster to find the best split point. Later versions used the histogram approximation method in LightGBM to separate continuous features into different Bin boxes. This is equivalent to having only data in different Bin boxes. Calculate the gradient and Hessian matrix inside the Bin box.
How to deal with high-dimensional features
We know that when performing feature splitting, it is necessary to traverse all features and all values of the features. In the case of high-dimensional features, traversing feature values will be very time-consuming.
XGBoost approximates through the bucketing idea (also called histogram approximation method), sorts all possible values of a certain feature (such as A), and then divides it into several buckets, counts and of each bucket respectively, and then tries to split each bucket to find the gain. This method will reduce the training time
How to alleviate overfitting
In addition to the regularization ideas mentioned above, XGBoost also draws on the random feature selection method in RF, also called column sampling. When splitting a certain node, why bother traversing all the features (such as A, B, C)? Isn't it better to extract some of them? For example, just extract A and B and calculate the split gain separately.
The level wise growth strategy of XGBoost can also be used as a measure to alleviate overfitting. When the nodes of one layer are split, they are split at the same time, so that a certain node will not be over-split (leaf wise). Of course, this method takes up slightly more memory.
How to do classification problems
We know that the base learner Cart tree can be used for classification or regression problems.
In the second classification, we directly process the output result of the overall model with Sigmoid, thus turning the real value output into probability, then the loss function is modified to Log Loss, that is:
This can also calculate the first and second derivatives:
In multi-classification problems, the output of XGBoost is slightly different, because one tree cannot output the probabilities of multiple categories, so we let one tree focus on the prediction of one category. In this way, the tree outputs the scores of categories, which are then normalized to probabilities through the softmax function.
N trees are constructed in each round, and trees are constructed in rounds of training.
How is the importance of xgboost features evaluated?
Feature importance evaluation is a very common problem, and the evaluation method is also common in GBDT-type models.
Feature importance can be calculated through the Gini index: during the training process, the total number of feature splits is recorded (more splits must be important), the total/average information gain is used to quantify the feature importance, and finally the importance of all features is ranked. This is a method for evaluating the feature importance of a single tree species. Then GBDT/XGBoost is an Ensemble of multiple trees, just average it.
Which one of LR and XGBoost is suitable for processing high-dimensional sparse data?
GBDT's tree models (XGBoost, LightGBM) are not suitable for processing high-dimensional data, and LR is more suitable.
Most of the high-dimensional sparse features are 0, and only a few are non-0. The tree model can only utilize a very small amount of non-zero information when splitting features, and LR can handle this well using weights. It can be explained similarly that GBDT is not suitable for one hot.