Permutation Feature Importance

medium
Unit 15 · Interpretability II0/2 to milestone

Which features does a trained model actually rely on? Permutation importance answers it for any model: shuffle one feature column, breaking its link to the target, and measure how much the score drops.

Implement permutation_importance(predict, X, y, metric, n_repeats=5, seed=0):

  • predict(X) returns predictions, and metric(y, y_pred) returns a score where higher is better.
  • Compute the baseline score on the unshuffled data.
  • For each feature j, in column order, repeat n_repeats times: copy X, shuffle column j with rng.permutation, and record baseline − score. Use one rng = np.random.default_rng(seed) created at the start, so results are reproducible.
  • Return an array of the mean drop per feature. Don't modify X.
#interpretability#evaluation#numpy2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Permutation Feature Importance · TechGradify