Simple Algorithm
Machine Learning · Convergence · 26 min

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 tt that split on feature jj, add up the impurity decrease weighted by the fraction p(t)=nt/np(t)=n_t/n of training examples reaching the node, then average over the BB trees:

MDI(j)=1B∑b=1B ∑t∈Tb: v(t)=jp(t) Δi(t)\mathrm{MDI}(j)=\frac1B\sum_{b=1}^B\ \sum_{t\in T_b:\ v(t)=j}p(t)\,\Delta i(t)
v(t)v(t) is the feature used at node tt. scikit-learn's 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 yy, 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.

DerivationThe decomposition is a Shapley value
  1. I(XB∪{j};Y)−I(XB;Y)=I(Xj;Y∣XB)I(X_{B\cup\{j\}};Y)-I(X_B;Y)=I(X_j;Y\mid X_B)

    By the chain rule of mutual information, each term is a marginal contribution: adding XjX_j to the set BB raises the information about YY by

  2. 1(pk)(p−k)=k! (p−k−1)!p!\frac1{\binom pk(p-k)}=\frac{k!\,(p-k-1)!}{p!}

    The weights are exactly Shapley weights, because (pk)(p−k)=p!k! (p−k−1)!\binom pk(p-k)=\frac{p!}{k!\,(p-k-1)!}:

  3. Imp(Xj)=ϕj(v),∑jϕj(v)=v(V)−v(∅)=I(X1,…,Xp;Y)\mathrm{Imp}(X_j)=\phi_j(v),\qquad\sum_j\phi_j(v)=v(V)-v(\varnothing)=I(X_1,\dots,X_p;Y)

    So Imp(Xj)\mathrm{Imp}(X_j) is the Shapley value of feature jj in the cooperative game whose value for a coalition BB is v(B)=I(XB;Y)v(B)=I(X_B;Y), 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.

  4. Imp(X1)=12[I(X1;Y)+I(X1;Y∣X2)]=12 [0+1]=0.5 bit\mathrm{Imp}(X_1)=\tfrac12\big[I(X_1;Y)+I(X_1;Y\mid X_2)\big]=\tfrac12\,[0+1]=0.5\text{ bit}

    Example: two fair bits and Y=X1⊕X2Y=X_1\oplus X_2. Each feature alone carries no information about YY, but given the other it carries one bit, so each gets half a bit of importance, and the total is H(Y)=1H(Y)=1 bit:

Check your understanding

In the totally randomized, infinite-sample setting above, you add a feature X3X_3 with 1,000 categories that is independent of (X1,X2,Y)(X_1,X_2,Y). What happens?

  1. X3X_3 gets positive importance, because it offers many split points
  2. X3X_3 gets zero importance, and the importances of X1X_1 and X2X_2 are unchanged
  3. X3X_3 gets zero importance, but X1X_1 and X2X_2 lose some of theirs to it
  4. It depends on how many trees are grown
Show the answer

B. X3X_3 gets zero importance, and the importances of X1X_1 and X2X_2 are unchanged

I(X3;Y∣B)=0I(X_3;Y\mid B)=0 for every BB, so X3X_3 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, X3X_3 would usually get positive MDI.

Permutation importance and correlated features

Breiman (2001) proposed a different measure: shuffle feature jj's column in the out-of-bag data, which breaks its association with yy and with every other feature, and record how much the error rises. In population form, with Xj′X_j' an independent copy of XjX_j substituted into X\mathbf X:

