Partial dependence and ICE
Quick answer
An individual conditional expectation (ICE) curve shows predictions for one row as a selected feature is varied. Partial dependence (PD) averages these curves over a reference dataset. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 SciPy 1.17.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
Reveal opposing ICE slopes
grid=[0,1,2]; b=[1,-1]ice=[[t*v for t in grid] for v in b]print(ice)print([sum(c)/len(c) for c in zip(*ice)])Observed local output:
[[0, 1, 2], [0, -1, -2]][0.0, 0.0, 0.0]The aggregate is flat even though both rows change.
How it works
For model f, selected coordinate j and grid value t, ICE_i(t)=f(t,x_i,-j). PD_j(t)=mean_i ICE_i(t). All other coordinates are copied from each original row. This is a marginal model behavior calculation, not E[y|X_j=t] and not a causal intervention on the world. Brute-force calculation works for any predictor and reveals interaction: for f(a,b)=a*b, rows with b=1 and b=-1 have opposite slopes and the average is flat. Centered ICE subtracts each curve at a common reference t0 to emphasize slope differences. Choose grid points with adequate observed support, use a representative reference set, and flag unrealistic combinations when features are correlated. scikit-learn 1.8 partial_dependence with method="brute", kind="both" returns grid values, averages and individual predictions. For regression the first dimension is the output dimension; the individual array has sample and grid dimensions. Recursion is not available for ICE.
Verify library partial dependence
import numpy as npfrom sklearn.linear_model import LinearRegressionfrom sklearn.inspection import partial_dependenceX=np.array([[0,0],[1,0],[0,2],[1,2]],dtype=float); y=2*X[:,0]+X[:,1]m=LinearRegression().fit(X,y)r=partial_dependence(m,X,[0],method="brute",kind="both",custom_values={0:[0,1,2]})print(np.round(r.average[0],4).tolist())print(bool(np.allclose(r.average[0],r.individual[0].mean(axis=0))))Observed local output:
[1.0, 3.0, 5.0]TrueThe mean of individual predictions agrees with the PD curve.
Common mistakes
A flat PD curve can hide strong opposing local trends. Never interpret extrapolated combinations as observed examples. A plot does not establish a causal effect. Specify the response scale for classifiers.
Practice and next steps
Use f(a,b)=a*b with b values [1,-1,2]. Calculate ICE and PD at a=0,1,2. Then center each curve at a=1. Complete the knowledge check and staged exercise. Next: SHAP and LIME assumptions.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Partial dependence and ICE — knowledge check
Apply partial dependence and ice to new scenarios.
What is ICE indexed by?
What is PD at a grid point?
Opposite ICE slopes can yield?
Why flag a grid point with no realistic combinations?
Which method supports ICE in sklearn?