Partial dependence and individual effects

See how a model's prediction changes as one feature moves, on average and row by row.

Lesson 58 of 6610 minAdvanced

You will learn to

  • Read partial dependence and ICE plots
  • Spot interactions between features
  • Avoid reading curves in regions with little data

Before this lesson

Intuition

Feature importance tells you that size matters. It does not tell you how: do bigger houses always cost more? Does the effect fade? Does it jump at some value? Partial dependence answers this by running a what-if experiment on the model.

Take your dataset. Set the size of every house to 60 and let the model predict, then average the predictions. Do it again with 80, 100 and so on. Plot the average prediction against the size you imposed. The result, a partial dependence plot (PDP), shows the model's average response to one feature while the other features stay as they are in the data.

The average can hide things. ICE (individual conditional expectation) lines draw the same what-if curve for each row separately: one thin line per house. If all lines are parallel, the average tells the full story. If they fan out or cross, the effect of the feature depends on other features. That is an interaction, and ICE is the quickest way to see it.

Picture it

Each grey line is one row as the feature changes. The thick blue line is their average, the partial dependence. Three rows rise steeply and three barely move, so the blue line shows a modest rise that describes neither group. The shaded region on the right has few rows: the curve there is an extrapolation.

Few rows hereFeature valuePredictionOne line per row (ICE)Average (partial dependence)
ICE lines and their average (illustrative). The fan of lines reveals an interaction that the average hides.

The math

For a model ff, a feature jj and the other features xCx_C (the complement), the partial dependence at value xx is the average over the nn rows of the data:

PDj(x)=1n∑i=1nf(x,  xC(i))\mathrm{PD}_j(x) = \frac{1}{n}\sum_{i=1}^{n} f\big(x,\; x_C^{(i)}\big)

where xC(i)x_C^{(i)} are the values of the other features in row ii. The ICE curve of row ii is a single term of this sum:

ICEj(i)(x)=f(x,  xC(i))\mathrm{ICE}_j^{(i)}(x) = f\big(x,\; x_C^{(i)}\big)

So the PDP is the mean of the ICE curves. This calculation assumes that the feature can be changed independently of the others. If feature jj is strongly correlated with another one, setting it to a value in every row creates unrealistic rows (a tiny house with ten bedrooms), and the model's answers there should not be trusted.

A two-feature PDP averages over all other features while moving two at once and is drawn as a heatmap or contour; it shows the joint effect of a pair.

Worked example

A price model (illustrative) behaves like this: if the building is newer than 20 years, price = 50 + 2 x size; otherwise price = 50 + 0.5 x size, with size in square metres and price in thousands. Four houses in the data have ages 5, 10, 40 and 60.

  1. ICE at size 60. The two new houses: 50 + 2 x 60 = 170 each. The two old ones: 50 + 0.5 x 60 = 80 each.
  2. PDP at size 60. The average is (170 + 170 + 80 + 80) / 4 = 125.
  3. ICE at size 100. New: 50 + 200 = 250. Old: 50 + 50 = 100.
  4. PDP at size 100. (250 + 250 + 100 + 100) / 4 = 175.
  5. Slopes. The PDP rises by 175 - 125 = 50 over 40 square metres, which is 1.25 per square metre. The ICE slopes are 2 for the new houses and 0.5 for the old ones. The average of the four slopes, (2 + 2 + 0.5 + 0.5) / 4 = 1.25, matches the PDP.

Nobody in the data has a slope of 1.25. A reader who saw only the PDP would say that "each square metre adds 1.25"; the ICE lines show it is 2 for new buildings and 0.5 for old ones. The age of the building changes the value of space.

How mlflow does it

mlflow does not show partial dependence plots in the app today. The Plots tab offers Feature importance and Permutation importance in its Explainability group. To draw PDP and ICE curves, download the model and use a notebook. The bundle contains pipeline.joblib, the fitted pipeline with its preprocessing, which takes the original columns as input:

python
import joblib
from sklearn.inspection import PartialDependenceDisplay

pipe = joblib.load("pipeline.joblib")

# average (PDP) and per-row (ICE) curves for one feature
PartialDependenceDisplay.from_estimator(
    pipe, X_test, features=["size"], kind="both", subsample=200
)

Here X_test is a data frame with the original columns, and "size" is a column name. Use a subsample of rows to keep the plot readable and the computation fast. See the docs page on downloading the model for the export formats.

Common mistakes

  • Reading the curve as a law of nature. It describes the model, not reality. Where the model is wrong, so is the curve.
  • Reading only the average. A flat average can hide two opposite effects that cancel. Always look at ICE lines when you suspect interactions.
  • Trusting sparse regions. Mark where the data is thin, for example with a rug of data points on the axis, and do not interpret the curve there.
  • Using PDP with strongly correlated features. Setting one feature without the other creates unrealistic rows. Consider a joint two-feature plot, or other methods designed for correlated inputs.
  • Using only the most important feature. Importance says where to look; the shape of the curve, and whether it is monotone, jumps or saturates, says what the model has learned. Check the shape against domain knowledge.

Try it in mlflow

Explore a model candidate

  1. Open Runs, choose a run and open a model candidate.
  2. Open the Plots tab and note the most important feature in Permutation importance.
  3. Download the model, then draw its partial dependence for that feature in a notebook, as shown in this lesson.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. What does a partial dependence curve show?
  2. 2. The ICE lines for a feature fan out: some rise steeply, others stay flat, while the average rises gently. What does this suggest?
  3. 3. Why be careful with the right end of a partial dependence curve if few rows have such large values?