Weighted Decision Stump Split Search

Quick Overview

Write a decision stump in Python that searches every feature and midpoint threshold for the split with the largest weighted Gini gain, then predicts with it. It tests weighted impurity, deterministic tie-breaking, the no-split fallback and how a stump fits into boosting.

Weighted Decision Stump Split Search

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `fit_stump(X, y, sample_weight=None)`, which searches every feature and threshold for the single split that maximizes weighted Gini gain. It returns a dict with keys `feature`, `threshold`, `left_class`, `right_class` and `gain`. Also implement `predict_stump(stump, X)`, which returns a NumPy array of predicted labels. The stump follows these rules: - Candidate thresholds are the midpoints between consecutive unique values of each feature. Rows with `x <= threshold` go left. - Compute $\text{gain} = G(\text{parent}) - \sum_{\text{child}} \frac{W_{\text{child}}}{W_{\text{total}}}\, G(\text{child})$, where the Gini impurity $G$ uses weighted class totals and `sample_weight` defaults to all ones. - Replace the best split only on a strictly greater gain, so ties go to the lowest feature, then the lowest threshold. - Each leaf class is the weighted majority label, with ties going to the smallest label. - If no split has positive gain, return `feature` and `threshold` as `None`, `gain` as `0.0`, and both leaves set to the overall weighted majority. `predict_stump` then returns `left_class` for every row. For example: ```python X = [[1.0], [2.0], [3.0], [4.0]] y = [0, 1, 0, 0] w = [1.0, 5.0, 1.0, 1.0] fit_stump(X, y, sample_weight=w) # {'feature': 0, 'threshold': 2.5, 'left_class': 1, # 'right_class': 0, 'gain': 0.2604...} # 75/288 ``` ```hint Counting by class Map the labels to integers 0 to k-1 once. The weighted class totals of any subset of rows then take a single NumPy call, and the same totals give you the leaf labels. ``` ### Constraints & Assumptions - `X` is numeric with shape `(n, d)`, and `y` holds `n` labels. - `sample_weight`, when given, has `n` non-negative entries with a positive sum. ### What a Strong Answer Covers - Weighted class totals for the parent and each child, with each child's share of the gain measured by its total weight - Midpoint thresholds and `x <= threshold` routing, applied the same way in `fit_stump` and `predict_stump` - Tie rules for the split and the leaf labels that still hold when floating-point gains differ in the last bits - The no-gain fallback and the predictions it produces - The cost of the exhaustive search and a faster alternative ### Follow-up Questions - How would you make the split search faster than $O(d \cdot n^2)$? - How would this stump serve as a weak learner in AdaBoost? - Would switching from Gini to entropy usually change the chosen split, and when could it?

Overview: Write a decision stump in Python that searches every feature and midpoint threshold for the split with the largest weighted Gini gain, then predicts with it. It tests weighted impurity, deterministic tie-breaking, the no-split fallback and how a stump fits into boosting.

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

Implement fit_stump(X, y, sample_weight=None), which searches every feature and threshold for the single split that maximizes weighted Gini gain. It returns a dict with keys feature, threshold, left_class, right_class and gain. Also implement predict_stump(stump, X), which returns a NumPy array of predicted labels.

The stump follows these rules:

  • Candidate thresholds are the midpoints between consecutive unique values of each feature. Rows with x <= threshold go left.
  • Compute gain=G(parent)−∑childWchildWtotal G(child)\text{gain} = G(\text{parent}) - \sum_{\text{child}} \frac{W_{\text{child}}}{W_{\text{total}}}\, G(\text{child}) , where the Gini impurity GG uses weighted class totals and sample_weight defaults to all ones.
  • Replace the best split only on a strictly greater gain, so ties go to the lowest feature, then the lowest threshold.
  • Each leaf class is the weighted majority label, with ties going to the smallest label.
  • If no split has positive gain, return feature and threshold as None , gain as 0.0 , and both leaves set to the overall weighted majority. predict_stump then returns left_class for every row.

For example:

X = [[1.0], [2.0], [3.0], [4.0]]
y = [0, 1, 0, 0]
w = [1.0, 5.0, 1.0, 1.0]
fit_stump(X, y, sample_weight=w)
# {'feature': 0, 'threshold': 2.5, 'left_class': 1,
#  'right_class': 0, 'gain': 0.2604...}   # 75/288

Constraints & Assumptions

  • X is numeric with shape (n, d) , and y holds n labels.
  • sample_weight , when given, has n non-negative entries with a positive sum.

What a Strong Answer Covers Guidance

  • Weighted class totals for the parent and each child, with each child's share of the gain measured by its total weight
  • Midpoint thresholds and x <= threshold routing, applied the same way in fit_stump and predict_stump
  • Tie rules for the split and the leaf labels that still hold when floating-point gains differ in the last bits
  • The no-gain fallback and the predictions it produces
  • The cost of the exhaustive search and a faster alternative

Follow-up Questions Guidance

  • How would you make the split search faster than O(d⋅n2)O(d \cdot n^2) ?
  • How would this stump serve as a weak learner in AdaBoost?
  • Would switching from Gini to entropy usually change the chosen split, and when could it?