Show that a random ID column tops impurity importance but not held-out permutation importance
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement five functions that show a random unique-ID column ranking at the top by impurity importance (mean decrease in impurity, MDI) but not by permutation importance measured on held-out data. `run_id_experiment` returns the dict shown below.
- `add_id_feature(X, seed=0)` appends `np.random.default_rng(seed).permutation(n)` as a float column, placed last, where `n` is the number of rows of `X`.
- `fit_deep_tree(X, y, random_state=0)` fits and returns `DecisionTreeClassifier(criterion="gini", max_depth=None, random_state=random_state)`.
- `impurity_importance(tree)` computes the weighted impurity decrease per split feature from `tree.tree_`, normalized to sum to 1. It must match `feature_importances_`.
- `permutation_importance_scores(tree, X, y, n_repeats=10, seed=0)` creates one `default_rng(seed)` and loops over features in the outer loop and repeats in the inner loop, calling `rng.permutation(n)` once per repeat. The score of a feature is baseline accuracy minus mean permuted accuracy.
- `run_id_experiment(X_train, y_train, X_test, y_test, seed=0, n_repeats=10, random_state=0)` adds IDs to `vstack([X_train, X_test])` with `seed`, then splits the rows back into train and test. It fits the deep tree on the training rows with `random_state`, and computes permutation importance on the test rows with `seed` and `n_repeats`. The rank of a score is 1 plus the number of scores strictly larger than it.
Return value of `run_id_experiment`, where `p` is the number of original features. The expected ranks assume labels with some noise, which is the setting the experiment is built for:
```python
{
"impurity_importance": np.ndarray, # shape (p + 1,)
"permutation_importance": np.ndarray, # shape (p + 1,)
"id_impurity_rank": int, # expected 1
"id_permutation_rank": int, # expected > 1
"train_accuracy": float, # 1.0 (memorised)
"test_accuracy": float,
}
```
Do not use `feature_importances_` or `sklearn.inspection.permutation_importance` in the implementation.
```hint Where the impurity numbers live
`tree.tree_` stores `impurity` and `weighted_n_node_samples` for every node, next to the child pointers and the split feature. Work out how much impurity each split removes from its parent.
```
```hint Reproducible permutation scores
The expected permutation scores depend on the exact sequence of random draws. Create the generator once and keep the loop order the prompt gives.
```
### Constraints & Assumptions
- `X_train` and `X_test` share the same `p` feature columns. After the ID is added, both have `p + 1` columns and the ID is the last one.
- The IDs are one permutation of `0..n-1` over the stacked train and test rows, so every ID is unique across both sets.
- The impurity scores must match the tree's own `feature_importances_`, and the permutation scores must follow the RNG order above exactly.
### What a Strong Answer Covers
- Building the ID column over the stacked data so IDs are unique across train and test, then splitting back in the original row order.
- Recomputing MDI from per-node impurity and weighted sample counts, with a normalization that matches `feature_importances_`, including a tree with no splits.
- Permutation importance on the test rows with one generator consumed in the specified order, and ranks computed with strict comparisons.
- Why the fully grown tree reaches training accuracy 1.0, and why the ID's held-out permutation importance is close to zero.
- The cost of permutation importance next to the cost of the tree fit.
### Follow-up Questions
- Why does impurity importance favour the ID column?
- How would you handle correlated features in permutation importance?
- Would limiting depth fix the bias?
Overview: Add a random unique-ID column to a dataset, fit a fully grown decision tree, and compare impurity-based feature importance with permutation importance on held-out data. Tests how MDI rewards high-cardinality features and how to compute both measures from tree internals with reproducible randomness.