Feature importance
Which inputs a model relies on: built-in importance versus permutation importance, and how correlated features distort both.
Lesson 56 of 6610 minIntermediate
You will learn to
- Compare built-in (impurity) and permutation importance
- Spot importance artefacts caused by correlated features
- Explain importance to a stakeholder without overclaiming
Before this lesson
Intuition
You hire a new analyst who predicts well, and you want to know what she looks at. There are two ways to find out. You can ask her, which tells you what she says she uses. Or you can take away one input at a time and see how much her predictions suffer. The first is how built-in importance works. The second is permutation importance.
Built-in importance comes from the inside of the model. A random forest counts how much each feature reduces impurity (the mixing of classes in a node) across all its splits. A linear model uses the size of its coefficients. It is free, because it is a by-product of training. But it is a statement about how the training went, not about how useful the feature is on new data.
Permutation importance treats the model as a black box. Shuffle the values of one column in held-out data, which breaks the link between that feature and the target, and score the model again. If the score falls a lot, the model needed that column. If it barely moves, the model did not rely on it. It works for any model and measures the effect on real held-out performance, at the cost of extra scoring passes.
Picture it
The same model, seen two ways. Size and Size copy carry the same information. Built-in importance gives each a large share. Permutation importance gives each a small score, because shuffling one leaves the other to cover for it, while a feature with unique information, Age, stands out. Noise gets a share of built-in importance even though shuffling it costs nothing.
The math
Let be the model's score on a held-out set (higher is better, such as accuracy), and the score after permuting feature in repetition . With repetitions the permutation importance of feature is
and the spread across repetitions, , tells you how stable it is. For an error metric where lower is better, flip the sign so a larger still means more important.
Built-in importance of a tree ensemble is the total impurity decrease contributed by feature , averaged over trees and normalised to sum to 1:
For a linear model on scaled features, the absolute value of the coefficient plays the same role. The scale matters: coefficients are only comparable when the features have comparable scales.
Worked example
A classifier scores 0.90 accuracy on the held-out set. You shuffle each feature in turn (illustrative numbers, matching the picture):
| Feature | Accuracy after shuffling | Permutation importance | Built-in importance |
|---|---|---|---|
| Age | 0.60 | 0.90 - 0.60 = 0.30 | 0.32 |
| Size | 0.85 | 0.05 | 0.30 |
| Size copy | 0.86 | 0.04 | 0.28 |
| Noise | 0.90 | 0.00 | 0.10 |
Read the table in three steps.
- Age is clearly used: removing its information costs 30 points of accuracy.
- Size and Size copy look weak under permutation (0.05 and 0.04). That is the artefact: they carry the same information, so each hides the other. Together they are important. A check is to drop one copy, retrain, and see that the score barely changes.
- Noise receives 0.10 of the built-in importance but 0.00 under permutation. Impurity-based importance is computed on the training data, where an unhelpful feature can still be used to fit noise. Permutation on held-out data shows it does no good.
A sentence you can say to a stakeholder: "The model mainly relies on age and on the size information. Shuffling age costs 30 accuracy points. This tells us what the model uses, not what causes the outcome."
How mlflow does it
- Where. Open a run, open a model candidate and go to the Plots tab. For classification and regression the Explainability group has Feature importance and Permutation importance.
- Feature importance reads the model's own importances, or the absolute coefficients for linear models. If the model has neither (for example a nearest-neighbour model), the plot cannot be drawn; use permutation importance instead. If preprocessing changes the number of columns, for example by one-hot encoding, bars may be labelled with positions such as feature_0 instead of column names.
- Permutation importance shuffles the columns of the held-out test data and shows the mean drop in score with error bars across 5 repetitions.
- Time series and clustering have no importance plots.
- The docs page on improving a model shows how to use these plots to decide what to change next.
Common mistakes
- Trusting built-in importance of a random forest on its own. It favours features with many distinct values and is computed on training data. Cross-check with permutation importance.
- Reading correlated features one by one. Importance is split or hidden between them. Group related features, or drop one and compare.
- Computing permutation importance on training data. An overfitted model looks like it relies on features that do not generalise. Use held-out data.
- Confusing importance with causation. A top feature may be a proxy for the real cause, or a leak. Ask whether the feature will be available and meaningful at prediction time.
- Reading small differences. With error bars of similar size, two bars of similar length are tied. Look at the spread across repetitions before ranking.
Try it in mlflow
Open the feature importance plot
- Open Runs, choose a classification or regression run and open a model candidate.
- Open the Plots tab and the Explainability group.
- Open Feature importance, then Permutation importance, and compare the order of the bars.
- Note any feature that ranks high in one plot and low in the other.