PIj=E[L(Y,f(X~(j)))]−E[L(Y,f(X))],X~(j)=(X1,…,Xj′,…,Xp)\mathrm{PI}_j=\E\big[L\big(Y,f(\tilde{\mathbf X}^{(j)})\big)\big]-\E\big[L\big(Y,f(\mathbf X)\big)\big],\qquad\tilde{\mathbf X}^{(j)}=(X_1,\dots,X_j',\dots,X_p)
Permutation importance needs only predictions, so it applies to any model; Fisher, Rudin & Dominici (2019) call the ratio of the two expected losses model reliance. sklearn.inspection.permutation_importance computes it on any held-out set.
DerivationA linear model, exactly
  1. PI1=a2 E(X1′−X1)2−2a E[X1(f(X)−Y)]=2a2−2a[(a−1)+(b−1)ρ]\mathrm{PI}_1=a^2\,\E(X_1'-X_1)^2-2a\,\E\big[X_1(f(\mathbf X)-Y)\big]=2a^2-2a\big[(a-1)+(b-1)\rho\big]

    Let Y=X1+X2+εY=X_1+X_2+\varepsilon, with X1,X2X_1,X_2 standardized, correlation ρ\rho, and independent noise. Consider fitted models f=aX1+bX2f=aX_1+bX_2 with a+b=2a+b=2, under squared error. Replacing X1X_1 by X1′X_1' changes the prediction by a(X1′−X1)a(X_1'-X_1); since X1′X_1' is independent with mean 0, the cross term reduces to −2a E[X1(f(X)−Y)]-2a\,\E[X_1(f(\mathbf X)-Y)]:

  2. PI1=2a(1−(1−a)ρ),E(f(X)−Y)2−Var⁡ε=2(a−1)2(1−ρ)\mathrm{PI}_1=2a\big(1-(1-a)\rho\big),\qquad\E\big(f(\mathbf X)-Y\big)^2-\Var\varepsilon=2(a-1)^2(1-\rho)

    With b=2−ab=2-a this simplifies, and the model's excess error over the true model is explicit too:

  3. (a,b)=(1,1): (2, 2)(2,0): (7.96, 0)(0,2): (0, 7.96)(a,b)=(1,1):\ (2,\,2)\qquad(2,0):\ (7.96,\,0)\qquad(0,2):\ (0,\,7.96)

    Take ρ=0.99\rho=0.99. The true model (a=b=1a=b=1) gives each feature importance 2. The model f=2X1f=2X_1 is only 0.02 worse in mean squared error, yet gives X1X_1 importance 4(1+ρ)=7.964(1+\rho)=7.96 and X2X_2 importance 0; f=2X2f=2X_2 does the reverse.

  4. Pr⁡(∣X1′−X2∣ large)≫Pr⁡(∣X1−X2∣ large)when ρ≈1\Pr\big(\abs{X_1'-X_2}\text{ large}\big)\gg\Pr\big(\abs{X_1-X_2}\text{ large}\big)\quad\text{when }\rho\approx1

    Permutation importance measures how much this model relies on a feature, not how much the feature matters for yy. And with correlated features, the shuffled points (X1′,X2)(X_1',X_2) fall where the data never are, since X1X_1 and X2X_2 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.

Predict before you look

In the same setting with ρ=0.99\rho=0.99, the fitted model is f=1.5X1+0.5X2f=1.5X_1+0.5X_2. What is the permutation importance of X1X_1?

Show the answer

4.49

2a(1−(1−a)ρ)=3×(1+0.5×0.99)=3×1.495≈4.492a\big(1-(1-a)\rho\big)=3\times(1+0.5\times0.99)=3\times1.495\approx4.49, while X2X_2 gets about 0.51. The model's excess error is only 2(0.5)2(0.01)=0.0052(0.5)^2(0.01)=0.005: 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 XjX_j 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 Xj∣X−jX_j\mid X_{-j} and keeps the points near the data. Drop-column importance refits the model without XjX_j 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 XjX_j is unavailable?", which is zero for a feature that another one duplicates, however relevant both are.

MeasureQuestion it answersMain pitfalls
MDI (Gini importance)how much splits on XjX_j reduced training impurity in the fitted treesbiased toward many split points; in-sample; unstable
Permutationhow much the model's loss rises when XjX_j's links to everything are brokenextrapolation under correlation; specific to one model; noisy
Conditional permutationwhether XjX_j helps the model given the features correlated with itneeds a good conditional sampler; duplicated features lose credit
Drop-column or LOCOwhat is lost if XjX_j were unavailable, after refittingone refit per feature; correlated features can both score near zero
Mean absolute SHAPhow large XjX_j's share of individual predictions is, on averagedepends on the value function and background data; not a measure of predictive gain
Five measures, five different questions. Most disagreements between them are disagreements about the question.

TreeSHAP: Shapley values for trees

For a single prediction, a Shapley attribution (Lundberg & Lee, 2017) splits f(x)−Ef(X)f(\mathbf x)-\E f(\mathbf X) among the features, using a value function vx(S)v_{\mathbf x}(S) for what the model predicts when only the features in SS are known:

ϕj(x)=∑S⊆F∖{j}∣S∣! (p−∣S∣−1)!p![vx(S∪{j})−vx(S)],∑j=1pϕj(x)=f(x)−Ef(X)\phi_j(\mathbf x)=\sum_{S\subseteq F\setminus\{j\}}\frac{\abs S!\,(p-\abs S-1)!}{p!}\Big[v_{\mathbf x}(S\cup\{j\})-v_{\mathbf x}(S)\Big],\qquad\sum_{j=1}^p\phi_j(\mathbf x)=f(\mathbf x)-\E f(\mathbf X)
Two value functions dominate. Interventional: vx(S)=E[f(xS,XSˉ)]v_{\mathbf x}(S)=\E\big[f(\mathbf x_S,\mathbf X_{\bar S})\big] fills in the unknown features from their marginal distribution. Conditional: vx(S)=E[f(X)∣XS=xS]v_{\mathbf x}(S)=\E\big[f(\mathbf X)\mid\mathbf X_S=\mathbf x_S\big]. They coincide when the features are independent.
DerivationTwo tiny examples
  1. v(∅)=14,v({1})=v({2})=12,v({1,2})=1⟹ϕ1=12⋅14+12⋅12=38=ϕ2v(\varnothing)=\tfrac14,\quad v(\{1\})=v(\{2\})=\tfrac12,\quad v(\{1,2\})=1\quad\Longrightarrow\quad\phi_1=\tfrac12\cdot\tfrac14+\tfrac12\cdot\tfrac12=\tfrac38=\phi_2

    Let X1,X2X_1,X_2 be independent, each positive with probability 1/21/2, and f(x)=1{x1>0} 1{x2>0}f(\mathbf x)=\mathbf 1\{x_1>0\}\,\mathbf 1\{x_2>0\}, a depth-2 tree. At x\mathbf x with both coordinates positive, f(x)=1f(\mathbf x)=1 and Ef=1/4\E f=1/4, and knowing one positive coordinate makes the prediction 1/21/2:

  2. interventional: (ϕ1,ϕ2)=(12, 0)conditional: (ϕ1,ϕ2)=(14, 14)\text{interventional: }(\phi_1,\phi_2)=\big(\tfrac12,\,0\big)\qquad\text{conditional: }(\phi_1,\phi_2)=\big(\tfrac14,\,\tfrac14\big)

    The attributions sum to 3/4=f(x)−Ef3/4=f(\mathbf x)-\E f. Now make the features identical, X2=X1X_2=X_1, and let the model read only the first, f(x)=1{x1>0}f(\mathbf x)=\mathbf 1\{x_1>0\}. Interventional values give X2X_2 nothing, since the model never reads it; conditional values split the credit, since knowing x2x_2 reveals x1x_1:

  3. 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 pp 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 O(TL2p)\order{TL2^p} to O(TLD2)\order{TLD^2} for TT trees with at most LL leaves and depth DD. 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 ∣ϕj∣\abs{\phi_j} 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 X1X_1 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 MRj\mathrm{MR}_j over the Rashomon set, all models in a class whose expected loss is within ϵ\epsilon of a reference model:

MCR−(ϵ)=min⁡f∈R(ϵ)MRj(f),MCR+(ϵ)=max⁡f∈R(ϵ)MRj(f),R(ϵ)={f∈F:EL(f)≤EL(fref)+ϵ}\mathrm{MCR}_-(\epsilon)=\min_{f\in\mathcal R(\epsilon)}\mathrm{MR}_j(f),\qquad\mathrm{MCR}_+(\epsilon)=\max_{f\in\mathcal R(\epsilon)}\mathrm{MR}_j(f),\qquad\mathcal R(\epsilon)=\Big\{f\in\mathcal F:\E L(f)\le\E L(f_{\text{ref}})+\epsilon\Big\}
A small MCR+\mathrm{MCR}_+ means no well-performing model relies much on XjX_j; a large MCR−\mathrm{MCR}_- means every one does. Fisher et al. give finite-sample bounds for both, and apply MCR to recidivism prediction to ask how much any well-performing model could rely on race or sex.

In the linear family above, with ρ=0.99\rho=0.99 and tolerance 0.020.02 in mean squared error, every split of weight a∈[0,2]a\in[0,2] between X1X_1 and X2X_2 is in the Rashomon set, and X1X_1's permutation importance ranges over [0, 7.96][0,\,7.96]. The data can say that X1X_1 and X2X_2 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:

  1. Compute importances on held-out data, repeat the permutations or refits, and report the spread.
  2. Prefer permutation, refit-based or SHAP-based summaries to in-sample MDI.
  3. Group strongly correlated features and report group importance, or use conditional or refit-based measures, and say which question you are answering.
  4. Check stability across random seeds, folds and model families, and across the Rashomon set when you can.
  5. 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 ∑k1(pk)(p−k)∑∣B∣=kI(Xj;Y∣B)\sum_k\frac1{\binom pk(p-k)}\sum_{\abs B=k}I(X_j;Y\mid B), a Shapley value of v(B)=I(XB;Y)v(B)=I(X_B;Y) that is zero exactly for irrelevant features.
  • Permutation importance measures a model's reliance on a feature. For f=aX1+bX2f=aX_1+bX_2 with a+b=2a+b=2 and correlation ρ\rho, PI1=2a(1−(1−a)ρ)\mathrm{PI}_1=2a(1-(1-a)\rho), ranging from 0 to 4(1+ρ)4(1+\rho) 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 O(TLD2)\order{TLD^2}. 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.

Question 1

A deep random forest gives the highest MDI to a customer-ID feature that is unrelated to the target. Why?

  1. The ID must secretly encode the label
  2. 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
  3. MDI is normalized to sum to 1, which inflates small scores
  4. 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.

Question 2

Two models fit the data almost equally well. In one, a feature X1X_1 has permutation importance near 8; in the other, near 0, because a highly correlated X2X_2 is used instead. What should you conclude?

  1. One of the models must be overfitting
  2. X1X_1 is causally important, because one good model relies on it
  3. Permutation importance is broken and should never be used
  4. 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.

Question 3

Feature X2X_2 is an exact copy of X1X_1, and both are strongly predictive. What do drop-column (refit) importances give?

  1. About zero for both, because the model refit without one can use the other
  2. Large values for both, because both are predictive
  3. A large value for whichever was listed first
  4. 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 X1X_1 loses nothing while X2X_2 is available, and vice versa, so each refit-based importance is near zero. Grouped importance, dropping both together, reveals that the pair matters.

Question 4

A model reads only X1X_1, and X2X_2 is perfectly correlated with X1X_1. How do interventional and conditional Shapley values treat X2X_2?

  1. Both give X2X_2 zero, because the model does not use it
  2. Interventional gives X2X_2 zero; conditional gives it a positive share, because knowing x2x_2 reveals x1x_1
  3. Both split the credit equally between X1X_1 and X2X_2
  4. Interventional gives X2X_2 a positive share; conditional gives it zero
Show the answer

B. Interventional gives X2X_2 zero; conditional gives it a positive share, because knowing x2x_2 reveals x1x_1

Interventional value functions fill in X1X_1 from its marginal distribution when only x2x_2 is known, so x2x_2 changes nothing and ϕ2=0\phi_2=0. Conditional ones compute E[f∣X2=x2]\E[f\mid X_2=x_2], which equals f(x)f(\mathbf x) here, so X2X_2 gets credit: in the example, (14,14)(\tfrac14,\tfrac14) against (12,0)(\tfrac12,0).

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).