Sub topic 2
Training Data Versus Test Data: What Each Importance Tells You
Assume you can train and evaluate a scikit-learn model with NumPy and pandas. You should also know why a train/test split is used and how metrics such as R² or accuracy work.
Training Data Versus Test Data
Permutation importance measures how much a model's score falls after shuffling one feature. On training data, the result shows which features the fitted model used to reproduce examples it has already seen. On held-out test data, it shows which features support predictions on unseen examples.
Test-set importance is usually the better estimate of predictive usefulness. Training importance can be inflated when the model overfits. Always report the scoring metric and several shuffle repeats.
| Data used | Main interpretation | Main risk |
|---|---|---|
| Training set | Features used to fit known examples | Overfitting can inflate importance |
| Test set | Features supporting generalization | A small test set gives noisy estimates |
Correlated Features: Shared and Hidden Importance
When two features contain similar information, the model may predict well if either one is available. Shuffling only one leaves its partner intact, so the score may fall very little. Both features can therefore receive low individual importance even though the information they represent is essential.
The opposite can also happen with tree models: one correlated feature may be selected early and receive most of the credit, while its partner receives little. Test several related features together, or use a method designed for conditional importance.
Partial Dependence: The Average Effect
Partial dependence (PD) estimates the model's average prediction as one feature changes. For a feature value v, it replaces that feature with v in many rows, predicts each modified row, and averages the predictions. It describes the model's global response, not an individual explanation.
A rising curve means higher feature values produce higher average predictions. A flat curve suggests little average effect. A bend or plateau shows a changing effect.
Computing Partial Dependence
import numpy as np
def pd_1d(model, X, feature, grid_size=20):
grid = np.linspace(
X[feature].quantile(0.05),
X[feature].quantile(0.95),
grid_size,
)
values = []
for value in grid:
modified = X.copy()
modified[feature] = value
values.append(model.predict(modified).mean())
return grid, np.array(values)Scikit-learn provides the same idea:
from sklearn.inspection import PartialDependenceDisplay
import matplotlib.pyplot as plt
PartialDependenceDisplay.from_estimator(
model, X_test, ["MedInc"], grid_resolution=20
)
plt.show()The grid is commonly restricted to the 5th through 95th percentiles. This avoids interpreting extreme values with little supporting data.
Exercise
Why can test-set permutation importance be more trustworthy than training-set importance?
Show answer
It measures the score drop on unseen examples, so it better reflects generalization and is less affected by memorization.