Histogram-Based Split Finding with Subtraction for Gradient Boosting

Quick Overview

Implement histogram-based split finding for gradient boosted trees: build per-bin gradient and hessian sums, derive a sibling histogram by subtraction, and pick the best split by regularized gain. It tests vectorized NumPy, second-order boosting math and careful handling of ties and invalid splits.

Histogram-Based Split Finding with Subtraction for Gradient Boosting

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Other

Histogram-based gradient boosting (the approach behind LightGBM and XGBoost's `hist` tree method) bins each feature once, then finds splits from per-bin sums of gradients and hessians. Implement three functions over pre-binned data: - `build_histogram` returns `(G_hist, H_hist)`, each a float array of shape `(n_features, n_bins)` with per-bin gradient and hessian sums, restricted to the `sample_indices` rows if given. - `subtract_histogram` returns the sibling `(G, H)` tuple as parent minus child. - `find_best_split` returns `(feature, bin, gain)` of the best split, where samples with bin ≤ `bin` go left. ```python def build_histogram(binned, grad, hess, n_bins, sample_indices=None): """Return (G_hist, H_hist), each of shape (n_features, n_bins).""" def subtract_histogram(parent, child): """parent and child are (G_hist, H_hist) tuples. Return the sibling's tuple.""" def find_best_split(grad_hist, hess_hist, reg_lambda=1.0, gamma=0.0, min_child_weight=0.0): """Return (feature, bin, gain). Samples with bin <= bin go left.""" ``` The gain of a split is $$\text{gain} = \frac{1}{2}\left(\frac{G_L^2}{H_L + \lambda} + \frac{G_R^2}{H_R + \lambda} - \frac{G^2}{H + \lambda}\right) - \gamma$$ where $G_L, H_L$ are the left child's sums, $G_R, H_R$ the right child's, $G, H$ the node totals for that feature, $\lambda$ is `reg_lambda` and $\gamma$ is `gamma`. The requirements: - Candidate split bins are 0 to `n_bins - 2`. - A split is valid only if `HL >= min_child_weight` and `HR >= min_child_weight`. - Ties go to the lowest feature, then the lowest bin. - If no valid split has gain > 0, or `n_bins < 2`, return `(-1, -1, 0.0)`. - A 1-D `binned` is treated as a single feature. Example: ```python binned = [[0, 1], [1, 0], [2, 1], [0, 2]] grad = [1, -2, 3, 0.5] hess = [1, 2, 1, 0.5] G, H = build_histogram(binned, grad, hess, n_bins=3) # G = [[1.5, -2, 3], [-2, 4, 0.5]] # H = [[1.5, 2, 1], [2, 2, 0.5]] find_best_split([[-4, 2, 2]], [[2, 1, 1]], reg_lambda=1.0) # (0, 0, 5.3333) which is 16/3 ``` ```hint Avoid a loop over samples Each feature's histogram is a weighted count of its bin ids. NumPy can do a weighted count for a whole column in one call, as long as the output always has `n_bins` entries. ``` ```hint Scoring every bin at once Look at how $G_L$ and $H_L$ change when the split moves one bin to the right. Once you have the node totals, scoring a split should never need the samples again. ``` ```hint Picking the winner Make sure an invalid split can never win the comparison, and check that the way you take the maximum returns the lowest feature and then the lowest bin when two gains are equal. ``` ### Constraints & Assumptions - `binned` has shape `(n_samples, n_features)`, or `(n_samples,)` for a single feature. - Bin ids are integers in `[0, n_bins)`. - `grad` and `hess` have length `n_samples`, `hess > 0`, and `reg_lambda > 0`. ### What a Strong Answer Covers - Histogram construction that respects `sample_indices`, accepts a 1-D `binned`, and returns float arrays of shape `(n_features, n_bins)`. - Why parent minus child gives the sibling's histogram, applied elementwise to both G and H. - A split search that scores every candidate bin from the histograms alone, applies `min_child_weight` and `gamma`, and follows the tie rule. - The `(-1, -1, 0.0)` result when no valid split has positive gain and when `n_bins < 2`. - Time and space cost, and how it compares with exact split finding over sorted samples. ### Follow-up Questions - Which child should you build directly and which via subtraction? - How would you handle missing values in this scheme? - What precision issues can histogram subtraction cause?

Overview: Implement histogram-based split finding for gradient boosted trees: build per-bin gradient and hessian sums, derive a sibling histogram by subtraction, and pick the best split by regularized gain. It tests vectorized NumPy, second-order boosting math and careful handling of ties and invalid splits.

PracHub logo
General
Nov 24, 2026
hardMachine Learning EngineerOtherMachine Learning
0
0

Histogram-based gradient boosting (the approach behind LightGBM and XGBoost's hist tree method) bins each feature once, then finds splits from per-bin sums of gradients and hessians. Implement three functions over pre-binned data:

  • build_histogram returns (G_hist, H_hist) , each a float array of shape (n_features, n_bins) with per-bin gradient and hessian sums, restricted to the sample_indices rows if given.
  • subtract_histogram returns the sibling (G, H) tuple as parent minus child.
  • find_best_split returns (feature, bin, gain) of the best split, where samples with bin ≤ bin go left.
def build_histogram(binned, grad, hess, n_bins, sample_indices=None):
    """Return (G_hist, H_hist), each of shape (n_features, n_bins)."""

def subtract_histogram(parent, child):
    """parent and child are (G_hist, H_hist) tuples. Return the sibling's tuple."""

def find_best_split(grad_hist, hess_hist, reg_lambda=1.0, gamma=0.0, min_child_weight=0.0):
    """Return (feature, bin, gain). Samples with bin <= bin go left."""

The gain of a split is

gain=12(GL2HL+λ+GR2HR+λ−G2H+λ)−γ\text{gain} = \frac{1}{2}\left(\frac{G_L^2}{H_L + \lambda} + \frac{G_R^2}{H_R + \lambda} - \frac{G^2}{H + \lambda}\right) - \gamma

where GL,HLG_L, H_L are the left child's sums, GR,HRG_R, H_R the right child's, G,HG, H the node totals for that feature, λ\lambda is reg_lambda and γ\gamma is gamma. The requirements:

  • Candidate split bins are 0 to n_bins - 2 .
  • A split is valid only if HL >= min_child_weight and HR >= min_child_weight .
  • Ties go to the lowest feature, then the lowest bin.
  • If no valid split has gain > 0, or n_bins < 2 , return (-1, -1, 0.0) .
  • A 1-D binned is treated as a single feature.

Example:

binned = [[0, 1], [1, 0], [2, 1], [0, 2]]
grad = [1, -2, 3, 0.5]
hess = [1, 2, 1, 0.5]
G, H = build_histogram(binned, grad, hess, n_bins=3)
# G = [[1.5, -2, 3], [-2, 4, 0.5]]
# H = [[1.5, 2, 1], [2, 2, 0.5]]

find_best_split([[-4, 2, 2]], [[2, 1, 1]], reg_lambda=1.0)
# (0, 0, 5.3333)   which is 16/3

Constraints & Assumptions

  • binned has shape (n_samples, n_features) , or (n_samples,) for a single feature.
  • Bin ids are integers in [0, n_bins) .
  • grad and hess have length n_samples , hess > 0 , and reg_lambda > 0 .

What a Strong Answer Covers Guidance

  • Histogram construction that respects sample_indices , accepts a 1-D binned , and returns float arrays of shape (n_features, n_bins) .
  • Why parent minus child gives the sibling's histogram, applied elementwise to both G and H.
  • A split search that scores every candidate bin from the histograms alone, applies min_child_weight and gamma , and follows the tie rule.
  • The (-1, -1, 0.0) result when no valid split has positive gain and when n_bins < 2 .
  • Time and space cost, and how it compares with exact split finding over sorted samples.

Follow-up Questions Guidance

  • Which child should you build directly and which via subtraction?
  • How would you handle missing values in this scheme?
  • What precision issues can histogram subtraction cause?