Search

Search papers and lessons

XGBoost: A Scalable Tree Boosting System

Tianqi Chen, Carlos Guestrin · 2016

Chen and Guestrin's 2016 system makes gradient tree boosting fast and scalable, with a sparsity-aware split rule, a weighted quantile sketch and out-of-core training.

On this page

  • Colour = topic mix
  • Linear algebra
  • Calculus
  • Probability
  • Discrete math
  • Classical models
  • Learning basics
  • ReadReady to readNot yet

TL;DR

XGBoost is a system for gradient tree boosting that is fast enough to train on billions of examples. The paper adds four ideas to the basic method: a split rule that handles missing and sparse values, a weighted quantile sketch for choosing split candidates on large or distributed data, a cache-friendly data layout, and out-of-core training from disk. On the paper's benchmarks it reached accuracy close to the baselines it was compared with, and it ran much faster.

Builds on: Gradient Boosting, Decision Tree, Hessian Matrix, L2 Regularization, Weighted Quantile Sketch · Leads to: gradient boosting as used in practice

Why it mattered

The paper's own count is the clearest measure. Among the 29 winning Kaggle solutions published in 2015 that the authors examined, 17 used XGBoost, and eight used it alone. The second most common method, deep neural nets, appeared in 11. Every winning team in the top ten of KDDCup 2015 used it.

The paper's comparison table (Table 1) sets XGBoost against other tree boosting systems on six features: exact greedy search, global and local approximate search, out-of-core training, sparsity awareness and parallelism. XGBoost is the only system marked yes on all of them. It trains on one machine with data on disk, and it spreads work over a cluster when needed. Its open-source package is used from Python, R and Julia, and it fits into scikit-learn pipelines.

The problem

Gradient boosting builds a model as a sum of trees. Each new tree must be chosen to lower a loss that depends on the current prediction. The hard part is the split search: at each node, the learner must pick a feature and a threshold. The exact method tries every distinct value of every feature, after sorting the data. That is accurate, but sorting and scanning cost time, and the data must fit in memory.

Three practical problems follow. The data is often sparse, with many zeros, one-hot columns or missing entries, and the learner needs a principled way to handle missing entries. The data may not fit in memory, or may be split across machines, so the exact method cannot run. And the work of reading gradient statistics in feature order jumps around memory, which makes the CPU wait for the cache. XGBoost addresses all three.

How it works

Think of XGBoost as a pipeline that runs once for each tree.

  1. Load the data into blocks. Each feature column is stored sorted by value, in compressed column format, in blocks. The sorting is paid once, before training, and reused in later trees.
  2. Compute the gradient statistics. For each row i, compute gig_i, the first derivative of the loss at the current prediction, and hih_i, the second derivative. These are the only numbers the tree learner needs from the loss.
  3. Choose the split candidates. For the exact method, every distinct value is a candidate. For the approximate method, each feature is cut at candidate points chosen by the weighted quantile sketch, which uses hih_i as the weight of each row.
  4. Score each split and pick the best. A split is scored from the sums of gg and hh on each side. Rows with a missing value go to a default direction, learned from the data, that is chosen to make the score highest.
  5. Set the leaf values. Each leaf gets a value computed from the sums of gg and hh in that leaf.
  6. Add the tree with shrinkage. The new tree is multiplied by a factor η\eta before it is added, which the paper calls shrinkage. Then go back to step 2 for the next tree.

Step 3 is where the approximate method saves work. The exact method scans every value; the approximate one scans only the buckets, with sums of gg and hh aggregated per bucket.

flowchart LR
  A["Sorted blocks of features"] --> B["Gradient statistics g and h"]
  B --> C["Candidate splits: exact or sketch"]
  C --> D["Score splits with sums of g and h"]
  D --> E["Leaf values and new tree"]
  E --> F["Add tree times shrinkage"]
  F --> B

Here is one split traced by hand. Four rows have targets y = (1, 1, 3, 3), the current prediction is 0 everywhere, and the loss is squared error, so gi=y^i−yi=(−1,−1,−3,−3)g_i = \hat{y}_i - y_i = (-1, -1, -3, -3) and hi=1h_i = 1. Use λ=1\lambda = 1 and γ=0\gamma = 0. Try the split that puts rows 1 and 2 on the left.

  1. Left sums: GL=−1+(−1)=−2G_L = -1 + (-1) = -2 and HL=2H_L = 2. Check: two rows, each with h = 1.
  2. Right sums: GR=−3+(−3)=−6G_R = -3 + (-3) = -6 and HR=2H_R = 2. Check: the totals are G=−8G = -8 and H=4H = 4.
  3. Score each side as G2/(H+λ)G^2 / (H + \lambda): left 4/34/3, right 36/3=1236/3 = 12, and the whole 64/5=12.864/5 = 12.8.
  4. The gain is 12(4/3+12−12.8)=12(0.533)≈0.267\tfrac{1}{2}(4/3 + 12 - 12.8) = \tfrac{1}{2}(0.533) \approx 0.267. Check: a positive gain means the split helps.
  5. Leaf values are w=−G/(H+λ)w = -G/(H + \lambda): left 2/3≈0.672/3 \approx 0.67 and right 6/3=26/3 = 2. Check: each leaf value is pulled toward its rows' targets, and the shrinkage then scales it.

