
Feature Importance Can Mislead: Impurity vs Permutation Importance
- كاتب موثّق
- scikit-learn 1.9
محتويات المقال
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
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.
What actually happens
Impurity importance counts splits on training rows
Permutation importance measures lost accuracy on new rows
tenure_days as a near copy of tenure, and a customer_id with no signal.
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.285import numpy as npfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.inspection import permutation_importancefrom 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), rng.poisson(1.5, n), rng.integers(0, 2, n), rng.permutation(n)])names = ["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)X_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)pi = permutation_importance(rf, X_te, y_te, n_repeats=10, random_state=0)for name, m, s in zip(names, pi.importances_mean, pi.importances_std): print(f"{name:14} {m:+.3f} +/- {s:.3f}")# tenure_months +0.001 +/- 0.008# tenure_days +0.013 +/- 0.010# support_calls +0.040 +/- 0.012# has_contract +0.040 +/- 0.015# customer_id -0.015 +/- 0.012tenure_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.
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
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.
feature_importances_ ranks what matters | |
How to apply it in your project
Check the model first Measure its held-out score with cross-validation. Importances of a model that doesn't generalize describe noise. Use permutation importance on held-out data Call permutation_importanceon a validation or test set withn_repeatsof at least 10, and report the mean with the spread.Compare with the training set Large gaps between training and held-out importance point to features the model uses to memorize. Cluster correlated features Group columns by rank correlation, as in scikit-learn's multicollinear example, and judge or drop whole groups. Word it as reliance Write "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.
📚 المصادر
Permutation feature importance — scikit-learn User Guide Permutation Importance vs Random Forest Feature Importance (MDI) — scikit-learn examples Permutation Importance with Multicollinear or Correlated Features — scikit-learn examples Strobl et al., Bias in random forest variable importance measures: Illustrations, sources and a solution (BMC Bioinformatics 2007)
التعليقات (0)