Multi-Output Variance Reduction Split Criterion

Quick Overview

Implement a variance reduction criterion for regression tree splits when the target has several columns, plus an exhaustive search over features and midpoint thresholds. It tests population variance, size-weighted child impurity, min_samples_leaf filtering and deterministic tie-breaking.

Multi-Output Variance Reduction Split Criterion

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `variance_reduction(Y, left_mask)`, which returns the variance reduction of a split summed over all target columns of `Y`. Also implement `best_split(X, Y, min_samples_leaf=1)`, which searches every feature and threshold and returns `(feature, threshold, reduction)` for the best split, or `None` if no valid split exists. Both functions follow these rules: - Reduction = `sum(var(Y)) - n_L/n * sum(var(Y_L)) - n_R/n * sum(var(Y_R))` using population variance (`ddof=0`) per column. An empty child contributes 0. Return a `float`. - A 1-D `Y` is treated as a single target column. - Candidate thresholds are midpoints of consecutive sorted unique values of each feature. Samples with `X[:, j] <= t` go left. - Skip candidates where either child has fewer than `min_samples_leaf` samples. Any valid candidate (even zero gain) beats no split. Return `None` only when no candidate is valid. - Ties (within `1e-12`) break to the lowest feature index, then the lowest threshold. Return `feature` as `int` and `threshold`/`reduction` as `float`. For example: ```python X = [[1.0], [2.0], [3.0], [4.0]] Y = [[0, 10], [0, 10], [2, 20], [2, 20]] variance_reduction(Y, [True, True, False, False]) # 26.0 (1 + 25) variance_reduction(Y, [True, False, False, False]) # 26/3 best_split(X, Y) # (0, 2.5, 26.0) ``` ```hint One code path A single-target `Y` and a multi-target `Y` can go through the same variance computation if you change the shape of one of them first. ``` ```hint Ties and the starting best Look at the order in which you visit candidates, then pick the comparison and the initial best value so that a later near-equal gain never wins and a zero-gain split still beats having no split. ``` ### Constraints & Assumptions - `X` is a 2-D array-like of shape (n, d) with numeric values. - `Y` has shape (n, k) for k targets, or shape (n,) for a single target. - `left_mask` is a boolean sequence of length n. - NumPy is available. ### What a Strong Answer Covers - Per-column population variance, summed over targets, with each child weighted by its share of the samples and empty children handled - Shared handling of 1-D and 2-D targets - Candidate thresholds from sorted unique values, the `<=` rule, and `min_samples_leaf` filtering - Deterministic tie-breaking within `1e-12` and the `None` case when no candidate is valid - Time and space complexity of the search ### Follow-up Questions - How does your gain relate to scikit-learn's `DecisionTreeRegressor` impurity for multi-output targets? - What goes wrong when target columns have very different scales? - How would you make `best_split` run in O(d · n log n)?

Overview: Implement a variance reduction criterion for regression tree splits when the target has several columns, plus an exhaustive search over features and midpoint thresholds. It tests population variance, size-weighted child impurity, min_samples_leaf filtering and deterministic tie-breaking.

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

Implement variance_reduction(Y, left_mask), which returns the variance reduction of a split summed over all target columns of Y. Also implement best_split(X, Y, min_samples_leaf=1), which searches every feature and threshold and returns (feature, threshold, reduction) for the best split, or None if no valid split exists.

Both functions follow these rules:

  • Reduction = sum(var(Y)) - n_L/n * sum(var(Y_L)) - n_R/n * sum(var(Y_R)) using population variance ( ddof=0 ) per column. An empty child contributes 0. Return a float .
  • A 1-D Y is treated as a single target column.
  • Candidate thresholds are midpoints of consecutive sorted unique values of each feature. Samples with X[:, j] <= t go left.
  • Skip candidates where either child has fewer than min_samples_leaf samples. Any valid candidate (even zero gain) beats no split. Return None only when no candidate is valid.
  • Ties (within 1e-12 ) break to the lowest feature index, then the lowest threshold. Return feature as int and threshold / reduction as float .

For example:

X = [[1.0], [2.0], [3.0], [4.0]]
Y = [[0, 10], [0, 10], [2, 20], [2, 20]]
variance_reduction(Y, [True, True, False, False])   # 26.0  (1 + 25)
variance_reduction(Y, [True, False, False, False])  # 26/3
best_split(X, Y)                                    # (0, 2.5, 26.0)

Constraints & Assumptions

  • X is a 2-D array-like of shape (n, d) with numeric values.
  • Y has shape (n, k) for k targets, or shape (n,) for a single target.
  • left_mask is a boolean sequence of length n.
  • NumPy is available.

What a Strong Answer Covers Guidance

  • Per-column population variance, summed over targets, with each child weighted by its share of the samples and empty children handled
  • Shared handling of 1-D and 2-D targets
  • Candidate thresholds from sorted unique values, the <= rule, and min_samples_leaf filtering
  • Deterministic tie-breaking within 1e-12 and the None case when no candidate is valid
  • Time and space complexity of the search

Follow-up Questions Guidance

  • How does your gain relate to scikit-learn's DecisionTreeRegressor impurity for multi-output targets?
  • What goes wrong when target columns have very different scales?
  • How would you make best_split run in O(d · n log n)?