Permutation Feature Importance
A simple, model-agnostic way to measure the model's reliance on each feature, and its pitfalls.
The algorithm
- Compute the model's performance \(s\) on the evaluation data.
- For each feature \(j\): shuffle its column, compute the performance \(s_j\), and repeat \(K\) times.
- 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
| Pitfall | Effect | Treatment |
|---|---|---|
| Correlated features | Importance is shared or disappears | Group correlated features and permute them together |
| Unrealistic examples | Permuting height without weight produces impossible cases | Conditional methods, or caution in interpretation |
| Training data | Importance for noisy features | Use test data |
| MDI | Bias towards features with many values | Prefer 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
-
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.
-
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
Complete the lesson
You have completed all the lessons in the track! You can now request the track completion certificate.
Request the certificate