The math

The model is a sum of KK regression trees, y^i=∑k=1Kfk(xi)\hat{y}_i = \sum_{k=1}^{K} f_k(\mathbf{x}_i), where fk(x)=wq(x)f_k(\mathbf{x}) = w_{q(\mathbf{x})} maps an input to the score ww of its leaf under the tree structure qq. The learning objective adds a penalty on tree size and leaf values:

L(ϕ)=∑il(y^i,yi)+∑kΩ(fk),Ω(f)=γT+12λ∥w∥2\mathcal{L}(\phi) = \sum_i l(\hat{y}_i, y_i) + \sum_k \Omega(f_k), \qquad \Omega(f) = \gamma T + \tfrac{1}{2}\lambda \lVert w \rVert^2

Here TT is the number of leaves, γ\gamma is the cost of each leaf, and λ\lambda shrinks leaf values, which is the L2 penalty named in the requirements. The paper notes that when the regularization is set to zero, the objective falls back to traditional gradient tree boosting.

Step 2 in symbols

Add tree ftf_t to the prediction from the previous round. A second-order Taylor expansion of the loss gives

L(t)≃∑i[l(yi,y^(t−1))+gift(xi)+12hift2(xi)]+Ω(ft)\mathcal{L}^{(t)} \simeq \sum_i \left[ l(y_i, \hat{y}^{(t-1)}) + g_i f_t(\mathbf{x}_i) + \tfrac{1}{2} h_i f_t^2(\mathbf{x}_i) \right] + \Omega(f_t)

with gi=∂y^(t−1)l(yi,y^(t−1))g_i = \partial_{\hat{y}^{(t-1)}} l(y_i, \hat{y}^{(t-1)}) and hi=∂y^(t−1)2l(yi,y^(t−1))h_i = \partial^2_{\hat{y}^{(t-1)}} l(y_i, \hat{y}^{(t-1)}). Removing the constant gives L~(t)\tilde{\mathcal{L}}^{(t)}. For a second derivative of a loss in one prediction, hih_i is just a number per row.

Step 4 in symbols

Let IjI_j be the set of rows in leaf j. Collecting terms per leaf gives

L~(t)=∑j=1T[(∑i∈Ijgi)wj+12(∑i∈Ijhi+λ)wj2]+γT\tilde{\mathcal{L}}^{(t)} = \sum_{j=1}^{T} \left[ \Big(\sum_{i \in I_j} g_i\Big) w_j + \tfrac{1}{2}\Big(\sum_{i \in I_j} h_i + \lambda\Big) w_j^2 \right] + \gamma T

Setting the derivative in each wjw_j to zero gives the best leaf value and the best score for a fixed structure:

wj∗=−∑i∈Ijgi∑i∈Ijhi+λ,L~(t)(q)=−12∑j=1T(∑i∈Ijgi)2∑i∈Ijhi+λ+γTw_j^* = -\frac{\sum_{i \in I_j} g_i}{\sum_{i \in I_j} h_i + \lambda}, \qquad \tilde{\mathcal{L}}^{(t)}(q) = -\frac{1}{2}\sum_{j=1}^{T} \frac{\left(\sum_{i \in I_j} g_i\right)^2}{\sum_{i \in I_j} h_i + \lambda} + \gamma T

The score is the sum over leaves of G2/(H+λ)G^2 / (H + \lambda), where GG and HH are the sums of gg and hh in that leaf. This is the form used in the walkthrough above.

The split gain in symbols

For a split of node II into ILI_L and IRI_R, the loss reduction is

Lsplit=12[(∑i∈ILgi)2∑i∈ILhi+λ+(∑i∈IRgi)2∑i∈IRhi+λ−(∑i∈Igi)2∑i∈Ihi+λ]−γ\mathcal{L}_{split} = \tfrac{1}{2}\left[ \frac{\left(\sum_{i \in I_L} g_i\right)^2}{\sum_{i \in I_L} h_i + \lambda} + \frac{\left(\sum_{i \in I_R} g_i\right)^2}{\sum_{i \in I_R} h_i + \lambda} - \frac{\left(\sum_{i \in I} g_i\right)^2}{\sum_{i \in I} h_i + \lambda} \right] - \gamma

Each split is accepted when this is positive and largest among the candidates. The approximate method uses the same formula, with the sums taken over buckets.

Step 3 in symbols, the sketch

The approximate method needs candidate cut points sk1,…,skls_{k1}, \dots, s_{kl} for feature k. The rank of a value is the share of hh below it:

rk(z)=1∑(x,h)∈Dkh∑(x,h)∈Dk, x<zhr_k(z) = \frac{1}{\sum_{(x, h) \in \mathcal{D}_k} h} \sum_{(x, h) \in \mathcal{D}_k,\ x \lt z} h

The sketch picks candidates such that consecutive ranks differ by less than ϵ\epsilon, with the smallest and largest values included. The paper says there are then roughly 1/ϵ1/\epsilon candidates. The Weighted Quantile Sketch lesson works through this.

