Sub topic 11
ICE curves: seeing the individual behind the average
This chapter assumes you can fit a scikit-learn regressor and that you have read the previous chapters on partial dependence. You should know that a partial dependence curve averages predictions over the data.
What an average can hide
A partial dependence plot (PDP) draws one line. That line is the mean prediction as one feature changes. A flat or gently sloped mean can come from two very different situations:
- Every row responds in roughly the same way, so the average is a fair summary.
- Some rows go up, others go down, and the effects cancel in the average.
An individual conditional expectation (ICE) curve separates these cases. It draws one line per row, so you see the spread behind the average.
How an ICE curve is computed
Take one row. Hold all its other features fixed. Set the feature of interest to each value on a grid and predict at each one. The resulting line is that row's ICE curve. Repeat for many rows.
The PDP is the pointwise mean of all ICE curves. ICE uses the same substitution step as partial dependence and skips the final averaging.
Computing ICE by hand
This script is self-contained. It returns a matrix with one row per sample and one column per grid value.
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor
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)
def ice_1d(model, X, feature, grid):
curves = np.empty((len(X), len(grid)))
for j, value in enumerate(grid):
X_mod = X.copy()
X_mod[feature] = value
curves[:, j] = model.predict(X_mod)
return curves
sample = X_test.sample(200, random_state=0)
grid = np.linspace(X_test["MedInc"].quantile(0.05), X_test["MedInc"].quantile(0.95), 20)
curves = ice_1d(model, sample, "MedInc", grid)
pd_curve = curves.mean(axis=0)
print("PDP at first grid point:", round(pd_curve[0], 3))
print("Spread of ICE at that point:", round(curves[:, 0].std(), 3))The mean of curves is the partial dependence curve for these 200 rows. The standard deviation of the curves at each grid point measures how much rows disagree.
Drawing ICE with scikit-learn
Setting kind="both" overlays the individual curves and their average. Use subsample to limit how many rows are drawn.
import matplotlib.pyplot as plt
from sklearn.inspection import PartialDependenceDisplay
fig, ax = plt.subplots(figsize=(7, 4))
PartialDependenceDisplay.from_estimator(
model, X_test, ["MedInc"], kind="both", subsample=200, random_state=0, ax=ax
)
plt.show()Reading the plot
Look at the thin lines before the thick one.
| What you see | What it suggests |
|---|---|
| Thin lines roughly parallel to the average | The feature acts the same way for most rows |
| Lines that fan out or cross | An interaction with another feature |
| A flat average with steep thin lines | Opposite effects cancel each other |
| A few lines far from the rest | Unusual rows worth inspecting |
Curves often start at different heights because rows have different baseline predictions. That makes shape differences hard to compare. A common fix is to subtract each curve's first value (curves - curves[:, [0]]). The curves then all start at zero, and only the changes remain.
Practical limits
- Drawing every row is slow and unreadable on large data. Sample a few hundred rows.
- ICE shows that rows differ. It does not say which other feature causes the difference. To find that, color the curves by a candidate feature, or plot a two-way PDP.
- Each curve costs one model prediction per grid point and row, so runtime grows with both.
Exercise
A PDP for a feature is nearly flat, but the ICE plot shows many steeply rising lines and many steeply falling lines. What does this indicate, and what would you check next?
Show answer
The feature has strong but opposite effects in different parts of the data, and the average cancels them. This points to an interaction with another feature. Check by coloring the ICE curves by a candidate feature, or by plotting a two-way partial dependence plot with it.