Explain the Model in Plain Words
A trained model gives good predictions, but sooner or later someone asks what it bases them on. This page shows a simple way to measure which features a model relies on, how to tell a real result from luck, how to say it in plain words, and where such an explanation stops: it describes the model, not the world the data came from.
Break one column and watch the score
Take a model that is already trained, and a set of held-out rows it never learned from, such as validation rows. Score the model on them as usual, for example with accuracy, the share of predictions that are right. That score is the reference.
Now pick one feature, say the months a customer has stayed, and shuffle its values: the same numbers are dealt out again to the rows in a random order. Every other column stays exactly as it was. The column still looks normal, with the same values and the same average, but each value now sits next to the wrong customer, so it says nothing true about that row any more.
| Months | Contract | Monthly charge |
|---|---|---|
| 2 | monthly | 85 |
| 60 | two years | 40 |
| 5 | monthly | 95 |
| 48 | one year | 70 |
| 30 | one year | 55 |
| Months (shuffled) | Contract | Monthly charge |
|---|---|---|
| 48 | monthly | 85 |
| 5 | two years | 40 |
| 30 | monthly | 95 |
| 2 | one year | 70 |
| 60 | one year | 55 |
Score the same model again on the shuffled rows. If the model leaned on that feature, its predictions now go wrong more often and the score falls. If it hardly used the feature, the predictions barely change and neither does the score. The drop in score is the feature's permutation importance (to permute means to reorder). Repeat it for each feature in turn, putting the column back each time, and you get one number per feature.
For one feature
A few things follow from how it works:
- Nothing is retrained. The same fitted model is only scored again, so the method works for any model: a tree, a logistic regression or something much harder to read.
- Held-out rows, not training rows. On its training rows a model can rely on details that only help it remember those rows. On held-out rows the drop shows what helps it predict new ones.
- It is one number with no direction. A drop says how much the model relies on the feature, not whether more months make leaving more or less likely.
- It depends on the score. The drop is measured in whatever score you chose. With accuracy it is a drop in accuracy; with recall it can rank the features differently, above all when one answer is rare.
True or false?
Permutation importance trains the model again without the feature and compares the two scores.
False. The fitted model is never retrained. Only the data is changed: one column is shuffled and the same model is scored again.
A shuffled column keeps the same values and the same average as before.
True. Shuffling only reorders the values. What is lost is the link between each value and its own row, which is exactly what the model used.
A large importance for the months feature tells you that customers with more months are less likely to leave.
False. The importance is a drop in score, a single number. It says the model relies on the feature, not in which direction the feature pushes a prediction.
Shuffle several times and read the spread
A shuffle is random, and so is its result. One shuffle may happen to leave several values close to where they were, another may scramble them thoroughly, and on a few hundred held-out rows a handful of predictions turning right or wrong moves the score. A single drop can therefore be partly luck.
The answer is to shuffle the same feature several times, each time in a new random order, and look at all the drops together. Their average is the importance. Their spread shows how much of it could be luck: it measures how far the drops lie from that average, and is usually given as the standard deviation. With a fixed random seed, the shuffles come out the same on every run, and so does every number measured from them.
- Feature A
- Feature B
- Feature C
- Feature D
Reading it is a matter of comparing each average with its own spread. Feature A loses about 0.06 of accuracy every time, far more than the drops vary, so the model clearly relies on it. Feature B loses less, about 0.02, but every shuffle agrees, so that is real too. Feature C averages a tiny drop that is no larger than its spread: some shuffles even raised the score. That is no evidence the model uses it at all. Feature D, with a slightly negative average, is the same case: a shuffle that happens to score a little higher is luck, not a sign that the feature harms the model.
A simple reading rule follows: a drop no larger than its spread is no evidence. Some people are stricter and ask for an average more than two spreads above zero, which is a reasonable habit. Neither rule is a formal statistical test. Both just keep you from reading noise as a finding.
In scikit-learn the function permutation_importance does the shuffling and scoring. You pass it a fitted model and held-out rows; n_repeats sets how many shuffles per feature (5 if you leave it out), and random_state fixes the seed (without it, every run shuffles differently). It returns the average drop and the spread for each column of the table you passed, in the same order as its columns.
from sklearn.inspection import permutation_importance
result = permutation_importance(
model, X_val, y_val, n_repeats=10, random_state=0
)
order = result.importances_mean.argsort()[::-1]
for i in order:
name = X_val.columns[i]
mean = result.importances_mean[i]
spread = result.importances_std[i]
print(f"{name:<20} {mean:.3f} +/- {spread:.3f}")Here model is any fitted model, and X_val and y_val are the held-out features and answers. With no scoring given, a classifier is scored by accuracy. argsort()[::-1] lists the column positions from the largest average drop to the smallest, and each line prints a feature's name, its average drop and its spread. When the model is a pipeline that prepares the columns itself, the shuffling happens on the original columns, before any preparation. A text column that one-hot encoding turns into several 0 or 1 columns is therefore shuffled as one feature and gets one importance.
True or false?
An average drop of 0.004 with a spread of 0.005 is evidence that the model relies a little on that feature.
False. The drop is no larger than its spread, so the shuffles disagree about whether there is any drop at all. That is no evidence either way.
A small negative importance means shuffling happened to score slightly higher, which is luck rather than harm.
True. The importance is the reference score minus the shuffled score, so any shuffle that scores a little higher gives a negative value. Read it like a drop inside its spread.
Without a fixed seed, running the same importance code twice can print different numbers.
True. Each run would shuffle in different random orders. A fixed seed makes the shuffles, and so the printout, repeat exactly.
What the model uses, not what causes the outcome
Every importance belongs to one model, measured on one set of rows with one score. Train a different kind of model, or a worse one, and the ranking can change: a feature that matters little to a weak model can matter a great deal to a good one. That is why the model's score is checked on held-out rows first. Explaining a model that predicts poorly explains very little.
Even for a good model, an importance answers only one question: what does this model look at to make its predictions? It does not say what makes the outcome happen, and it does not say what would change it. A model finds patterns that go together in past rows, and things can go together without one causing the other.
Here is why the step from "the model relies on it" to "it causes leaving" fails. Suppose customers who have just moved house are both more likely to pick a short contract and more likely to leave, and the table has no column for moving. The model will lean on the contract type, because in the past rows it went together with leaving. Yet putting those customers on a longer contract would not stop them from moving away. Something that is not in the table and affects both a feature and the outcome is called a confounding variable, and a prediction model cannot see past one.
A trustworthy explanation in plain words therefore does four things:
- it names the model, the rows and the score;
- it gives the size of the drop beside its spread;
- it mentions features that could stand in for each other;
- it stops at what the model uses.
Measuring what would change the outcome is a separate question for a separate method, usually an experiment where some customers get the change at random and others do not.
True or false?
If a feature has the largest importance, changing that feature for a customer is the best way to change what happens to them.
False. The importance describes what the model uses to predict. Whether changing the feature changes the outcome is a question about cause, which this measurement cannot answer.
The same feature can rank high for one model and low for another model trained on the same rows.
True. An importance belongs to one fitted model, one set of rows and one score, not to the feature itself.
A model can predict well while leaning on a feature that only goes together with the outcome without causing it.
True. Predicting needs only a pattern that holds in the data. A feature linked to the outcome through a confounding variable predicts just as well as a cause would.