Explainable AI (XAI) Beginner 12 minutes

Permutation Feature Importance

A simple, model-agnostic way to measure the model's reliance on each feature, and its pitfalls.

Your progress in the track0%

The algorithm

  1. Compute the model's performance \(s\) on the evaluation data.
  2. For each feature \(j\): shuffle its column, compute the performance \(s_j\), and repeat \(K\) times.
  3. Importance = \(s – \overline{s_j}\).
from sklearn.ensemble import RandomForestClassifier
from sklearn.inspection import permutation_importance
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
X, y = load_breast_cancer(return_X_y=True, as_frame=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)
rf = RandomForestClassifier(n_estimators=300, random_state=0).fit(X_tr, y_tr)
r = permutation_importance(rf, X_te, y_te, n_repeats=20, random_state=0, scoring="roc_auc")
for i in r.importances_mean.argsort()[::-1][:5]:
    print(f"{X.columns[i]:<25} {r.importances_mean[i]:.4f} ± {r.importances_std[i]:.4f}")

Pitfalls

PitfallEffectTreatment
Correlated featuresImportance is shared or disappearsGroup correlated features and permute them together
Unrealistic examplesPermuting height without weight produces impossible casesConditional methods, or caution in interpretation
Training dataImportance for noisy featuresUse test data
MDIBias towards features with many valuesPrefer permutation importance

Importance is not causal

The importance of the feature “number of hospital visits” for predicting death does not mean that visits cause death; it may be a sign of disease severity. Causal questions need a causal design (experiments or causal inference), not explanation tools alone.

Lesson terms

Permutation importance
The drop in the model's performance when the values of one feature are shuffled at random in the evaluation set.
Impurity importance
An importance measure in trees that sums the impurity decrease at each split on the feature; it is biased towards features with many values.

Abstract

Permutation importance measures the drop in the model's performance when the values of one feature are shuffled at random, breaking its relationship with the target. It is computed on separate test data and repeated several times to estimate the spread. Impurity importance in trees (MDI) is biased in favour of features with many values. With correlated features, importance may be split or disappear, and permutation may create unrealistic examples. Importance means the model relies on the feature, not that there is a causal relationship.

Review questions

  1. Why do we compute permutation importance on test data rather than training data?

    Because an overfitted model may rely on noisy features in training that do not help generalization.

  2. What happens to the importance of two highly correlated features?

    Each may look unimportant, because the model compensates with the other when one of them is shuffled.

Sources and references

  1. scikit-learn — Permutation feature importance
  2. Breiman (2001) — Random Forests
  3. Strobl et al. (2007) — Bias in random forest variable importance measures

Complete the lesson