Sub topic 12
Interpreting feature importance
This chapter assumes you can train a random forest in scikit-learn and know the difference between training and held-out data. It covers the two standard ways to rank features, and the mistakes that make those rankings misleading.
Impurity-based importance
Every split in every tree reduces impurity (Gini or entropy for classification, variance for regression). The impurity-based importance of a feature is the total weighted impurity decrease from all splits that use it. It is averaged over the trees and scaled so the importances sum to 1. In scikit-learn this is feature_importances_, and it is free because it is computed during training.
It has three weaknesses:
- It is computed on the training data. A feature the trees used to memorize noise can still score high.
- It favors features with many possible split points, such as continuous or high-cardinality columns. A random ID column can look important.
- Correlated features split the credit between them, so each one looks less important than the pair really is.
Permutation importance
Permutation importance asks a different question: how much does the model's score drop on held-out data when one feature's values are shuffled? Shuffling breaks the link between that feature and the target and leaves the other columns unchanged.
It works with any fitted model and any metric, and it measures what the model relies on for prediction, not what it used while fitting. The cost is extra prediction passes: n_repeats times the number of features.
Comparing the two
The code below prints the top five features by impurity importance next to their permutation importance on the test set.
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import RandomForestClassifier
from sklearn.inspection import permutation_importance
from sklearn.model_selection import train_test_split
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(
data.data, data.target, test_size=0.2, random_state=0, stratify=data.target
)
clf = RandomForestClassifier(n_estimators=300, random_state=0, n_jobs=-1)
clf.fit(X_train, y_train)
impurity = clf.feature_importances_
perm = permutation_importance(
clf, X_test, y_test, n_repeats=10, random_state=0
).importances_mean
for i in impurity.argsort()[::-1][:5]:
print(f"{data.feature_names[i]:<25} impurity={impurity[i]:.3f} permutation={perm[i]:.3f}")Read the two columns differently. Impurity values sum to 1 across all features, so they are shares. Permutation values are score drops, so they are not shares. They can be near zero or slightly negative for features that do not help, and they do not have to sum to anything. The breast cancer data has many strongly correlated measurements, such as radius, perimeter and area. Expect the rankings to disagree and the permutation values to look small, because when one of a correlated pair is shuffled the other still carries the signal.
Common errors
- Treating importance as causal. Both measures describe the model, not the world. A high value means the model uses the feature, not that changing it changes the outcome.
- Computing permutation importance on training data. An overfit forest scores almost perfectly on its own training rows, so the result reflects memorization. Use a validation or test set.
- Ignoring correlated features. If two features carry the same signal, shuffling one costs little. Either group correlated columns and shuffle them together, or drop one of each pair and recompute.
- Reading a single run. Permutation importance is random. Use
n_repeatsand look atimportances_stdas well as the mean. - Over-reading small differences. Features ranked close together are usually indistinguishable. Check the spread before ordering them.
Exercise
A forest's impurity importance ranks a randomly generated ID column among the top five features, but its permutation importance on the test set is close to zero. Why do the two measures disagree, and which should you trust?
Show answer
Impurity importance is computed on the training data and favors columns with many unique values, which offer many candidate split points. The trees split on the ID column to memorize training rows, which lowers training impurity without generalizing. Permutation importance shuffles the column on held-out data and finds that the score does not change, so the model's predictions do not depend on it in a useful way. Trust the permutation result here.