Skip to content

Partial dependence, ICE and ALE

Sweep one feature and watch predictions move. The average hides interactions, ICE lines reveal them, and ALE stays honest when features are correlated.

Applied ~12 min8 steps

What runs in CodeSplash

You will be able to

  • Build a partial dependence curve and read its ICE lines
  • Spot an interaction from crossing ICE lines
  • Choose ALE when features are correlated

Read first

01

Sweep one feature

Read

To ask how the model uses temperature, do this for each of a set of values:

  1. Set every row's temperature to that value.
  2. Predict.
  3. Average the predictions.

That average, drawn across values, is the partial dependencePartial dependenceThe average prediction as one feature is set to each value across all rows. curve. Keep each row's line separate and you have ICE curvesICE curveOne row's prediction as one feature sweeps its values. Partial dependence is the average of these lines..

Partial dependence with ICEHow does the prediction change as one feature changes, on average and for individual rows?
View data
BMIpredicted probabilitylinekind
180.4428row 1ice
19.5000.4476row 1ice
210.4524row 1ice
22.5000.4573row 1ice
240.4621row 1ice
25.5000.4669row 1ice
270.4717row 1ice
28.5000.4766row 1ice
300.4814row 1ice
31.5000.5662row 1ice
330.5711row 1ice
34.5000.5759row 1ice
360.5807row 1ice
37.5000.5855row 1ice
390.5904row 1ice
40.5000.5952row 1ice
420.6000row 1ice
43.5000.6048row 1ice
450.6097row 1ice
46.5000.6145row 1ice
480.6193row 1ice
180.3717row 2ice
19.5000.4044row 2ice
210.4372row 2ice
22.5000.4699row 2ice
240.5027row 2ice
25.5000.5354row 2ice
270.5682row 2ice
28.5000.6009row 2ice
300.6337row 2ice
31.5000.7464row 2ice
330.7792row 2ice
34.5000.8119row 2ice
360.8447row 2ice
37.5000.8774row 2ice
390.9102row 2ice
40.5000.9429row 2ice
420.9757row 2ice
43.5000.9800row 2ice
450.9800row 2ice
46.5000.9800row 2ice
480.9800row 2ice
180.2310row 3ice
19.5000.2639row 3ice
210.2968row 3ice
22.5000.3297row 3ice
240.3626row 3ice
25.5000.3955row 3ice
270.4285row 3ice
28.5000.4614row 3ice
300.4943row 3ice
31.5000.6072row 3ice
330.6401row 3ice
34.5000.6730row 3ice
360.7059row 3ice
37.5000.7388row 3ice
390.7717row 3ice
40.5000.8046row 3ice
420.8376row 3ice
43.5000.8705row 3ice

First 60 of 1281 rows.

02

What can the average hide?

Predict

Predict first

A feature's partial dependence curve is flat. Half of its ICE lines rise, and the other half fall. What's going on?

Flat curve: the feature isn't usedFlat curve: the feature could be used in opposite waysFlat curve: the model has a bug
Reveal

The feature is used in opposite ways. Its effect depends on another feature, and the average cancels it out. That's an interaction. So always look at the ICE lines: a flat average with lines going both ways doesn't mean "no effect". Friedman's H can tell you which partner it is.

03

Feel an interaction

Explore

Set True interaction above zero. The effect of xx changes with Moderator value. A partial dependence curve would give the average of those slopes, and ICE lines would fan out.

DExplorable · Interactions and simple slopes

When the effect of x depends on a moderator, where is it actually different from zero?

What is the slope of x at this moderator value?

0.0
How sure?
0.50
120
0.0

Centering: with a mean far from zero, the coefficient on x describes a moderator value no one has.

1.0
no effectJ–NJ–N

Where the band excludes zero, the slope of x is significant at that moderator value. Johnson–Neyman (J–N) marks the boundaries.

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

Now set True interaction to zero. The slope is the same everywhere: an average tells the whole story, and ICE lines would be parallel.

04

Impossible rows

Read

