#260Histogram Split Finding (GBM)HardDecision TreesEnsemble LearningBoostingAsked atMicrosoft · Meta · Amazon
Histogram Split Finding (GBM)
Background
Histogram-based GBM (LightGBM, XGBoost's hist mode) bins each feature into a fixed number of bins, then computes per-bin gradient and Hessian sums. Sweeping candidate splits is now rather than — the trick behind LightGBM's speed.
Problem statement
Implement best_split(values, gradients, hessians, n_bins=32, reg_lambda=1.0). Steps:
- Bin
valuesinton_binsequal-width bins on . - Accumulate
g_sum[b]andh_sum[b]per bin. - For each candidate split (between bin and , with ), compute:
- Pick the threshold (right edge of bin ) that maximises gain.
Input
values— 1-Dnp.ndarrayof floats, the feature column.gradients— 1-Dnp.ndarraysame length, per-row first-order gradients.hessians— 1-Dnp.ndarraysame length, per-row second-order Hessians.n_bins—int >= 2, histogram resolution.reg_lambda—float >= 0, L2 regularisation.
Output
Returns (threshold: float, gain: float). If no positive-gain split exists, return (values.min(), 0.0).
Examples
Example 1 — bimodal separable case → clear positive-gain split
Input: values bimodal around ±5; gradients of opposite signs per cluster
Output: threshold near 0, gain > 0
Example 2 — all-zero gradients → gain = 0
Input: gradients all 0
Output: gain == 0.0
Example 3 — degenerate values (lo == hi) → gain = 0
Input: all values equal
Output: gain == 0.0
Constraints
- Skip splits where one side has zero Hessian (no rows on that side).
- Return Python
floats. - .
Notes
- Why equal-width. Production implementations use quantile bins for skewed features; equal-width is the simpler kernel.
- Categorical features. Categorical splits use a different split-finding routine (best-vs-rest after sorting by target mean) — out of scope here.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference example: bimodal data yields a positive-gain split
- •Sample: clean two-bin split gain equals 8/3
- •Example: all-zero gradients give zero gain