Sub topic 8
Partial dependence: the average effect of a feature
This chapter assumes you can train a scikit-learn model, split data into train and test sets, and read a prediction array. You should also know the permutation importance idea from earlier chapters.
What question it answers
Permutation importance tells you which features matter. It does not tell you how a feature matters. Does a higher median income push predicted house prices up? Is the relationship a straight line, a plateau, or a step?
Partial dependence answers that. It shows the average prediction of the model as one feature (or two) is moved across a range of values. The other features stay as they are in the data.
The definition
Let the model be f, the feature of interest be xS, and the remaining features be xC. The partial dependence at a value v is:
PD(v) = average over the data of f(v, x_C)
In words: pick the value v. Write it into the feature column of every row. Predict for all rows and average the predictions. Repeat for many values of v and plot the results.
The averaging is the "partial" part. The curve does not follow any single row. It marginalizes over the other features, which means it averages them out.
What the curve means
Each point answers a what-if question: if every row in this dataset had this feature value, what would the model predict on average? The result is a global description of the model's behavior. It describes the model, not the real world. If the model learned a wrong relationship, the curve shows the wrong relationship.
A small example makes the output concrete. The code below trains a model and asks scikit-learn for five points of the curve for median income.
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor
from sklearn.inspection import partial_dependence
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)
result = partial_dependence(model, X_test, ["MedInc"], grid_resolution=5)
print(result["grid_values"][0].round(2))
print(result["average"][0].round(2))The first line printed is the five income values. The second is the average predicted price at each. Prices should rise with income. The next chapter shows how these numbers are built by hand.
Where the curve is trustworthy
Partial dependence rests on one assumption: the feature you vary is not strongly related to the others. When you set a feature to v in every row, you create rows that may never occur naturally. A row with a very high income and a tiny house may be one of them. The model has seen no data like that, so its predictions there are guesses, and they enter the average.
Two consequences follow:
- Read the curve most carefully where data is dense. Scikit-learn limits the grid to the 5th to 95th percentile of the feature by default for this reason.
- Treat the curve as an average. Two opposite effects in different groups can cancel and produce a flat line. A flat curve does not prove the feature is unused.
Comparing the two tools
| Tool | Question | Output |
|---|---|---|
| Permutation importance | Which features does the model rely on? | One number per feature |
| Partial dependence | How does the prediction change with a feature? | A curve per feature |
Importance ranks features. Partial dependence describes the shape of the effect of the features you choose to inspect.
Exercise
A partial dependence curve for a feature is almost perfectly flat, yet permutation importance ranks that feature near the top. Give one explanation.
Show answer
The feature probably has opposite effects in different parts of the data, such as an interaction with another feature. Averaging over all rows cancels the effects and flattens the curve, while shuffling the feature still damages predictions and lowers the score. Individual curves (ICE) would show the opposing slopes.