How MDI and Permutation Importance Split Credit Between Correlated Feature Copies

Quick Overview

Append a noisy copy of one feature, then compare how mean decrease in impurity and permutation importance split credit between the two columns across several max_features values. Tests computing MDI from scikit-learn tree arrays, seeded permutation loops, and reasoning about correlated features in random forests.

How MDI and Permutation Importance Split Credit Between Correlated Feature Copies

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Append a noisy copy of feature `col` to `X`, then measure how mean decrease in impurity (MDI) and permutation importance divide credit between the original column and the copy, for each value in `max_features_list`. Accuracy is the fraction of rows where `model.predict(X) == y`. Implement these four functions: - `add_near_duplicate(X, col, noise_scale, seed)`: return `X` as float with `X[:, col] + noise_scale * rng.standard_normal(n)` appended as the last column, where `rng = np.random.default_rng(seed)` and n is the number of rows. - `forest_mdi_importance(forest)`: compute MDI from each fitted tree's `tree_` arrays and match `forest.feature_importances_`. It must work for both a fitted `RandomForestClassifier` and a fitted `RandomForestRegressor`, and the result sums to 1. - `permutation_importance_accuracy(model, X, y, n_repeats, seed)`: create one `np.random.default_rng(seed)`, loop over features in the outer loop and repeats in the inner loop, and draw `rng.permutation(n)` for each shuffle. Each drop is baseline accuracy minus permuted accuracy. Return `(mean, std)`, two arrays with one entry per feature, with std computed with `ddof=0`. `model` can be any object exposing `predict(X)`, not only a forest. - `run_credit_split_experiment(X, y, col, noise_scale, max_features_list, n_estimators, n_repeats, seed)`: augment `X` once with `seed`. For each value `mf`, fit `RandomForestClassifier(n_estimators=n_estimators, max_features=int(mf), random_state=seed)` on the augmented data and score both importances on that same training data, using `n_repeats` and `seed` for the permutation scores. Return a DataFrame with columns `max_features` (int64), `mdi_original`, `mdi_copy`, `perm_original`, `perm_copy` (float64), in that order, with one row per value of `max_features_list`. ```hint MDI from tree arrays Each internal node gives its split feature the node's weighted impurity minus the weighted impurity of its two children. Check how scikit-learn scales one tree's vector and which trees the forest leaves out of its average. ``` ```hint Reproducible shuffles Every permutation score depends on the order of the `rng.permutation` calls, so keep the loop order exactly as specified. ``` ### Constraints & Assumptions - `X` is a numeric array of shape (n, p) and `y` holds n class labels. - `col` is a 0-based column index, and the copy becomes column p of the augmented matrix. - Each value in `max_features_list` is a whole number no larger than the number of columns after augmentation. ### What a Strong Answer Covers - The per-node impurity decrease, the scaling and per-tree normalization, and the forest average that together reproduce `feature_importances_`, including forests with single-leaf trees. - A permutation loop whose generator order matches the spec, with the baseline computed once and a per-feature mean and population standard deviation. - The experiment table with the exact columns, order and dtypes. - How each importance measure divides credit between near-duplicate columns, and how `max_features` shifts that split. - The limits of scoring importance on the training data. ### Follow-up Questions - Why does MDI split credit between the copies while permutation importance can make both look unimportant? - How does `max_features` change the split of credit? - How would you get a fairer importance for a group of correlated features?

Overview: Append a noisy copy of one feature, then compare how mean decrease in impurity and permutation importance split credit between the two columns across several max_features values. Tests computing MDI from scikit-learn tree arrays, seeded permutation loops, and reasoning about correlated features in random forests.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Append a noisy copy of feature col to X, then measure how mean decrease in impurity (MDI) and permutation importance divide credit between the original column and the copy, for each value in max_features_list. Accuracy is the fraction of rows where model.predict(X) == y.

Implement these four functions:

  • add_near_duplicate(X, col, noise_scale, seed) : return X as float with X[:, col] + noise_scale * rng.standard_normal(n) appended as the last column, where rng = np.random.default_rng(seed) and n is the number of rows.
  • forest_mdi_importance(forest) : compute MDI from each fitted tree's tree_ arrays and match forest.feature_importances_ . It must work for both a fitted RandomForestClassifier and a fitted RandomForestRegressor , and the result sums to 1.
  • permutation_importance_accuracy(model, X, y, n_repeats, seed) : create one np.random.default_rng(seed) , loop over features in the outer loop and repeats in the inner loop, and draw rng.permutation(n) for each shuffle. Each drop is baseline accuracy minus permuted accuracy. Return (mean, std) , two arrays with one entry per feature, with std computed with ddof=0 . model can be any object exposing predict(X) , not only a forest.
  • run_credit_split_experiment(X, y, col, noise_scale, max_features_list, n_estimators, n_repeats, seed) : augment X once with seed . For each value mf , fit RandomForestClassifier(n_estimators=n_estimators, max_features=int(mf), random_state=seed) on the augmented data and score both importances on that same training data, using n_repeats and seed for the permutation scores. Return a DataFrame with columns max_features (int64), mdi_original , mdi_copy , perm_original , perm_copy (float64), in that order, with one row per value of max_features_list .

Constraints & Assumptions

  • X is a numeric array of shape (n, p) and y holds n class labels.
  • col is a 0-based column index, and the copy becomes column p of the augmented matrix.
  • Each value in max_features_list is a whole number no larger than the number of columns after augmentation.

What a Strong Answer Covers Guidance

  • The per-node impurity decrease, the scaling and per-tree normalization, and the forest average that together reproduce feature_importances_ , including forests with single-leaf trees.
  • A permutation loop whose generator order matches the spec, with the baseline computed once and a per-feature mean and population standard deviation.
  • The experiment table with the exact columns, order and dtypes.
  • How each importance measure divides credit between near-duplicate columns, and how max_features shifts that split.
  • The limits of scoring importance on the training data.

Follow-up Questions Guidance

  • Why does MDI split credit between the copies while permutation importance can make both look unimportant?
  • How does max_features change the split of credit?
  • How would you get a fairer importance for a group of correlated features?