Results

The paper reports results on four datasets (Allstate, Higgs Boson, Yahoo! LTRC and Criteo). Experiments used maximum depth 8 and shrinkage 0.1 unless noted.

  • Exact method against two other tools (Higgs-1M, 500 trees, Table 3). XGBoost took 0.6841 seconds per tree with test AUC 0.8304. scikit-learn took 28.51 seconds per tree with AUC 0.8302. R's gbm took 1.032 seconds with AUC 0.6224. The paper says XGBoost runs more than 10 times faster than scikit-learn. The paper attributes gbm's lower accuracy to its greedy method, which expands only one branch of a tree: faster, but it can lose accuracy.
  • Learning to rank (Yahoo! LTRC, 500 trees, Table 4). XGBoost took 0.826 seconds per tree with NDCG@10 of 0.7892. With column subsampling at 0.5 it took 0.506 seconds with 0.7913. pGBRT took 2.576 seconds with 0.7915.
  • Sparsity-aware split finding. On Allstate-10K the sparsity-aware algorithm was more than 50 times faster than the naive version.
  • Cache-aware exact greedy. On large data, the cache-aware version ran twice as fast as the naive one (Figure 7).
  • Block size. The paper chose 2162^{16} examples per block, which balances cache misses against parallel work (Figure 9).
  • Compression for disk. Block compression reached roughly a 26 to 29 percent compression ratio on most datasets tested. On Criteo, compression gave a 3 times speedup and sharding over two disks another 2 times (Figure 11).
  • Scale. The final out-of-core method processed all 1.7 billion Criteo examples on a single machine.
  • Distributed comparison (32 EC2 nodes, 10 iterations, Figure 12). The paper reports XGBoost ran more than 10 times faster than Spark per iteration and 2.2 times faster than H2O's optimized version. Spark slowed sharply when it ran out of memory.

The limits are real. Most timing comparisons are on specific hardware, and the scikit-learn comparison uses the 1M subset because the baseline is slow. The column-subsampled model was slightly less accurate on Higgs but slightly more accurate on Yahoo!, so subsampling is a tuning choice, not a free win. The paper's accuracy claims are for the datasets it tested.

Reading guide

  • Section 1 (Introduction). Read with Gradient Boosting and Ensemble Learning in mind. It lists the contributions and the Kaggle evidence.
  • Section 2 (Tree boosting in a nutshell). Needs Decision Tree, Taylor Series, Hessian Matrix and L2 Regularization. Equation (2), the tree ensemble model, and Equation (7), the split score, are the core of the paper.
  • Section 3.1 (Exact greedy). Sorting the data and scanning it, which the block structure later makes cheap.
  • Sections 3.2 and 3.3 (Approximate algorithm and weighted quantile sketch). Need Weighted Quantile Sketch. Appendix A holds the proofs of the merge and prune guarantees.
  • Section 3.4 (Sparsity-aware split finding). Needs Sparse Matrix and the idea of an indicator column, since one-hot encoding creates many sparse columns.
  • Section 4 (System design). Blocks, cache-aware access and out-of-core computation. Read for the engineering, not for new statistics.
  • Section 5 (Related work). Column sampling is borrowed from Random Forest, and the paper places XGBoost among earlier parallel tree boosting systems.
  • Section 6 (End-to-end evaluations). Uses AUC for classification and NDCG for ranking. Tables 3 and 4 and Figures 10 to 13 are the results.
  • Section 7 (Conclusion). A short summary of the lessons.

Check yourself

QuestionWhat are gig_i and hih_i in the XGBoost objective?Show answer
They are the first and second derivatives of the loss with respect to the current prediction for row i. Every tree is chosen using only these sums of these two numbers per row.
QuestionFour rows have g = (-1, -1, -3, -3) and h = 1 each, with λ=1\lambda = 1 and γ=0\gamma = 0. Compute the leaf value for the right leaf, which has rows 3 and 4 only.Show answer
GR=−6G_R = -6 and HR=2H_R = 2, so w=−G/(H+λ)=6/3=2w = -G/(H + \lambda) = 6/3 = 2.
QuestionWhy does the approximate method use the sum of hh rather than the number of rows to place its candidates?Show answer
The loss reduction is a weighted squared loss with weights hih_i, as the Taylor expansion shows. Placing candidates by hh spreads the cut points where the weighted loss is decided, so rows with small hh count for little.
QuestionThe paper reports 17 of 29 winning Kaggle solutions used XGBoost. What fraction is that, and what does the paper add about the eight that used XGBoost alone?Show answer
17 divided by 29 is about 0.59, or 59 percent. Eight of the 29 used XGBoost alone, and the paper's point is that XGBoost was the most-used single method among the winners, while most of the others combined it with neural nets.

Sources

  • Chen and Guestrin, "XGBoost: A Scalable Tree Boosting System," arXiv 1603.02754 (2016).
  • The same paper, KDD 2016, ACM DOI 10.1145/2939672.2939785.
  • Wikipedia, "XGBoost."

Read the full paper

Tianqi Chen, Carlos Guestrin, 2016. arXiv:1603.02754