Global surrogate decision tree: measure fidelity to a black box and extract one rule per leaf
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Given a black-box `predict_fn(X)` that returns class labels, fit a shallow `DecisionTreeClassifier` on its predictions, measure how well the tree agrees with the black box, and extract one human-readable rule per leaf.
Implement four functions:
- `fit_surrogate(predict_fn, X, max_depth, random_state=0)` fits `DecisionTreeClassifier(criterion="gini", max_depth=max_depth, random_state=random_state)` on `X` with labels `predict_fn(X)` and returns the fitted tree.
- `fidelity(surrogate, predict_fn, X)` returns the fraction of rows where the surrogate and the black box predict the same label, as a `float`.
- `fidelity_curve(predict_fn, X_train, X_test, depths, random_state=0)` fits a surrogate on `X_train` for each depth and scores its fidelity on `X_test`. It returns a float `np.ndarray` with one value per depth.
- `extract_rules(surrogate, feature_names=None)` returns one string per leaf, depth-first with the left child before the right, each of the form `"if <conds> then class=<label>"`.
The rule strings follow these conventions:
- Join the conditions on the root-to-leaf path with `" and "`. Write each one as `name <= thr` for a left branch or `name > thr` for a right branch, with `thr` formatted `:.4f`.
- The label is the majority class of the leaf (`classes_[argmax(value)]`).
- Default feature names are `x0, x1, ...`.
- A tree that is a single leaf yields `"if True then class=<label>"`.
Example:
```python
import numpy as np
X = np.array([[a, b] for a in range(10) for b in range(10)], dtype=float)
bb = lambda X: ((X[:, 0] > 4.5) & (X[:, 1] > 2.5)).astype(int)
fidelity_curve(bb, X, X, [1, 2, 3]) # array([0.85, 1.0, 1.0])
extract_rules(fit_surrogate(bb, X, 2))
# ['if x0 <= 4.5000 then class=0',
# 'if x0 > 4.5000 and x1 <= 2.5000 then class=0',
# 'if x0 > 4.5000 and x1 > 2.5000 then class=1']
```
```hint Walking the fitted tree
`surrogate.tree_` exposes parallel arrays: `children_left`, `children_right`, `feature`, `threshold` and `value`. A node whose left child is -1 is a leaf.
```
```hint Split direction
Check which side sklearn sends a sample whose feature value equals the threshold, and make the rule text agree with it.
```
### Constraints & Assumptions
- `predict_fn` accepts a 2-D float array and returns a 1-D label array with one label per row.
- The fitted tree must match an sklearn oracle with the same settings exactly.
### What a Strong Answer Covers
- Fitting the tree on the black box's predictions as the target, with the exact criterion, depth and seed so it matches the reference fit.
- Fidelity as agreement with the black box, and why the curve is scored on held-out rows.
- A depth-first walk of `tree_` that visits left before right, uses `<=` on left branches and `>` on right branches, and formats thresholds to four decimals.
- Leaf labels mapped through `classes_`, default feature names, and the single-leaf `True` case.
- The cost of fitting the curve and of extracting the rules.
### Follow-up Questions
- Why is fidelity not the same as accuracy, and when can high fidelity mislead?
- How would you choose the surrogate depth?
- How would you make the rule set more compact for stakeholders?
Overview: Fit a shallow scikit-learn decision tree on a black-box classifier's predictions, measure its fidelity across depths on held-out data, and print one readable rule per leaf. Tests tree internals, sklearn split semantics and the trade-off between interpretability and faithfulness.