Sub topic 3
LightGBM: Histogram-Based Split Finding
This chapter assumes you know how gradient boosted trees add trees one at a time, what a split's gain is, and how to use pandas and scikit-learn. It covers how LightGBM finds splits quickly and which parameters control that process.
The problem with exact splits
A tree learner must choose a feature and a threshold at every node. The exact method sorts each feature's values and scans every possible threshold. With n rows and d features, that costs roughly O(n × d) per node after sorting. It also needs the raw values in memory for every scan. On millions of rows this is slow.
The histogram idea
LightGBM bins each feature once, before training. It maps each continuous value to one of at most max_bin integer bins. The default is 255. After that, the raw values are no longer needed for split search.
At each node, LightGBM builds a histogram per feature. Each bin stores the sum of gradients, the sum of hessians and a row count for the rows in that bin. The best split is then found by scanning the bins, not the rows. That scan costs O(bins) per feature.
Why this is fast
- Cheaper scans. A split search touches about 255 bins, not every row.
- Less memory. A bin index fits in one byte when
max_binis 255 or lower. A float64 value takes eight bytes. - Histogram subtraction. A parent's histogram equals the sum of its two children's histograms. LightGBM builds the histogram for the smaller child and gets the larger child's by subtracting it from the parent's. This roughly halves the histogram work.
The cost: approximate thresholds
Binning limits candidate thresholds to bin boundaries. The best exact threshold may fall inside a bin, so the model can be slightly less precise. In practice the loss is small, and the coarseness acts as mild regularization. Raising max_bin gives finer thresholds at the price of speed and memory.
Inspecting the bins
The example below trains on a feature with many unique values and shows that the number of bins stays bounded.
import numpy as np
import lightgbm as lgb
rng = np.random.default_rng(0)
X = rng.normal(size=(10000, 1))
y = (X[:, 0] > 0.3).astype(int)
for max_bin in (8, 255):
ds = lgb.Dataset(X, label=y, params={"max_bin": max_bin, "verbose": -1})
ds.construct()
model = lgb.train(
{"objective": "binary", "max_bin": max_bin, "verbose": -1},
ds,
num_boost_round=20,
)
thresholds = np.unique(
[
node["threshold"]
for tree in model.dump_model()["tree_info"]
for node in [tree["tree_structure"]]
if "threshold" in node
]
)
print(max_bin, "root thresholds:", np.round(thresholds, 3))With max_bin=8, the root thresholds can only land on a few bin edges. With 255 bins, they can sit much closer to 0.3.
Common errors
- Setting
max_binvery low on a feature that needs fine thresholds, such as a price or timestamp. Accuracy drops with no warning. - Making a validation
Datasetwithoutreference=train_set. Its bins may then differ from the training bins. - Assuming histogram training changes the model type. The trees are still ordinary gradient boosted trees. Only the split search is approximate.
Exercise
A parent node has histogram bin counts [40, 25, 35] for one feature, and its left child has [30, 10, 5]. What are the right child's counts, and why does LightGBM compute it this way?
Show answer
The right child's counts are [10, 15, 30]. LightGBM subtracts the smaller child's histogram from the parent's instead of building a second histogram from rows. This saves about half of the histogram-building work.