Sub topic 14
A practical workflow: rank, then probe
This chapter assumes you can fit a scikit-learn model on a train/test split. It also assumes you have met permutation importance, partial dependence (PD) and ICE curves in earlier chapters. Here you combine them into one routine.
Why order matters
The two tools answer different questions. Permutation importance asks how much the model needs a feature. Partial dependence asks how the prediction changes as a feature moves. Importance is cheap to read across many features. PD plots take effort to read, so you should spend that effort only where importance says it matters.
The routine has four steps:
- Rank features with permutation importance on held-out data.
- Draw PD and ICE curves for the top features.
- Check for the problems that mislead both tools.
- Re-run the ranking and plots after you change the model or features.
The workflow in code
The script below runs steps 1 and 2 end to end. It selects the top three features by mean importance and plots each one as ICE curves plus their average.
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor
from sklearn.inspection import PartialDependenceDisplay, permutation_importance
from sklearn.model_selection import train_test_split
X, y = fetch_california_housing(as_frame=True, return_X_y=True)
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)
# Step 1: rank on held-out data
result = permutation_importance(
model, X_test, y_test, n_repeats=10, random_state=0, n_jobs=-1
)
order = result.importances_mean.argsort()[::-1]
top = [X.columns[i] for i in order[:3]]
print("Top features:", top)
# Step 2: probe the top features
PartialDependenceDisplay.from_estimator(
model, X_test, top, kind="both", subsample=200, random_state=0
)
plt.show()Use kind="both" so you see the average and the individual curves together. A flat average with fanned-out ICE lines points to an interaction. Opposite effects in different rows cancel in the average.
Reading the two outputs together
| Importance result | PD/ICE result | Likely meaning |
|---|---|---|
| High | Steep, ICE lines parallel | Strong, consistent effect |
| High | Flat average, ICE lines diverge | Strong effect that depends on other features |
| Near zero | Steep curve | Suspect a correlated partner that carries the same signal |
| Near zero | Flat | The model does not use the feature |
The third row is the common surprise. When two features are correlated, shuffling one leaves the other available, so importance is split or hidden. The PD curve still shows the model responds to the feature.
Step 3: pitfall checks
Run these checks before you report anything:
- Compute importance on the test set, not the training set. Training importance can reward features the model memorized.
- Look at the standard deviation across repeats. If it is as large as the mean, do not trust the rank order.
- Compute pairwise correlations among the top features. For strongly correlated pairs, treat their importance as shared.
- Compare the plotted PD range with the data. Trim the grid to the 5th to 95th percentile, as in the hand-written
partial_dependence_1d, so you avoid extrapolation. - If a PD curve looks implausible, check whether the grid crosses combinations that never occur in the data. Consider ALE for that case.
Step 4: repeat after changes
Importance and PD shift when you retrain, drop features, or switch models. Keep the script above as a function or notebook cell and rerun it after each change. Compare the top features across model types. If a random forest and a gradient-boosted model agree on the ranking and curve shapes, you can be more confident in the conclusions.
Exercise
Permutation importance on the test set gives a feature a score of 0.001, but its PD curve rises steeply. Name one likely cause and one check.
Show answer
A correlated feature probably carries the same information, so shuffling one column barely hurts the score. Check the correlation between the two features, and compare importance after dropping the partner or permuting the pair together.