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.
What runs in CodeSplash
- XGBoost Verified in Python and R
- LightGBM Verified in Python and R
- Random forest Verified in Python and R
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
Sweep one feature
ReadTo ask how the model uses temperature, do this for each of a set of values:
- Set every row's temperature to that value.
- Predict.
- 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..
View data
| BMI | predicted probability | line | kind |
|---|---|---|---|
| 18 | 0.4428 | row 1 | ice |
| 19.500 | 0.4476 | row 1 | ice |
| 21 | 0.4524 | row 1 | ice |
| 22.500 | 0.4573 | row 1 | ice |
| 24 | 0.4621 | row 1 | ice |
| 25.500 | 0.4669 | row 1 | ice |
| 27 | 0.4717 | row 1 | ice |
| 28.500 | 0.4766 | row 1 | ice |
| 30 | 0.4814 | row 1 | ice |
| 31.500 | 0.5662 | row 1 | ice |
| 33 | 0.5711 | row 1 | ice |
| 34.500 | 0.5759 | row 1 | ice |
| 36 | 0.5807 | row 1 | ice |
| 37.500 | 0.5855 | row 1 | ice |
| 39 | 0.5904 | row 1 | ice |
| 40.500 | 0.5952 | row 1 | ice |
| 42 | 0.6000 | row 1 | ice |
| 43.500 | 0.6048 | row 1 | ice |
| 45 | 0.6097 | row 1 | ice |
| 46.500 | 0.6145 | row 1 | ice |
| 48 | 0.6193 | row 1 | ice |
| 18 | 0.3717 | row 2 | ice |
| 19.500 | 0.4044 | row 2 | ice |
| 21 | 0.4372 | row 2 | ice |
| 22.500 | 0.4699 | row 2 | ice |
| 24 | 0.5027 | row 2 | ice |
| 25.500 | 0.5354 | row 2 | ice |
| 27 | 0.5682 | row 2 | ice |
| 28.500 | 0.6009 | row 2 | ice |
| 30 | 0.6337 | row 2 | ice |
| 31.500 | 0.7464 | row 2 | ice |
| 33 | 0.7792 | row 2 | ice |
| 34.500 | 0.8119 | row 2 | ice |
| 36 | 0.8447 | row 2 | ice |
| 37.500 | 0.8774 | row 2 | ice |
| 39 | 0.9102 | row 2 | ice |
| 40.500 | 0.9429 | row 2 | ice |
| 42 | 0.9757 | row 2 | ice |
| 43.500 | 0.9800 | row 2 | ice |
| 45 | 0.9800 | row 2 | ice |
| 46.500 | 0.9800 | row 2 | ice |
| 48 | 0.9800 | row 2 | ice |
| 18 | 0.2310 | row 3 | ice |
| 19.500 | 0.2639 | row 3 | ice |
| 21 | 0.2968 | row 3 | ice |
| 22.500 | 0.3297 | row 3 | ice |
| 24 | 0.3626 | row 3 | ice |
| 25.500 | 0.3955 | row 3 | ice |
| 27 | 0.4285 | row 3 | ice |
| 28.500 | 0.4614 | row 3 | ice |
| 30 | 0.4943 | row 3 | ice |
| 31.500 | 0.6072 | row 3 | ice |
| 33 | 0.6401 | row 3 | ice |
| 34.500 | 0.6730 | row 3 | ice |
| 36 | 0.7059 | row 3 | ice |
| 37.500 | 0.7388 | row 3 | ice |
| 39 | 0.7717 | row 3 | ice |
| 40.500 | 0.8046 | row 3 | ice |
| 42 | 0.8376 | row 3 | ice |
| 43.500 | 0.8705 | row 3 | ice |
First 60 of 1281 rows.
What can the average hide?
PredictPredict first
A feature's partial dependence curve is flat. Half of its ICE lines rise, and the other half fall. What's going on?
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.
Feel an interaction
ExploreSet True interaction above zero. The effect of 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?
Centering: with a mean far from zero, the coefficient on x describes a moderator value no one has.
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 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.
Impossible rows
ReadPartial 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.
ALE stays where the data is
ReadAccumulated 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:
- Take only rows that really fall in the window.
- Move each from the window's low edge to its high edge.
- Average the change.
Then add up those local changes across windows. Rows are moved only a short way, so they stay plausible.
View data
| daylight (minutes) | effect on daily rentals | lower | upper |
|---|---|---|---|
| 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,120 | 59.600 | −2 | 121.20 |
| 1,200 | 301.30 | 233.30 | 369.30 |
| 1,280 | 505.90 | 431.50 | 580.30 |
| 1,360 | 598.10 | 517.30 | 678.90 |
| 1,440 | 753.40 | 666.20 | 840.60 |
| 1,520 | 927.50 | 833.90 | 1021.10 |
| 1,600 | 975.10 | 875.10 | 1075.10 |
| 1,680 | 1034.30 | 927.90 | 1140.70 |
| 1,760 | 1088.80 | 976 | 1201.60 |
| 1,840 | 1133.10 | 1013.90 | 1252.30 |
| 1,920 | 1168.50 | 1042.90 | 1294.10 |
| 2,000 | 1195.40 | 1063.40 | 1327.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.
Check your understanding
CheckpointCheck 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.
Plot them
In the appIn 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).
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))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)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