Sub topic 12
Where partial dependence misleads
This chapter assumes you can compute a partial dependence curve, either by hand or with scikit-learn, and that you have seen ICE curves. A partial dependence plot (PDP) is an average, and an average can hide or invent structure. Three problems account for most bad readings: hidden heterogeneity, the independence assumption, and extrapolation.
Hidden heterogeneity
Partial dependence averages the predictions over all rows. If half the rows respond upward and half respond downward, the average is flat. A flat curve therefore does not mean "this feature does not matter". It may mean the effects cancel.
The synthetic example below makes this visible. The target is x1 * x2, where x2 is either -1 or 1.
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.inspection import partial_dependence
rng = np.random.default_rng(0)
n = 2000
X = pd.DataFrame({"x1": rng.uniform(-1, 1, n), "x2": rng.choice([-1, 1], n)})
y = X["x1"] * X["x2"] + rng.normal(0, 0.1, n)
model = RandomForestRegressor(n_estimators=100, random_state=0).fit(X, y)
res = partial_dependence(model, X, ["x1"], kind="both", grid_resolution=5)
print("grid: ", np.round(res["grid_values"][0], 2))
print("average:", np.round(res["average"][0], 2))
last = res["individual"][0][:, -1]
print("ICE at largest x1:", round(last.min(), 2), "to", round(last.max(), 2))The average values stay close to 0. The ICE values at the largest x1 split into two groups, near -0.9 and near 0.9. The code uses the grid_values key from scikit-learn 1.3 and later. Older versions call it values.
Permutation importance would rank x1 highly here, so the two tools disagree. That disagreement is a signal to draw ICE curves or color them by another feature.
The independence assumption
To compute partial dependence for x1, you set x1 to a grid value in every row and keep the other columns unchanged. This pairs the grid value with combinations of the other features that may never occur. If height and weight are strongly correlated, setting height to 2.0 m for a row weighing 45 kg creates an implausible person.
The model has little or no training data there. Its predictions in those regions are arbitrary, and they still enter the average.
Extrapolation
Extrapolation is the same problem seen from the grid side. A grid that spans the full min-max range includes sparse tails. Tree models predict a constant beyond the data, which produces misleading flat ends. Other models may produce wild values.
scikit-learn's default grid uses the 5th to 95th percentiles for this reason. The hand-written function in an earlier chapter does the same. This trims the tails but does nothing for impossible combinations in the middle of the range.
Practical checks
- Draw ICE curves with
kind="both"and see whether the lines resemble the average. - Check the correlation matrix for the feature you are plotting. A correlation above about 0.7 is a warning sign.
- Add a rug of data deciles under the curve. Ignore wiggles where there are few points.
- Compare with a two-way PDP when you suspect an interaction.
Summary table
| Pitfall | Symptom | Check or remedy |
|---|---|---|
| Hidden heterogeneity | Flat PDP, high permutation importance | ICE curves, two-way PDP |
| Independence assumption | Correlated features, odd shapes | Correlation matrix, ALE |
| Extrapolation | Flat or erratic ends | Percentile grid, data rug |
Exercise
A PDP for feature age is nearly flat, but permutation importance ranks age second. Give two possible explanations and one plot that helps you tell them apart.
Show answer
Either the effect of age differs across rows and cancels in the average (an interaction), or the model uses age in a way the averaged curve hides. For example, age may matter only in a narrow range outside the trimmed grid. An ICE plot with kind="both" shows whether individual lines slope in different directions. A two-way PDP with the suspected interacting feature is another option.