Quantile Histogram Pre-Binning with Missing Bin

Quick Overview

Implement quantile pre-binning for histogram gradient boosting in NumPy: per-feature thresholds from midpoints or percentiles, left-sided bin assignment and a reserved code for missing values. It tests careful handling of NaN, ties at thresholds, uint8 bin codes and input validation.

Quantile Histogram Pre-Binning with Missing Bin

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `fit_bin_edges(X, max_bin=255)`, returning one sorted `float64` threshold array per feature, and `apply_bins(X, bin_edges, max_bin=255)`, returning a `uint8` array of bin codes with the same shape as `X`. The functions follow these rules: - Fitting ignores `NaN`. If a feature has at most `max_bin` distinct values, the thresholds are the midpoints between consecutive distinct values. - Otherwise, the thresholds are `np.percentile(col, np.linspace(0, 100, max_bin + 1)[1:-1], method="midpoint")`. - A value's code is `np.searchsorted(thresholds, x, side="left")`, so a value equal to a threshold goes to the left bin. - `NaN` maps to the reserved code `max_bin`. - Raise `ValueError` if `max_bin` is not an integer in [2, 255], `X` is not 2-D, or `len(bin_edges)` does not match the number of features. Example: ```python import numpy as np X = [[3.0], [1.0], [2.0], [2.0], [np.nan]] edges = fit_bin_edges(X, max_bin=4) # [array([1.5, 2.5])] apply_bins(X, edges, max_bin=4) # [[2], [0], [1], [1], [4]], dtype uint8 ``` ```hint Missing values in NumPy Check what `np.unique`, `np.percentile` and `np.searchsorted` each do with a `NaN` in the input before deciding where to filter or overwrite it. ``` ```hint Room for the missing code Count how many thresholds each branch can produce and the largest code a non-missing value can get. Compare that with `max_bin` and with the range of `uint8`. ``` ### Constraints & Assumptions - `X` is numeric. `NaN` marks missing values, and all other values are finite. - Features are binned independently. Unseen values outside the training range clip to the first or last bin. - Output must match scikit-learn's `_BinMapper(n_bins=max_bin + 1)`. ### What a Strong Answer Covers - Per-feature thresholds from non-missing values, with the distinct-value branch and the percentile branch both matching the rules. - Left-sided binary search, so ties at a threshold land in the left bin, and out-of-range values clip to the end bins. - A reserved missing code that can never collide with a real bin, and a `uint8` output that cannot overflow. - The input validation, plus edge cases such as constant or all-missing columns. - Fit and apply complexity per feature. ### Follow-up Questions - How would you scale fitting to 100M rows? - How should categorical features be binned differently? - Why reserve a separate missing bin instead of imputing?

Overview: Implement quantile pre-binning for histogram gradient boosting in NumPy: per-feature thresholds from midpoints or percentiles, left-sided bin assignment and a reserved code for missing values. It tests careful handling of NaN, ties at thresholds, uint8 bin codes and input validation.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement fit_bin_edges(X, max_bin=255), returning one sorted float64 threshold array per feature, and apply_bins(X, bin_edges, max_bin=255), returning a uint8 array of bin codes with the same shape as X.

The functions follow these rules:

  • Fitting ignores NaN . If a feature has at most max_bin distinct values, the thresholds are the midpoints between consecutive distinct values.
  • Otherwise, the thresholds are np.percentile(col, np.linspace(0, 100, max_bin + 1)[1:-1], method="midpoint") .
  • A value's code is np.searchsorted(thresholds, x, side="left") , so a value equal to a threshold goes to the left bin.
  • NaN maps to the reserved code max_bin .
  • Raise ValueError if max_bin is not an integer in [2, 255], X is not 2-D, or len(bin_edges) does not match the number of features.

Example:

import numpy as np

X = [[3.0], [1.0], [2.0], [2.0], [np.nan]]
edges = fit_bin_edges(X, max_bin=4)   # [array([1.5, 2.5])]
apply_bins(X, edges, max_bin=4)       # [[2], [0], [1], [1], [4]], dtype uint8

Constraints & Assumptions

  • X is numeric. NaN marks missing values, and all other values are finite.
  • Features are binned independently. Unseen values outside the training range clip to the first or last bin.
  • Output must match scikit-learn's _BinMapper(n_bins=max_bin + 1) .

What a Strong Answer Covers Guidance

  • Per-feature thresholds from non-missing values, with the distinct-value branch and the percentile branch both matching the rules.
  • Left-sided binary search, so ties at a threshold land in the left bin, and out-of-range values clip to the end bins.
  • A reserved missing code that can never collide with a real bin, and a uint8 output that cannot overflow.
  • The input validation, plus edge cases such as constant or all-missing columns.
  • Fit and apply complexity per feature.

Follow-up Questions Guidance

  • How would you scale fitting to 100M rows?
  • How should categorical features be binned differently?
  • Why reserve a separate missing bin instead of imputing?