الانتقال إلى المحتوى
Machine Learningتقييم الموديلات· 7 دقائق للقراءة

Feature Importance Can Mislead: Impurity vs Permutation Importance

feature_importances_ favors columns with many distinct values and is computed on training data. Permutation importance on held-out data is closer to the truth, until correlated features share the credit.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
  • scikit-learn 1.9
محتويات المقال

A churn team plots its random forest's feature importances for a planning meeting. Tenure in days tops the chart, then customer_id, then tenure in months and support calls; has_contract comes last. Someone suggests that older accounts, with lower IDs, might churn differently, and the contract offer the product team wanted drops down the roadmap. Both conclusions come from the chart, and both are wrong: the ID carries no information at all, and the contract flag is one of the two features the model leans on most for new customers.

The common misconception

Many developers read feature_importances_ as a ranking of what truly matters for the target. It's an easy belief to hold:

  • It comes free with every fitted forest, sums to 1 and sorts into a clean bar chart.
  • It's computed from the model's own splits, so it feels like an inside view of how the model decides.
  • On well-behaved data it often agrees with intuition, which builds trust in the cases where it shouldn't.

The half-truth: it does describe the trained forest, which columns the trees split on and how much each split reduced impurity on the training rows. But that's a record of fitting, including overfitting, not a measure of predictive value on data the model hasn't seen.

What actually happens

There are two common ways to score a feature, and they answer different questions.

Impurity importance counts splits on training rows

The built-in score, (MDI), adds up how much each feature's splits reduced impurity across all trees. scikit-learn's permutation importance example gives two reasons it misleads: it's biased toward high-cardinality features, and it's computed on training set statistics, so it doesn't reflect what generalizes. A column with 1,500 distinct values offers up to 1,499 candidate split points; a binary column offers one. Each split is chosen to look best on the training rows, so the column with more chances finds splits that fit noise, especially deep in the tree where nodes are small. Strobl and colleagues traced the bias in random forests to this biased split selection, plus effects of bootstrap sampling.

Permutation importance measures lost accuracy on new rows

shuffles one column at a time and records how much the model's score drops. Computed on a held-out set, it asks the question you actually have: how much does the model rely on this column for predictions that generalize? The script below builds churn data where only tenure, support calls and the contract flag matter, adds tenure_days as a near copy of tenure, and a customer_id with no signal.

Python
import numpy as npfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.model_selection import train_test_splitrng = np.random.default_rng(0)n = 2000months = rng.integers(1, 72, n)X = np.column_stack([months,                     months * 30 + rng.integers(0, 30, n),  # tenure in days                     rng.poisson(1.5, n),     # support calls                     rng.integers(0, 2, n),   # has a contract                     rng.permutation(n)])     # customer ID: no signalnames = ["tenure_months", "tenure_days", "support_calls", "has_contract",         "customer_id"]logit = 0.5 - 0.04 * X[:, 0] + 0.6 * X[:, 2] - 1.5 * X[:, 3]y = (rng.random(n) < 1 / (1 + np.exp(-logit))).astype(int)  # churnedX_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)rf = RandomForestClassifier(random_state=0).fit(X_tr, y_tr)for name, mdi in zip(names, rf.feature_importances_):    print(f"{name:14} {mdi:.3f}")# tenure_months  0.188# tenure_days    0.321# support_calls  0.122# has_contract   0.084# customer_id    0.285

The impurity ranking follows the number of distinct values almost exactly: tenure_days has 1,091 in the training rows, customer_id 1,500, tenure_months 71, support_calls 8 and has_contract 2. On held-out data the picture flips. Shuffling customer_id costs nothing, −0.015 ± 0.012, which is noise around zero, while support calls and the contract flag, last in the impurity chart, are the model's two most relied-on features, each worth 0.040 of accuracy.

The forest really does use the ID, though. It scores 1.000 on its training rows and 0.69 on the test rows, and on the training rows shuffling customer_id costs 0.184 of accuracy. The model memorized customers by ID. The permutation guide uses exactly this comparison: features that matter on the training set but not on held-out data may be causing the model to overfit.

Correlated features hide each other

Now look at tenure. It drives churn in this data, yet both tenure columns score near zero: +0.001 and +0.013. Shuffling one leaves the other intact, so the model barely notices. The guide warns about exactly this: when two features are correlated, permuting one leaves the information available through the other, which lowers the importance of both even if they matter. Drop tenure_days and refit, and tenure_months rises to +0.030. scikit-learn's multicollinear example shows the extreme case: on the breast cancer data, a forest scores about 97% on the test set, yet permuting any single feature drops training accuracy by at most 0.012. Its fix is to cluster features by rank correlation and keep one per cluster.

MythReality
feature_importances_ ranks what mattersIt follows the count of distinct values: the no-signal ID ranked second
A low importance means a weak featureThe contract flag ranked last by impurity and tied for first by permutation
Importance on training data is fineOn training rows the ID cost 0.184 of accuracy; on new rows, nothing
Permutation importance settles itCorrelated copies hide each other: tenure scored +0.001 next to its twin
Importance shows what drives the outcomeIt shows what this model relies on, not what changes the outcome

How to apply it in your project

  1. Check the model firstMeasure its held-out score with cross-validation. Importances of a model that doesn't generalize describe noise.
  2. Use permutation importance on held-out dataCall permutation_importance on a validation or test set with n_repeats of at least 10, and report the mean with the spread.
  3. Compare with the training setLarge gaps between training and held-out importance point to features the model uses to memorize.
  4. Cluster correlated featuresGroup columns by rank correlation, as in scikit-learn's multicollinear example, and judge or drop whole groups.
  5. Word it as relianceWrite "the model relies on support calls", not "support calls drive churn"; importance describes the model, not cause and effect.

Key takeaways

  • Impurity importance comes from training splits and favors many distinct values: a no-signal ID ranked second.
  • Permutation importance on held-out data measures what the model needs for new predictions.
  • A feature important on training rows but not held-out ones is being used to overfit.
  • Correlated features share and hide credit; group them before you trust or drop anything.

📚 المصادر

  1. Permutation feature importance — scikit-learn User Guide
  2. Permutation Importance vs Random Forest Feature Importance (MDI) — scikit-learn examples
  3. Permutation Importance with Multicollinear or Correlated Features — scikit-learn examples
  4. Strobl et al., Bias in random forest variable importance measures: Illustrations, sources and a solution (BMC Bioinformatics 2007)

التعليقات (0)