Decision Tree Depth Sweep with Leaf Rectangles
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement three functions. `fit_tree(X, y, max_depth, random_state=0)` returns a fitted scikit-learn `DecisionTreeClassifier`. `leaf_rectangles(estimator)` returns one dict per leaf. `depth_sweep(X_train, y_train, X_test, y_test, depths, random_state=0)` returns one summary dict per depth, in input order.
Requirements:
- Each leaf dict has the keys `bounds` and `label`. `bounds` is an `(n_features, 2)` float array of `[low, high]` rows, and a point is inside when `low < x <= high` on every feature.
- Unbounded sides are `-inf` and `inf`. A split sets the left child's `high` and the right child's `low` to the threshold.
- Leaves are ordered depth-first, visiting the left child before the right.
- `label` is `classes_[argmax(node value)]`.
- Each summary dict has the keys `max_depth`, `n_leaves` (int), `train_accuracy` and `test_accuracy` (float), and `rectangles`. `None` in `depths` means unlimited depth.
Example (bounds shown as nested lists for readability):
```python
X = [[1, 1], [2, 1], [1, 3], [2, 3], [3, 1], [4, 1], [3, 3], [4, 3]]
y = [0, 0, 0, 0, 1, 1, 2, 2]
X_test = [[0, 0], [5, 0], [5, 5], [2, 5]]
y_test = [0, 1, 2, 1]
depth_sweep(X, y, X_test, y_test, [1])[0]
# n_leaves=2, train_accuracy=0.75, test_accuracy=0.5
# rectangles: [{bounds: [[-inf, 2.5], [-inf, inf]], label: 0},
# {bounds: [[2.5, inf], [-inf, inf]], label: 1}]
```
```hint Recovering a leaf's box
Walk the fitted `tree_` arrays from the root and carry a bounds array for each node. At every split only one row of that array changes for each child. If you use a stack, think about which child to push first.
```
### Constraints & Assumptions
- `X_train` and `X_test` are numeric with shape `(n_samples, n_features)`. Labels can be integers or strings.
- `depths` holds positive integers and `None`, and the output follows the same order.
- The same `random_state` is used for every tree in the sweep.
### What a Strong Answer Covers
- Fitting one tree per depth with a fixed `random_state`, with `None` treated as unlimited depth.
- Recovering each leaf's box by tightening the split feature's upper bound on the left branch and its lower bound on the right branch, consistent with the `(low, high]` convention.
- Leaves in left-first depth-first order, each labeled with `classes_[argmax]`, including how a tied leaf is labeled.
- Summary dicts with the required keys and types, and what the train and test accuracy curves show as depth grows.
- The cost of the traversal and edge cases such as a single-leaf tree.
### Follow-up Questions
- How would you choose `max_depth` from a sweep like this without making the reported test accuracy optimistic?
- Why can the leaf count and both accuracies stop changing even as `max_depth` keeps rising?
- Can a different `random_state` change the rectangles when the features and data stay the same? Why?
Overview: Fit decision trees at several maximum depths, recover the axis-aligned box each leaf covers from the fitted scikit-learn tree, and report leaf counts with train and test accuracy for each depth. It tests reading the tree_ arrays, half-open split bounds, and spotting overfitting as depth grows.