Implement Vectorized Prediction for Oblivious Decision Tree Ensembles

Quick Overview

Implement leaf indexing and prediction for oblivious decision trees, where every level shares one split, plus the sum over a boosted ensemble, all vectorized in numpy. It tests bit packing of comparisons, input validation, multi-output leaves, and why this tree shape makes inference fast.

Implement Vectorized Prediction for Oblivious Decision Tree Ensembles

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

An oblivious tree of depth `D` applies the same split `(features[d], thresholds[d])` to every node at level `d`. CatBoost builds its trees this way. Implement `oblivious_leaf_index`, `oblivious_predict` and `ensemble_predict`, vectorized over all rows of `X` (no per-row Python loops). ```python def oblivious_leaf_index(X, features, thresholds): ... def oblivious_predict(X, features, thresholds, leaf_values): ... def ensemble_predict(X, trees, base_score=0.0, learning_rate=1.0): ... ``` Requirements: - The leaf index of a row is the sum over `d` of `(X[:, features[d]] > thresholds[d]) * 2**d`. Level 0 is the least significant bit, and a value equal to the threshold gives bit 0. Return `int64`. - `oblivious_predict` returns `leaf_values[idx]`, with shape `(n,)`, or `(n, k)` for multi-output leaves. - Depth 0 (empty `features` and `thresholds`) maps every row to leaf 0. - Raise `ValueError` if `X` is not 2-D, if `features` and `thresholds` have different lengths, if a feature index is out of range, or if `leaf_values` does not have `2**depth` rows. - `ensemble_predict` takes `trees` as a list of `(features, thresholds, leaf_values)` tuples and returns `base_score + learning_rate * sum(tree outputs)`. An empty list gives `base_score` for every row. Example: ```python X = [[0.5, 2.0], [1.5, 0.0], [1.5, 3.0], [0.0, 0.0]] features, thresholds = [0, 1], [1.0, 1.0] oblivious_leaf_index(X, features, thresholds) # [2, 1, 3, 0] oblivious_predict(X, features, thresholds, [10, 20, 30, 40]) # [30., 20., 40., 10.] ``` ```hint Edge cases in the indexing Decide what depth 0 should produce before writing the bit packing, and check what numpy does with a negative feature index: `X[:, -1]` silently reads the last column. ``` ### Constraints & Assumptions - `X` has shape `(n, n_features)`, and valid feature indices are integers in `[0, n_features)`. - A tree of depth `D` has `2**D` leaves, so `leaf_values` has shape `(2**D,)` or `(2**D, k)`. - numpy is available. ### What a Strong Answer Covers - Packing the D comparisons of a row into a leaf index with the stated bit order and tie rule - A computation that is vectorized over rows and handles depth 0 - Validation for every listed error, including negative feature indices - Gathering single-output and multi-output leaf values, and combining trees with the base score and learning rate - Time and space cost per tree and for the whole ensemble ### Follow-up Questions - Why are oblivious trees fast at inference compared to ordinary depth-wise trees? - How would you vectorize across all trees of the ensemble at once? - How does CatBoost handle categorical features and missing values at prediction time?

Overview: Implement leaf indexing and prediction for oblivious decision trees, where every level shares one split, plus the sum over a boosted ensemble, all vectorized in numpy. It tests bit packing of comparisons, input validation, multi-output leaves, and why this tree shape makes inference fast.

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

An oblivious tree of depth D applies the same split (features[d], thresholds[d]) to every node at level d. CatBoost builds its trees this way. Implement oblivious_leaf_index, oblivious_predict and ensemble_predict, vectorized over all rows of X (no per-row Python loops).

def oblivious_leaf_index(X, features, thresholds): ...
def oblivious_predict(X, features, thresholds, leaf_values): ...
def ensemble_predict(X, trees, base_score=0.0, learning_rate=1.0): ...

Requirements:

  • The leaf index of a row is the sum over d of (X[:, features[d]] > thresholds[d]) * 2**d . Level 0 is the least significant bit, and a value equal to the threshold gives bit 0. Return int64 .
  • oblivious_predict returns leaf_values[idx] , with shape (n,) , or (n, k) for multi-output leaves.
  • Depth 0 (empty features and thresholds ) maps every row to leaf 0.
  • Raise ValueError if X is not 2-D, if features and thresholds have different lengths, if a feature index is out of range, or if leaf_values does not have 2**depth rows.
  • ensemble_predict takes trees as a list of (features, thresholds, leaf_values) tuples and returns base_score + learning_rate * sum(tree outputs) . An empty list gives base_score for every row.

Example:

X = [[0.5, 2.0], [1.5, 0.0], [1.5, 3.0], [0.0, 0.0]]
features, thresholds = [0, 1], [1.0, 1.0]
oblivious_leaf_index(X, features, thresholds)                 # [2, 1, 3, 0]
oblivious_predict(X, features, thresholds, [10, 20, 30, 40])  # [30., 20., 40., 10.]

Constraints & Assumptions

  • X has shape (n, n_features) , and valid feature indices are integers in [0, n_features) .
  • A tree of depth D has 2**D leaves, so leaf_values has shape (2**D,) or (2**D, k) .
  • numpy is available.

What a Strong Answer Covers Guidance

  • Packing the D comparisons of a row into a leaf index with the stated bit order and tie rule
  • A computation that is vectorized over rows and handles depth 0
  • Validation for every listed error, including negative feature indices
  • Gathering single-output and multi-output leaf values, and combining trees with the base score and learning rate
  • Time and space cost per tree and for the whole ensemble

Follow-up Questions Guidance

  • Why are oblivious trees fast at inference compared to ordinary depth-wise trees?
  • How would you vectorize across all trees of the ensemble at once?
  • How does CatBoost handle categorical features and missing values at prediction time?