Skip to content

How LightGBM Builds Its Trees

Published:
•16 min read

LightGBM is a library for gradient-boosted decision trees. This post follows a deck I prepared on LightGBM, in the deck’s order: decision trees, ensemble methods, gradient boosting, LightGBM, its hyperparameters, and categorical features.

The case-study numbers come from the same deck, where I fitted each kind of model to one product’s weekly sales at one location. The numbers depend on the dataset, so the results are given relative to each other, not as raw error values. The tuning post covers how far to tune the result.

Decision trees

Intro

Because each question only asks which side of a threshold a value falls on, a tree depends on the order of a feature’s values, not their scale. Multiplying a feature by 1,000 moves the threshold but sends every row the same way.

Toy example: a depth-2 decision tree with three leaves splits a plane of two-class points into axis-aligned rectangles.

Figure 1. A toy example on 70 synthetic points. Each question compares one feature with one threshold, so the tree cuts the plane into rectangles.

CART

Decision trees challenges: regularizing

Decision trees challenges: orthogonal boundaries

A tree can only cut along one feature at a time. When the true boundary between classes is diagonal, it needs a staircase of many splits to follow it. Rotated 45 degrees, the same boundary needs 8 leaves instead of one split.

Toy example: with a vertical class boundary one split classifies every point; with the same boundary turned 45 degrees, an 8-leaf tree approximates it with a staircase.

Figure 2. A toy example on 120 synthetic points. The same straight boundary takes one split along an axis, and a staircase of 8 leaves when turned, which still gets 97.5% of the points right.

Decision trees challenges: instability

Case study: max depth influence

On the weekly sales series, a single tree gave these errors, measured as WAPE (weighted absolute percentage error: total absolute error divided by total actual sales, lower is better) and indexed to the depth-1 tree = 100:

TreeError, depth 1 = 100
max_depth=1100
max_depth=599.6
max_depth=2086.5

Here the deepest tree did best.

Ensemble methods

Bagging or pasting

The chance that a given row is never drawn in n draws is (1 − 1/n)^n, which approaches 1/e, about 37%, as n grows.

Toy example: three bootstrap samples of rows 1 to 8 drawn with replacement, each listing its out-of-bag rows, and one pasting sample of 6 rows drawn without replacement.

Figure 3. A toy example with 8 rows. Bagging repeats some rows and leaves others out; pasting never repeats a row. Rows a tree never saw (out-of-bag) give a free check of the forest.

Random forest

Case study: random forest

On the same series, going from 5 trees (n_estimators=5) to 100 trees cut the random forest’s error by about 9%. More trees helped, but here the forest still had about 28% more error than the single deep tree above.

Gradient boosting

Boosting and GBM

Diagram: in bagging and random forests, trees are trained independently on their own samples and averaged; in boosting, each tree is trained on the errors of the ones before it and the outputs are summed.

Figure 4. Bagging averages independent trees to reduce variance; boosting fits each new tree to the errors left by the trees before it.

Some gradient boosting hyperparameters

Toy example. Left: synthetic points with the boosted model after 1, 5 and 34 trees. Right: training error keeps falling while validation error is lowest at round 34 for learning rate 0.1 and round 64 for 0.05.

Figure 5. A toy example on 40 synthetic points. Validation error is lowest at round 34 with a learning rate of 0.1, and at round 64 with 0.05.

LightGBM

Motivation

From the LightGBM paper (Ke et al., 2017):

LightGBM uses four techniques: leaf-wise growth, histograms, GOSS and exclusive feature bundling.

Leaf-wise tree growth

Toy example. A level-wise tree with 4 leaves is balanced at depth 2; a leaf-wise tree with 4 leaves splits the most useful leaf each time and reaches depth 3 on one side.

Figure 6. A toy example. Both trees have four leaves; the leaf-wise one is deeper where the error is.

Histogram based splitting

Problem:

Solution:

Toy example. Twelve feature values with gradients are grouped into four bins of three rows; only the three bin edges are scored.

Figure 7. A toy example. Three bin edges are scored instead of eleven gaps.

Gradient based one side sampling (GOSS)

Problem:

Solution:

Toy example of gradient-based one-side sampling: of 20 rows sorted by absolute gradient, the top 4 are kept, 2 of the remaining 16 are sampled and weighted by 8, the rest are skipped.

Figure 8. A toy example with LightGBM’s default rates.

Exclusive feature bundling

Problem:

Solution:

Toy example of exclusive feature bundling: three one-hot columns become one column whose values 1, 2 and 3 say which one was set.

Figure 9. A toy example. One histogram replaces three.

In the paper’s five benchmarks, adding GOSS and bundling to LightGBM’s own histogram algorithm cut training time per iteration by 1.6 to 21 times, with almost the same accuracy.

Hyperparameters

LightGBM’s parameters split into the modeling setup (objective, boosting), those that control the ensemble training, such as the number of trees and the learning rate, and those that control the growth of each tree, such as max_depth and min_data_in_leaf.

Modeling setup

Objective, for regression:

Boosting:

Tree growth parameters

Recommendations

Start by defining the problem: the objective. Then, from LightGBM’s parameter-tuning guide:

For better accuracy:

To deal with over-fitting:

The deck’s LightGBM case study, hand-tuning the same series, is in the tuning post, with how far tuning is worth taking.

Categorical features vs continuous features

Toy example: six categories are sorted by sum of gradients over sum of hessians; scoring only the five cuts along that order puts categories 1, 3 and 5 on one side and 0, 2 and 4 on the other.

Figure 10. A toy example with six categories. Five cuts are scored instead of 31 partitions.

Key hyperparameters for categorical features

References

Newsletter subscriptions are not currently available.