Sub topic 7
Correlated features: shared and hidden importance
This chapter assumes you can compute permutation importance on a held-out set, as in the previous chapters. You should also know what a partial dependence curve is.
Why correlation changes the answer
Permutation importance shuffles one column and measures the score drop. The shuffle breaks that feature's link to the target. It also breaks its link to every feature correlated with it.
If a model can get the same information from a second, correlated column, shuffling the first costs little. The second column still carries the signal. Each feature in the group then looks less important than the group really is.
A demonstration
The target below depends on x1 and x3. The column x2 is a noisy copy of x1 and adds no new information.
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.inspection import permutation_importance
from sklearn.model_selection import train_test_split
rng = np.random.default_rng(0)
n = 2000
x1 = rng.normal(size=n)
x2 = x1 + rng.normal(scale=0.1, size=n)
x3 = rng.normal(size=n)
y = 3 * x1 + x3 + rng.normal(scale=0.5, size=n)
X = pd.DataFrame({"x1": x1, "x2": x2, "x3": x3})
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
def report(columns):
model = RandomForestRegressor(n_estimators=100, n_jobs=-1, random_state=0)
model.fit(X_train[columns], y_train)
result = permutation_importance(
model, X_test[columns], y_test, n_repeats=10, random_state=0, n_jobs=-1
)
for name, mean in zip(columns, result.importances_mean):
print(f"{name:<4} {mean:.3f}")
print("With x2:")
report(["x1", "x2", "x3"])
print("Without x2:")
report(["x1", "x3"])With x2 present, x1 loses importance, and x2 gets a share of it. Without x2, x1 should come out clearly as the dominant feature. The split depends on which copy the forest happened to use in each tree, so it is not stable across random seeds.
Reading the symptoms
- A feature that your domain knowledge says matters shows a low importance. Check for a correlated partner.
- Two features have similar, modest importances, and the standard deviation across repeats is large.
- Removing one feature from the model raises the importance of the other.
Large importance for a feature does not prove the model needs that exact column. Low importance does not prove the information is useless.
Handling correlated groups
You have three practical options.
- Inspect the correlation matrix first. Use
X_train.corr()or a rank-based version, and flag pairs above roughly 0.8. - Permute groups together. Shuffle the same row order across all columns in a group, then score. The drop is the importance of the group as a whole. Scikit-learn's
permutation_importancedoes not do this directly, so you write the loop yourself, as in the hand-built version from the procedure chapter. - Drop or merge redundant columns and refit, if the model is meant to be simple.
There is a second problem. Shuffling x1 alone creates rows such as a large x1 paired with a small x2. Those rows may never occur in real data. The model is scored on inputs it was not trained on, and the score drop can be exaggerated or arbitrary.
Effect on partial dependence
Partial dependence has the same weakness, because it also sets one feature to a grid value while leaving the others unchanged. For correlated features it evaluates the model at combinations that do not exist. The curve may then reflect model behavior in empty regions. A later chapter covers this under extrapolation and the independence assumption.
Exercise
In the example, a colleague says "x2 has an importance near zero, so we can ignore it, and x1 is only moderately important." What would you check before agreeing, and what would you expect to see?
Show answer
Check the correlation between x1 and x2, which is very high by construction. Refit without x2 and recompute permutation importance. If x1's importance rises sharply, the earlier low value came from x2 acting as a backup signal. You could also shuffle x1 and x2 together to get the importance of the group.