Sub topic 13
Beyond These Tools: Conditional Permutation, ALE, and SHAP
This chapter assumes you can compute permutation importance and partial dependence (PD) with scikit-learn, and that you know why correlated features cause trouble for both. It names three alternatives, says what each fixes, and gives criteria for choosing one.
What problem the alternatives solve
Permutation importance and PD share one assumption. To measure a feature, they replace its values with values that ignore the other features. If MedInc and AveRooms are correlated, a shuffled row can pair a high income with a room count that never occurs in real data. The model is then asked about inputs it has never seen, and its answers there are unreliable.
The three methods below each address this in a different way.
| Method | Question it answers | Scope |
|---|---|---|
| Conditional permutation | How much does the score drop if a feature is shuffled only among similar rows? | Global ranking |
| ALE | How does the prediction change as a feature moves within its local data range? | Global curve |
| SHAP | How much did each feature contribute to this one prediction? | Local, summable to global |
Conditional permutation
Ordinary permutation shuffles a column across the whole test set. Conditional permutation shuffles it only within groups of rows that have similar values in the correlated features. The shuffled values stay plausible, so the model is not pushed off the data.
A simple version bins a correlated feature and shuffles inside each bin. The sketch below shuffles MedInc only within bins of AveRooms, reusing model, X_test, and y_test from earlier chapters.
import numpy as np
import pandas as pd
rng = np.random.default_rng(0)
baseline = model.score(X_test, y_test)
bins = pd.qcut(X_test["AveRooms"], q=10, duplicates="drop")
X_cond = X_test.copy()
for _, idx in X_test.groupby(bins, observed=True).indices.items():
values = X_cond["MedInc"].to_numpy()[idx]
X_cond.iloc[idx, X_cond.columns.get_loc("MedInc")] = rng.permutation(values)
print(f"Conditional drop for MedInc: {baseline - model.score(X_cond, y_test):.3f}")Compare this drop with the ordinary one. A much smaller value suggests the ordinary score partly came from impossible rows. Scikit-learn does not provide this method, and the binning choice affects the result.
ALE: accumulated local effects
ALE (accumulated local effects) replaces PD's global substitution with local differences. It splits the feature's range into intervals. For rows that fall inside an interval, it measures how the prediction changes between the interval's two edges. It then accumulates those changes from left to right.
Because each difference uses only rows that really lie in that interval, ALE avoids most unrealistic combinations. It is also less biased than PD when features are correlated. Scikit-learn has no ALE function. The alibi and PyALE packages implement it.
SHAP
SHAP (SHapley Additive exPlanations) assigns each feature a contribution to a single prediction. The contributions plus a base value add up to the model's output. Averaging the absolute contributions over many rows gives a global importance ranking.
SHAP also has a correlation problem, because it needs a way to handle "missing" features. Tree-specific algorithms are fast, but different choices for handling missing features give different answers. SHAP values describe how the model behaves, not how the world works.
Choosing a method
- Start with permutation importance and PD. They are cheap and built in.
- If features are strongly correlated, check with conditional permutation or ALE before trusting a ranking or a curve.
- If you must explain one prediction to a person, use SHAP.
- If you need a global curve in correlated data, prefer ALE over PD.
Exercise
You find that ordinary permutation importance for AveRooms is large, but conditional permutation within bins of AveBedrms gives a much smaller drop. What is the likely explanation, and which result would you trust more?
Show answer
The two features are correlated. Ordinary shuffling creates unrealistic row combinations, and the model's reaction to them inflates the drop. Conditional permutation keeps the combinations plausible, so it is usually the more trustworthy value, subject to the choice of bins.