Sub topic 4
Permutation importance: break a feature and watch the score
This chapter assumes you can train a scikit-learn model, split data into train and test sets, and read a score such as R². It covers the core idea of permutation importance and a first run on real data. Later chapters cover the step-by-step procedure and its pitfalls.
The core idea
A feature matters to a model if the model gets worse when it loses that feature's information. Permutation importance tests this without retraining. It takes a fitted model and a held-out dataset, then shuffles one column at random. The shuffle keeps the column's values but breaks their link to each row's target. It then scores the model again.
If the score barely moves, the model did not rely on that feature. If the score falls sharply, the model depended on it. The importance is the size of the drop:
importance = baseline score − score with the column shuffled
Because the method only calls predict and a scoring function, it works with any model. This is what "model-agnostic" means here.
Why shuffling and not deleting
You could set the column to zero or drop it. Setting it to zero creates values the model may never have seen, such as an income of 0. Dropping it requires retraining, which is slow and measures a different model. Shuffling keeps the feature's distribution unchanged, so the inputs stay plausible one column at a time. Only the connection to the target is destroyed.
A first run
The example below fits a random forest on the California housing data and measures importance on the test split. It repeats the shuffle 10 times per feature, so you get a mean and a spread.
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor
from sklearn.inspection import permutation_importance
from sklearn.model_selection import train_test_split
X, y = fetch_california_housing(as_frame=True, return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
model = RandomForestRegressor(n_estimators=100, n_jobs=-1, random_state=0)
model.fit(X_train, y_train)
result = permutation_importance(
model, X_test, y_test, n_repeats=10, random_state=0, n_jobs=-1
)
for i in result.importances_mean.argsort()[::-1]:
print(f"{X.columns[i]:<12} {result.importances_mean[i]:.3f} +/- {result.importances_std[i]:.3f}")Each line shows a feature, the average drop in R² across the 10 shuffles, and the standard deviation of those drops. The features are sorted from largest drop to smallest. Median income (MedInc) should rank near the top, because it is the strongest signal for house value in this dataset.
The standard deviation tells you whether two features are really different. If two means sit within one standard deviation of each other, do not read much into their order.
Reading the numbers
- A large positive value means the model relies on the feature.
- A value near zero means the model ignores the feature, or the feature's information is duplicated elsewhere.
- A small negative value means shuffling happened to help by chance. Treat it as zero.
- The unit is the score's unit. Here, 0.1 means R² fell by 0.1.
Why it improves on built-in importances
A random forest also exposes feature_importances_, which is computed from training splits and can favor features with many distinct values. Permutation importance is computed on data the model did not train on, so it reflects what helps predictions that generalize. The earlier chapter on impurity importance gives the details.
Exercise
You run permutation importance and the feature AveRooms shows a mean drop of 0.002 with a standard deviation of 0.004. What can you conclude, and what should you check before calling the feature unimportant?
Show answer
The drop is smaller than its spread, so the data show no reliable effect from shuffling AveRooms. Before calling it unimportant, check whether it is strongly correlated with another feature (such as AveBedrms). A correlated partner can cover for it when it is shuffled, which hides its true importance.