Decision Tree Root-Split Instability Experiment
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement a Gini CART classifier (`gini`, `best_split`, `fit_tree`, `predict_tree`) and `instability_experiment(X, y, X_test, resample_indices, max_depth=None)`. Fit a reference tree on all the data, refit on each index array in `resample_indices`, and compare each refit's root split and `X_test` predictions with the reference.
Conventions:
- `gini(y)` returns the Gini impurity ${1 - \sum_k p_k^2}$ of a label array, and `0.0` for an empty array.
- `best_split(X, y)` returns `(feature, threshold)` over the midpoints of each feature's sorted unique values, with rows where `x <= t` going left. Only a strictly positive gain counts, and ties keep the first split found (lowest feature, then lowest threshold). It returns `None` when no split qualifies.
- A leaf is `{"leaf": True, "value": v}`, where `v` is the majority label (ties go to the smallest label). An internal node is `{"leaf": False, "feature": f, "threshold": t, "left": ..., "right": ...}`. Growth stops at a pure node, a node with fewer than 2 rows, a node with no split, or `depth >= max_depth` (the root has depth 0).
- `predict_tree(tree, X)` routes each row to a leaf with the same `<=` rule and returns the leaf labels.
- A tree's root is `(feature, threshold)`, or `None` if the tree is a single leaf. A root counts as changed if exactly one of the two roots is `None`, the features differ, or the thresholds differ by more than `1e-9`. The feature-change rate compares features only, with `None` standing for a leaf.
- Disagreement for one run is the fraction of `X_test` rows where the refit's predictions differ from the reference's. With no resamples, all rates and the mean are `0.0`.
- The result has the keys `reference_root`, `root_splits`, `root_change_rate`, `root_feature_change_rate`, `disagreement_per_run` (a numpy array) and `mean_disagreement`. Rates and means are Python `float`s.
Example:
```python
X = [[1, 1], [2, 2], [4, 3], [3, 4], [5, 5], [6, 6]]
y = [0, 0, 0, 1, 1, 1]
X_test = [[1, 1], [2, 5], [5, 2], [6, 6], [3.5, 4], [4.6, 3.2]]
resample_indices = [[0, 1, 2, 3, 4, 5], [0, 1, 3, 4, 5],
[1, 2, 3, 4, 5], [0, 1, 2, 4, 5]]
instability_experiment(X, y, X_test, resample_indices, max_depth=1)
# {
# "reference_root": (1, 3.5),
# "root_splits": [(1, 3.5), (0, 2.5), (1, 3.5), (0, 4.5)],
# "root_change_rate": 0.5,
# "root_feature_change_rate": 0.5,
# "disagreement_per_run": array([0.0, 0.5, 0.0, 0.6667]),
# "mean_disagreement": 0.2917, # 7/24
# }
```
```hint Ties in floating point
Two splits that are equally good in exact arithmetic can produce gains that differ in the last bits. Decide how your comparison keeps the first one found.
```
```hint Comparing runs
Every run is compared with the same reference predictions on the same `X_test`. Think about what the root looks like when a resample contains only one class.
```
### Constraints & Assumptions
- Features are numeric. Labels are discrete with 2 or more classes.
- Resample index arrays may repeat indices and may produce single-class subsets.
### What a Strong Answer Covers
- A Gini split search over midpoint thresholds that counts only strictly positive gain and keeps the first-found split on ties, with a tolerance so floating-point noise does not break a tie.
- Tree growth with every stopping rule, majority leaves that break ties toward the smallest label, and prediction with the same `<=` rule.
- Root comparison that handles leaf roots (`None`) and the threshold tolerance, plus the separate feature-only rate.
- Per-run disagreement against reference predictions computed once, the no-resample case, and Python `float` outputs.
- The cost of the experiment and what the metrics say about the variance of greedy trees.
### Follow-up Questions
- Why do deep trees show higher disagreement than stumps under resampling?
- How would bagging change these instability metrics?
- Why can the root change while predictions barely disagree?
Overview: Build a small Gini decision tree from scratch, refit it on resampled training sets, and measure how often the root split changes and how much test predictions disagree with a full-data tree. It tests CART split search, tie-breaking rules and the high variance of greedy trees.