Sub topic 6
Training data versus test data: what each importance tells you
This chapter assumes you can fit a scikit-learn model, split data into train and test sets, and call permutation_importance as shown earlier. The question here is which split to pass in, and what each choice measures.
Two different questions
Permutation importance shuffles one column and measures how much the score falls. The score depends on the data you shuffle, so the split changes what the number means.
- Test data: how much does the model rely on this feature to make predictions that generalize?
- Training data: how much does the model rely on this feature to reproduce the labels it was fitted on?
A flexible model can answer "yes" to the second question for a feature that is useless for new data. It memorizes noise in the training rows, and shuffling that noise column breaks the memorized predictions.
Seeing the gap
To make the effect visible, add a column of pure random noise. It carries no information about the target, so a trustworthy importance should be near zero.
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor
from sklearn.inspection import permutation_importance
from sklearn.model_selection import train_test_split
X, y = fetch_california_housing(as_frame=True, return_X_y=True)
X["noise"] = np.random.default_rng(0).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)
print(f"train R^2 {model.score(X_train, y_train):.3f}, test R^2 {model.score(X_test, y_test):.3f}")
for name, (Xs, ys) in {"train": (X_train, y_train), "test": (X_test, y_test)}.items():
r = permutation_importance(model, Xs, ys, n_repeats=5, random_state=0, n_jobs=-1)
i = list(X.columns).index("noise")
print(f"{name:<5} noise importance {r.importances_mean[i]:.4f} +/- {r.importances_std[i]:.4f}")Expect a large gap between train and test R². The train-based importance of noise should come out clearly above zero, because the forest used it to fit individual training rows. The test-based importance should sit close to zero, within its spread across repeats.
How to read the two results
| Result | Meaning |
|---|---|
| High on test | The model uses the feature in a way that holds on unseen data. |
| High on train, near zero on test | The model uses the feature to memorize. This is a sign of overfitting. |
| Near zero on both | The model does not use the feature. |
| Slightly negative on test | Shuffling happened to help by chance. Treat it as zero. |
Comparing the two columns of numbers is a useful diagnostic. A feature that ranks high on train but low on test points to where the model overfits.
Practical rules
- Report test-based importance when you want to explain what the model does in deployment.
- Compute the train-based version only as a diagnostic, and say so.
- If the model is well regularized and train and test scores are close, the two rankings will usually agree. A large disagreement is a signal to examine the model, not to pick the more flattering ranking.
- Use the standard deviation across repeats to decide whether a small test value differs from zero.
Exercise
A gradient boosting model has train R² of 0.99 and test R² of 0.70. Feature zip_code_hash has a permutation importance of 0.15 on the training set and 0.00 on the test set. What does this suggest, and which number do you report to explain the model's behavior on new data?
Show answer
The model uses zip_code_hash to memorize training rows, not to make predictions that generalize. This is consistent with overfitting. Report the test-set value of 0.00, and treat the train value as a diagnostic that points to where the model overfits.