Partial dependence has a flaw. Setting every row's temperature to a value creates combinations that never occur, such as a freezing July day. With correlated features, the model is then being asked about places it never saw. Its answers there are noise, and they enter the average.

Check where the data is. If a value has few rows, or a combination isn't plausible, don't trust that part of the curve.

05

ALE stays where the data is

Read

Accumulated local effectsAccumulated local effectsAdds up local prediction changes, moving only rows that fall in each window of the feature. avoid this. For a narrow window of the feature:

  1. Take only rows that really fall in the window.
  2. Move each from the window's low edge to its high edge.
  3. Average the change.

Then add up those local changes across windows. Rows are moved only a short way, so they stay plausible.

Accumulated local effectsWhat is a feature's effect on predictions when features are correlated?
View data
daylight (minutes)effect on daily rentalslowerupper
400−1067.90−1183.90−951.90
480−1093.10−1202.70−983.50
560−985.50−1088.70−882.30
640−884−980.80−787.20
720−840.20−930.60−749.80
800−657.90−741.90−573.90
880−510.30−587.90−432.70
960−390−461.20−318.80
1,040−200.40−265.20−135.60
1,12059.600−2121.20
1,200301.30233.30369.30
1,280505.90431.50580.30
1,360598.10517.30678.90
1,440753.40666.20840.60
1,520927.50833.901021.10
1,600975.10875.101075.10
1,6801034.30927.901140.70
1,7601088.809761201.60
1,8401133.101013.901252.30
1,9201168.501042.901294.10
2,0001195.401063.401327.40

If PD and ALE agree, you have little to worry about. If they differ, trust ALE, and look at which features are correlated with this one.

06

Check your understanding

Checkpoint

Check yourself

How is one point of a partial dependence curve computed?

Show the answer

Answer: a

The second option is a conditional average, which mixes in correlated features. Partial dependence sets the value across all rows.

Check yourself

ICE lines for a feature are all parallel. What does that mean?

Show the answer

Answer: a

Parallel lines differ in level only. The model can still be non-linear in the feature itself.

Check yourself

Height and weight are strongly correlated. Which plot is safer for the effect of height?

Show the answer

Answer: b

Partial dependence would create very tall, very light people. ALE avoids asking the model about them.

07

Plot them

In the app

In a run's Explanations tab, tick Partial dependence + ICE and Accumulated local effects, then press Explain. Remember that these describe the model and not the world. To check for interaction partners, add Interactions (Friedman's H).

Python
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.impute import SimpleImputer
from sklearn.inspection import partial_dependence
from sklearn.pipeline import make_pipeline

df = pd.read_csv("airquality.csv").dropna(subset=["Ozone"])
X, y = df[["Solar.R", "Wind", "Temp"]].astype(float), df["Ozone"]
m = make_pipeline(SimpleImputer(strategy="median"), RandomForestRegressor(300, random_state=1)).fit(X, y)
pd_res = partial_dependence(m, X, ["Temp"], kind="both", grid_resolution=8)
print("grid", pd_res["grid_values"][0].round(0))
print("average", pd_res["average"][0].round(1))
print("spread of ICE at each value", pd_res["individual"][0].std(axis=0).round(1))
R
library(ranger)
df <- airquality[!is.na(airquality$Ozone), c("Ozone", "Solar.R", "Wind", "Temp")]
df$Solar.R[is.na(df$Solar.R)] <- median(df$Solar.R, na.rm = TRUE)
fit <- ranger(Ozone ~ ., df, num.trees = 300, seed = 1)
grid <- round(seq(min(df$Temp), max(df$Temp), length.out = 8))
ice <- sapply(grid, function(t) { d <- df; d$Temp <- t; predict(fit, d)$predictions })
round(rbind(grid = grid, average = colMeans(ice), ice_spread = apply(ice, 2, sd)), 1)
08

Recap

Recap
  • Partial dependence averages predictions as one feature sweeps. ICE shows each row.
  • Crossing lines mean an interaction, even when the average is flat.
  • With correlated features, partial dependence asks about impossible rows, so use ALE.

Concepts in this lesson