Sub topic 12
How XGBoost finds splits: exact and histogram methods
This chapter assumes you know the XGBoost objective from the earlier chapter: the gradient sum G, the hessian sum H, and the regularization terms lambda and gamma. It also assumes you can train a model with the scikit-learn API.
What a split search does
Each tree grows by choosing, for a node, the feature and threshold with the highest gain. For a candidate split, with G and H summed over the rows going left (L) or right (R):
gain = 0.5 × [GL² / (HL + λ) + GR² / (HR + λ) − (GL + GR)² / (HL + HR + λ)] − γ
A split is worth making only if the gain is positive. The search has to evaluate this formula for many candidate thresholds on every feature. The tree method decides which thresholds are considered.
Exact greedy
The exact method sorts the rows by one feature. It then scans from left to right and keeps running sums of G and H. Every boundary between two distinct values is a candidate, and the gain at each one comes from the running sums.
This finds the best possible threshold. The cost grows with the number of rows times the number of features, and the sorted data must be handled at every node. It becomes slow on large data.
Histogram method
The histogram method sorts nothing at each node. Before training, it groups each feature's values into at most max_bin bins (default 256) using quantiles. Then, at each node, it does two things:
- It adds up G and H for the rows in each bin.
- It scans only the bin boundaries as candidate splits.
The scan covers at most 256 candidates per feature, however many rows there are. After a node splits, XGBoost builds the histogram for the smaller child. It gets the larger child's histogram by subtracting the smaller one from the parent's. This roughly halves the work.
Choosing a tree method
| `tree_method` | Candidates | Typical use |
|---|---|---|
| `exact` | Every distinct value | Small data, when you want exact thresholds |
| `approx` | Quantile sketch, recomputed per tree | Rarely needed now |
| `hist` | Fixed bins, computed once | Default in XGBoost 2.0 and later, and the choice for large data and GPUs |
import time
from sklearn.datasets import make_regression
from xgboost import XGBRegressor
X, y = make_regression(n_samples=50000, n_features=50, noise=10, random_state=0)
for method in ["exact", "hist"]:
model = XGBRegressor(
n_estimators=100, max_depth=6, tree_method=method, random_state=0
)
start = time.perf_counter()
model.fit(X, y)
print(method, round(time.perf_counter() - start, 2), "seconds")On most machines, hist finishes much faster. Its accuracy is usually about the same.
Common errors
- Assuming
exactis always more accurate. It finds the best threshold on the training data, which can overfit slightly more than binned thresholds. - Setting
max_bintoo low. With very few bins, important thresholds can fall inside one bin and be lost. Raise it if accuracy drops. - Setting
max_binvery high. This slows training and rarely helps. - Relying on the default. Older versions defaulted to
auto, which pickedexacton small data. Settree_methodexplicitly if you need results that match across versions.
Exercise
A feature has 1,000,000 distinct values, and max_bin is 256. How many candidate thresholds does the histogram method evaluate for that feature at a node, and why is this faster than the exact method?
Show answer
At most 255 thresholds, the boundaries between 256 bins. The exact method would scan up to about 1,000,000 boundaries. The histogram method also avoids re-sorting at each node, because the bins are built once before training.