Sub topic 10
Reading One-Way and Two-Way Partial Dependence Plots
This chapter assumes you can fit a scikit-learn regressor and split data into train and test sets. It also assumes you know how a partial dependence value is computed: set a feature to a grid value for every row, predict, and average. Here the focus is on reading the plots that result.
What a one-way plot shows
A one-way partial dependence plot has the feature's values on the x-axis. The y-axis is the model's average prediction when every row is given that value. The curve answers one question: on average, how does the prediction move as this feature changes?
Read each plot in this order:
- Axis range. Check the units and the span of the y-axis. A curve that looks steep may cover only a small slice of the target.
- Direction. Does the curve rise, fall, or stay level?
- Shape. Look for straight lines, plateaus, steps, and bends. Tree ensembles produce stepped curves, because their predictions change only at split thresholds.
- Strength. Compare the curve's total vertical range with the spread of the target.
- Data density. scikit-learn draws tick marks along the x-axis at the deciles of the feature. Be cautious where the ticks are sparse.
Drawing both kinds of plot
The script below fits the model and draws two one-way plots and one two-way plot. It sets grid_resolution=20 so the two-way surface computes quickly.
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor
from sklearn.inspection import PartialDependenceDisplay
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)
fig, ax = plt.subplots(figsize=(8, 3.5))
PartialDependenceDisplay.from_estimator(
model, X_test, ["MedInc", "HouseAge"], grid_resolution=20, ax=ax
)
fig, ax = plt.subplots(figsize=(6, 5))
PartialDependenceDisplay.from_estimator(
model, X_test, [("MedInc", "AveOccup")], grid_resolution=20, ax=ax
)
plt.show()Here MedInc (median income) should rise steeply, so higher income means higher predicted house value. HouseAge should be much flatter. The y-axis range shows this, and it is the strength comparison from step 4.
Reading a two-way plot
A two-way plot is a contour map. The two features are on the axes, and each contour line joins points with the same average prediction. Color shows the predicted value.
- Contours crossing the plot evenly mean the features act roughly additively. The two-way surface is then close to the sum of the two one-way curves.
- Contours that bend or change spacing mean an interaction. The effect of one feature depends on the value of the other.
- Dense contours mark a steep region. Wide gaps mark a flat one.
Compare the surface against the one-way curves for the same features. If the surface shows nothing beyond what the one-way curves already show, the two-way plot adds little.
| What you see | What it suggests |
|---|---|
| Smooth, monotonic curve | Steady average effect |
| Staircase with plateaus | Threshold behavior, typical of trees |
| Flat line | No average effect (see the warning below) |
| Parallel straight contours | Additive features |
| Curved or fanning contours | Interaction between the two features |
Staying within the data
Partial dependence builds every grid value into every row, including combinations that never occur in real data. scikit-learn trims the grid to the 5th through 95th percentile by default (percentiles=(0.05, 0.95)) to limit extreme extrapolation. Even so, treat the regions where the decile ticks are sparse as less trustworthy.
Exercise
A two-way plot of features A and B shows contour lines that are straight and parallel. What does this say about their interaction, and what would change your answer?
Show answer
Straight, parallel contours mean the features act additively: the effect of A does not depend on B, so there is little or no interaction visible in the average. Contours that bent, or whose spacing changed as B changed, would indicate an interaction. Averaging can still hide opposing interactions, so check ICE curves before concluding there is none.