Sub topic 3
Why built-in tree importances are not enough
This chapter assumes you can fit a scikit-learn tree ensemble, split data into train and test sets, and read an R² score. You also need basic pandas. The previous chapter set up a held-out split and a score to track. Here you will see why the importance numbers that come with tree models are a weak basis for explaining them.
What the built-in number is
Fitted forests and gradient-boosted trees in scikit-learn expose feature_importances_. This is the impurity-based importance, also called mean decrease in impurity (MDI). Each split in each tree reduces impurity, which is the variance of the target for regression or Gini impurity for classification. The model credits that reduction to the feature used for the split. It then sums the credit per feature, averages across trees, and normalizes so the values add up to 1.
It is free, because it is computed while the model trains. That convenience is also the source of its problems.
Three limits
- It is computed on training data. The numbers describe how the splits behaved on the rows the model learned from. They say nothing about whether a feature helps on new rows. A feature the model uses to memorize noise still earns credit.
- It favors features with many distinct values. A continuous or high-cardinality feature offers many candidate split points. Some split will reduce impurity by chance, so such a feature collects credit even when it carries no signal. Binary and low-cardinality features have fewer chances.
- It is tied to the model type. MDI exists only for tree-based models. You cannot compare it with a linear model or a neural network, and it does not use your evaluation metric.
A further limit is that correlated features split the credit between them in an arbitrary way. A later chapter covers that problem, because it affects permutation importance too.
See the bias directly
The code below adds a column of pure random noise to the California housing data. By construction it has no relationship to the target. Then it fits a forest and prints the impurity importances.
import numpy as np
import pandas as pd
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
X, y = fetch_california_housing(as_frame=True, return_X_y=True)
rng = np.random.default_rng(0)
X["Noise"] = rng.normal(size=len(X))
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
model = RandomForestRegressor(n_estimators=100, n_jobs=-1, random_state=0)
model.fit(X_train, y_train)
imp = pd.Series(model.feature_importances_, index=X.columns)
print(imp.sort_values(ascending=False).round(3))
print("Train R^2:", round(model.score(X_train, y_train), 3))
print("Test R^2: ", round(model.score(X_test, y_test), 3))Look at the Noise row. It receives a nonzero share of the importance, even though it cannot help predict anything. Now compare the two R² values. The training score is much higher than the test score. The forest fits the training rows almost perfectly, and some of the trees' splits go to the noise column to do it. MDI counts those splits as useful.
What a better measure needs
The limits above point to three requirements.
| Limit of MDI | What a replacement should do |
|---|---|
| Uses training data | Score the model on held-out data |
| Biased toward many-valued features | Not depend on how many split points a feature offers |
| Specific to trees | Work with any fitted model through `predict` |
Permutation importance meets all three. It treats the model as a black box, uses your chosen metric, and measures damage on data the model has not seen. The next chapter introduces it.
Exercise
You fit a random forest and find that a continuous ID-like column, unrelated to the target, ranks third in feature_importances_. Give two reasons this can happen, and name one property a better importance measure should have.
Show answer
Impurity importance is computed on training data, so splits that fit noise still earn credit. It also favors features with many distinct values, because they offer many candidate split points and some will reduce impurity by chance. A better measure should be computed on held-out data, so a feature gets credit only if it helps on rows the model did not train on. It should also work for any model, not only trees.