Stop 09 · Trees & Ensemble MethodsTopics · 8/8

Feature Importance & Permutation Importance

Part of the Machine Learning Roadmap

In short

'Which features drive this prediction?' is the first question every manager asks. Tree models hand you an answer for free, but that answer can mislead. Permutation importance gives a more honest one.

Tree models report impurity-based feature importance, but it can favour features with many unique values. Permutation importance shuffles one feature at a time on validation data and measures how much the score drops, which is more trustworthy.

The jaadu tastes a spoonful from one of several biryani pots lined up on a stove, with little bowls of spices shuffled around beside each pot.

Think of it like this

Swapping spices in the biryani

To find which spice matters most in a biryani, a cook makes the same recipe again but swaps one spice for a random one from the rack. If the biryani tastes terrible without the real saffron, saffron mattered. If nobody notices when the bay leaf is swapped, it barely mattered. Permutation importance shuffles one feature at a time and checks how much the score drops.

Watch it happen

Validation accuracy drop when each feature is shuffled

income: -0.12
0
credit_score: -0.09
1
age: -0.01
2
customer_id: 0.00
3

1/5The model scores 0.88 accuracy on validation data with all features intact.

The big ideas

What you need to know

Impurity importance

feature_importances_ sums how much each feature's splits reduced impurity in training. Fast, but measured on training data.

Its biases

It favours continuous or high-cardinality features like IDs, which offer many split points even when they mean nothing.

Permutation importance

Shuffle one column in validation data, re-score, and measure the drop. Works for any model.

Watch correlated features

If two columns carry the same information, shuffling one barely hurts because the other covers for it. Both can look unimportant.

See it in code

Walk through the code

python
1import numpy as np2import pandas as pd3from sklearn.datasets import fetch_openml4from sklearn.ensemble import RandomForestClassifier5from sklearn.inspection import permutation_importance6from sklearn.model_selection import train_test_split7 8X, y = fetch_openml("credit-g", version=1, return_X_y=True, as_frame=True)9X = X.select_dtypes("number").copy()10X["random_id"] = np.random.default_rng(0).integers(0, 100000, len(X))   # pure noise11 12X_tr, X_val, y_tr, y_val = train_test_split(X, y, stratify=y, random_state=0)13rf = RandomForestClassifier(n_estimators=300, random_state=0).fit(X_tr, y_tr)14 15impurity = pd.Series(rf.feature_importances_, index=X.columns).sort_values(ascending=False)16perm = permutation_importance(rf, X_val, y_val, n_repeats=10, random_state=0)17permuted = pd.Series(perm.importances_mean, index=X.columns).sort_values(ascending=False)18 19print(impurity.round(3))20print(permuted.round(3))     # random_id drops to about zero here

1/5Load the credit data, keep numeric columns, and add a meaningless ID with many unique values.

The German credit dataset with a fake random ID column added as a trap.

Save yourself the pain

Common mistakes

Oops

Presenting feature_importances_ to stakeholders as the truth.

Do this instead

Confirm with permutation importance on validation data, and say clearly that importance is not the same as cause.

Oops

Dropping a feature because permutation importance is near zero when it's correlated with another.

Do this instead

Check correlations first. Group correlated features and permute them together, or drop one and re-check.

Your mission

Catch the fake feature

~25 min

Quick check

A random ID column ranks third in a random forest's feature_importances_. What is the most likely explanation?

Go deeper

  1. 1Plot feature_importances_ from a random forest.
  2. 2Compute permutation_importance on the validation set and compare the rankings.
  3. 3Add a random noise column and see where it ranks in each method.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge