Feature Importance & Its Pitfalls
Impurity, permutation and conditional importance.
Every forest library prints a ranked list of feature importances, and people publish them as findings. But impurity-based scores favor features with many possible split points, permutation scores make the model extrapolate when features are correlated, and a feature's importance can change completely between two equally accurate models. This lesson takes the main measures apart and pins down what each one actually measures.
What you’ll learn
- Define mean decrease in impurity (MDI), explain its bias toward features with many split points, and state its exact decomposition for totally randomized trees.
- Define permutation importance, compute it exactly for a linear model with correlated features, and contrast it with conditional permutation, drop-column and LOCO importance.
- Compute Shapley attributions for small trees, explain what TreeSHAP computes in polynomial time, and distinguish interventional from conditional attributions.
- Explain model class reliance and the Rashomon set, and why importance is a property of a model rather than of the data.
Mean decrease in impurity
The default feature importance of most tree libraries is the mean decrease in impurity (MDI, or Gini importance). Over all nodes that split on feature , add up the impurity decrease weighted by the fraction of training examples reaching the node, then average over the trees:
feature_importances_ normalizes these scores to sum to 1. MDI is computed on the very data the trees were grown on, and that is the root of its problems.Two biases follow. Many split points: a feature with many distinct values or many categories offers many candidate splits, so even when it is independent of , its best split finds some impurity decrease by chance (a multiple-comparisons effect), and deep trees keep splitting on it. Strobl, Boulesteix, Zeileis & Hothorn (2007) showed in simulations that uninformative continuous predictors and predictors with many categories received inflated importance, with permutation importance in bootstrap-based forests affected as well, and proposed forests of conditional inference trees grown on subsamples drawn without replacement. In-sample evaluation: impurity decreases measured on the training data reward splits that merely fit noise, which fully grown trees make constantly. Computing the decreases on out-of-bag or held-out data targets this second bias directly.
By the chain rule of mutual information, each term is a marginal contribution: adding to the set raises the information about by
The weights are exactly Shapley weights, because :
So is the Shapley value of feature in the cooperative game whose value for a coalition is , and the sum rule is the efficiency property of Shapley values. Sutera, Louppe, Huynh-Thu, Wehenkel & Geurts (2021) make this correspondence explicit and derive local, per-prediction MDI importances from it.
Example: two fair bits and . Each feature alone carries no information about , but given the other it carries one bit, so each gets half a bit of importance, and the total is bit:
In the totally randomized, infinite-sample setting above, you add a feature with 1,000 categories that is independent of . What happens?
- gets positive importance, because it offers many split points
- gets zero importance, and the importances of and are unchanged
- gets zero importance, but and lose some of theirs to it
- It depends on how many trees are grown
Show the answer
B. gets zero importance, and the importances of and are unchanged
for every , so is irrelevant and its importance is exactly zero, and the theorem says relevant features keep their importance. The many-categories bias is a finite-sample effect: with real data and greedy splits, would usually get positive MDI.
Permutation importance and correlated features
Breiman (2001) proposed a different measure: shuffle feature 's column in the out-of-bag data, which breaks its association with and with every other feature, and record how much the error rises. In population form, with an independent copy of substituted into :
sklearn.inspection.permutation_importance computes it on any held-out set.Let , with standardized, correlation , and independent noise. Consider fitted models with , under squared error. Replacing by changes the prediction by ; since is independent with mean 0, the cross term reduces to :
With this simplifies, and the model's excess error over the true model is explicit too:
Take . The true model () gives each feature importance 2. The model is only 0.02 worse in mean squared error, yet gives importance and importance 0; does the reverse.
Permutation importance measures how much this model relies on a feature, not how much the feature matters for . And with correlated features, the shuffled points fall where the data never are, since and almost always agree, so the score reports the model's behavior far from its training data. Hooker, Mentch & Zhou (2021) show that this extrapolation can badly distort importances for flexible models such as forests and networks.
In the same setting with , the fitted model is . What is the permutation importance of ?
Show the answer
4.49
, while gets about 0.51. The model's excess error is only : nearly as good as the true model, with very different importances.
Several measures avoid extrapolation. Conditional permutation importance (Strobl, Boulesteix, Kneib, Augustin & Zeileis, 2008) permutes only within groups of examples with similar values of the features correlated with it, using a grid built from the forest's own splits, which approximates drawing from and keeps the points near the data. Drop-column importance refits the model without and measures the loss increase; LOCO, leave-one-covariate-out (Lei, G'Sell, Rinaldo, Tibshirani & Wasserman, 2018), does the same with distribution-free inference. Both refit-based measures answer "what is lost if is unavailable?", which is zero for a feature that another one duplicates, however relevant both are.
| Measure | Question it answers | Main pitfalls |
|---|---|---|
| MDI (Gini importance) | how much splits on reduced training impurity in the fitted trees | biased toward many split points; in-sample; unstable |
| Permutation | how much the model's loss rises when 's links to everything are broken | extrapolation under correlation; specific to one model; noisy |
| Conditional permutation | whether helps the model given the features correlated with it | needs a good conditional sampler; duplicated features lose credit |
| Drop-column or LOCO | what is lost if were unavailable, after refitting | one refit per feature; correlated features can both score near zero |
| Mean absolute SHAP | how large 's share of individual predictions is, on average | depends on the value function and background data; not a measure of predictive gain |
TreeSHAP: Shapley values for trees
For a single prediction, a Shapley attribution (Lundberg & Lee, 2017) splits among the features, using a value function for what the model predicts when only the features in are known:
Let be independent, each positive with probability , and , a depth-2 tree. At with both coordinates positive, and , and knowing one positive coordinate makes the prediction :
The attributions sum to . Now make the features identical, , and let the model read only the first, . Interventional values give nothing, since the model never reads it; conditional values split the credit, since knowing reveals :
Neither is wrong. Interventional attributions are true to the model: a feature the model never reads gets zero. Conditional attributions are true to the data: they credit features for the information they carry, so an explanation can name a feature the model does not use. Choose deliberately, and say which one you report.
Exact Shapley values cost time exponential in for a general model. For tree ensembles, Lundberg, Erion & Lee (2018) found an exact algorithm that tracks, along each root-to-leaf path, the proportion of feature subsets that would reach the leaf, reducing the cost from to for trees with at most leaves and depth . This "path-dependent" TreeSHAP stands in for the conditional value function by sending an unknown feature down both branches, weighted by the fractions of training examples that went each way, which matches neither definition exactly in general. An interventional variant averages over a background dataset, at a cost linear in its size. Lundberg et al. (2020) built TreeExplainer on these algorithms, adding SHAP interaction values and global summaries such as the mean over a dataset (Interpretability: SHAP, LIME & Their Limits covers the general theory).
Importance of what? Model reliance and the Rashomon set
The linear example gave three nearly equally accurate models whose permutation importances for were 0, 2 and about 8. This is the general situation. Fisher, Rudin & Dominici (2019) define model class reliance (MCR): the range of a feature's model reliance over the Rashomon set, all models in a class whose expected loss is within of a reference model:
In the linear family above, with and tolerance in mean squared error, every split of weight between and is in the Rashomon set, and 's permutation importance ranges over . The data can say that and together matter, not how to divide the credit between them. Xin et al. (2022) compute such ranges over the entire Rashomon set of sparse decision trees (Optimal & Interpretable Trees).
A defensible protocol:
- Compute importances on held-out data, repeat the permutations or refits, and report the spread.
- Prefer permutation, refit-based or SHAP-based summaries to in-sample MDI.
- Group strongly correlated features and report group importance, or use conditional or refit-based measures, and say which question you are answering.
- Check stability across random seeds, folds and model families, and across the Rashomon set when you can.
- Never read an importance as a causal effect.
Key takeaways
- MDI sums weighted impurity decreases on the training data; it favors features with many split points and splits that fit noise. For totally randomized trees in the infinite-sample limit it equals , a Shapley value of that is zero exactly for irrelevant features.
- Permutation importance measures a model's reliance on a feature. For with and correlation , , ranging from 0 to across nearly equally accurate models; with correlated features it also forces extrapolation.
- Conditional permutation, drop-column and LOCO answer different questions; refit-based measures give duplicated features near-zero importance, so group correlated features.
- TreeSHAP computes exact Shapley attributions for trees in . Interventional attributions are true to the model, conditional ones true to the data, and they differ when features are dependent.
- Model class reliance reports the range of importance across the Rashomon set: importance is a property of a model, and never by itself a causal effect.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A deep random forest gives the highest MDI to a customer-ID feature that is unrelated to the target. Why?
- The ID must secretly encode the label
- The ID offers a huge number of candidate splits, and impurity decreases are measured on the training data, so splits that fit noise get credited
- MDI is normalized to sum to 1, which inflates small scores
- Random forests always rank the last column highest
Show the answer
B. The ID offers a huge number of candidate splits, and impurity decreases are measured on the training data, so splits that fit noise get credited
With many split points, the best split of a noise feature still lowers training impurity by chance, and fully grown trees keep making such splits. Held-out permutation importance would put the ID near zero, unless it really leaks information about the label.
Two models fit the data almost equally well. In one, a feature has permutation importance near 8; in the other, near 0, because a highly correlated is used instead. What should you conclude?
- One of the models must be overfitting
- is causally important, because one good model relies on it
- Permutation importance is broken and should never be used
- Importance is a property of each model; the data support many models that divide the credit differently, so report the pair's joint importance or the range across good models
Show the answer
D. Importance is a property of each model; the data support many models that divide the credit differently, so report the pair's joint importance or the range across good models
Permutation importance measures a model's reliance on a feature. With strongly correlated features, the Rashomon set contains models that rely on either, so a single model's score says little about the data; model class reliance reports the range.
Feature is an exact copy of , and both are strongly predictive. What do drop-column (refit) importances give?
- About zero for both, because the model refit without one can use the other
- Large values for both, because both are predictive
- A large value for whichever was listed first
- Half of the joint importance each
Show the answer
A. About zero for both, because the model refit without one can use the other
Removing loses nothing while is available, and vice versa, so each refit-based importance is near zero. Grouped importance, dropping both together, reveals that the pair matters.
A model reads only , and is perfectly correlated with . How do interventional and conditional Shapley values treat ?
- Both give zero, because the model does not use it
- Interventional gives zero; conditional gives it a positive share, because knowing reveals
- Both split the credit equally between and
- Interventional gives a positive share; conditional gives it zero
Show the answer
B. Interventional gives zero; conditional gives it a positive share, because knowing reveals
Interventional value functions fill in from its marginal distribution when only is known, so changes nothing and . Conditional ones compute , which equals here, so gets credit: in the example, against .
Further reading
- C. Strobl, A.-L. Boulesteix, A. Zeileis & T. Hothorn, "Bias in random forest variable importance measures: illustrations, sources and a solution," BMC Bioinformatics 8, 25 (2007).
- G. Louppe, L. Wehenkel, A. Sutera & P. Geurts, "Understanding variable importances in forests of randomized trees," Advances in Neural Information Processing Systems 26 (2013).
- G. Hooker, L. Mentch & S. Zhou, "Unrestricted permutation forces extrapolation: variable importance requires at least one more model, or there is no free variable importance," Statistics and Computing 31(6) (2021).
- S. M. Lundberg, G. Erion, H. Chen, A. DeGrave, J. M. Prutkin, B. Nair, R. Katz, J. Himmelfarb, N. Bansal & S.-I. Lee, "From local explanations to global understanding with explainable AI for trees," Nature Machine Intelligence 2, 56–67 (2020).
- A. Fisher, C. Rudin & F. Dominici, "All models are wrong, but many are useful: learning a variable's importance by studying an entire class of prediction models simultaneously," Journal of Machine Learning Research 20(177), 1–81 (2019).