Explainability#
The sdm.explain package provides methods for explaining predictions from an ICLModel.
It includes both wrappers around external libraries such as shapiq and captum, and native implementations that require no additional dependencies.
All explainers follow a similar interface, but their arguments and return types vary by method. Some explain preprocessed numerical features rather than raw inputs, and some require a fitted model. Once initialized, an explainer can be reused across different models and inputs:
import sdm
explainer = sdm.explain.SaliencyExplainer()
explanation = explainer(model, x_query, output="True")
Choosing an Explainer#
Prediction explanations explain individual query predictions:
Explainer |
Question |
Background Data |
|---|---|---|
How sensitive is the prediction to a small change in an input? |
||
How much does each column contribute to the prediction of a row? |
Reference rows |
|
The same, for a model that never sees the removed columns. |
In-context examples |
|
How does the prediction of a row respond when one column changes? |
||
How does the mean prediction respond when one column changes? |
Feature importances measure each column’s contribution to prediction quality on labeled queries:
Explainer |
Question |
Background Data |
|---|---|---|
How much does the fitted model rely on a column? |
Query labels |
|
How much does including a column improve model performance? |
Query labels + in-context examples |
Shapley Values#
ShapleyExplainer distributes the prediction of each row over its columns.
It evaluates a fitted model on subsets of the columns of a row, and an imputer decides how columns outside a subset are removed:
Imputer |
Replacement Values |
Reference Prediction |
|---|---|---|
|
Column means or modes from |
Prediction for the resulting reference row |
|
Values from sampled |
Mean prediction over the sampled rows |
|
Missing values handled by the model |
Prediction for a row with all values missing |
explainer = sdm.explain.ShapleyExplainer(imputer="baseline")
explanation = explainer(
model,
x_query,
output="True",
background=x_context,
budget=256,
)
The explainer returns one shapiq.interaction_values.InteractionValues object per query row.
The Shapley values of a row and its reference prediction sum to its prediction.
They are exact when budget covers all \(2^D\) subsets of the \(D\) columns, and approximated from sampled subsets otherwise.
The output names one column of the predict() output, e.g., a class for classification or the median "q500" for regression.
Passing max_order=2 returns k-Shapley interaction values (k-SII) instead, which split the prediction into main effects and pairwise interactions:
sdm.explain.ShapleyExplainer(imputer="baseline", max_order=2)
InteractionValues can be plotted by shapiq itself:
values = explanation[0]
values.plot_waterfall(feature_names=values.feature_names, show=True)
values.plot_network(feature_names=values.feature_names, show=True)
An in-context model can also remove a column by never seeing it.
RecontextualizedShapleyExplainer drops the columns outside a subset from both the in-context and the query examples.
It takes the in-context examples instead of a fitted model:
explainer = sdm.explain.RecontextualizedShapleyExplainer()
explanation = explainer(
model,
x_context=x_context,
y_context=y_context,
x_query=x_query,
output="True",
budget=256,
)
Both explainers return the same kind of values, but answer different questions: ShapleyExplainer explains how the fitted model uses the value of a column, and RecontextualizedShapleyExplainer explains what the model gains from having the column at all.
When columns are dependent, replacing individual values can create unrealistic combinations of original and replacement values.
Recontextualization instead removes the column from both the in-context examples and query rows, letting the model use the remaining columns.
However, recontextualization is more expensive because it reprocesses the in-context examples for each model call.
ICE and Partial Dependence#
ICEExplainer sets a column to each value of a grid in all rows.
The predictions of a single row trace its individual conditional expectation (ICE) curve:
explainer = sdm.explain.ICEExplainer(grid_resolution=5)
explanation = explainer(model, x_query, output="True", inputs="age")
grid, curve = explanation["age"] # [G], [R, G]
PartialDependenceExplainer takes the same options and returns the mean of these curves over rows, which is the partial dependence of the column:
explainer = sdm.explain.PartialDependenceExplainer(grid_resolution=5)
explanation = explainer(model, x_query, output="True", inputs="age")
grid, average = explanation["age"] # [G], [G]
The grid of a numerical column holds grid_resolution evenly spaced values between its 5th and 95th percentiles in the query rows, and the grid of a categorical column holds the categories of the query rows.
Plot the curves against the grid, e.g., with matplotlib:
import matplotlib.pyplot as plt
plt.plot(grid.cpu(), curve.t().cpu(), color="gray") # ICE
plt.plot(grid.cpu(), average.cpu(), color="black") # Mean
Feature Importance#
PermutationExplainer shuffles a column across rows and reports how much the score of the fitted model decreases.
LOCOExplainer instead leaves the column out of both the in-context and the query examples (leave-one-covariate-out, LOCO), so that the model can fall back on related columns:
explainer = sdm.explain.PermutationExplainer(num_repeats=10)
explanation = explainer(model, x_query, y_query)
explanation.to_pandas() # [10, D]
explainer = sdm.explain.LOCOExplainer()
explanation = explainer(model, x_context, y_context, x_query, y_query)
explanation.to_pandas() # [1, D]
Both explainers return a TableTensor of score decreases, named by column.
A column that is important under permutation but not when left out shares its information with other columns.
A nonpositive LOCOExplainer value means the model predicts at least as well without the column.
For in-context learning models, this requires no retraining since we can simply omit the column from both the in-context examples and query rows.
Saliency#
SaliencyExplainer measures how sensitive a prediction is to small changes in each preprocessed numerical feature.
It takes raw query rows and a fitted model, and returns the (absolute) gradients of the selected output:
explainer = sdm.explain.SaliencyExplainer(signed=False)
explanation = explainer(model, x_query, output="True")
explanation.to_pandas() # [R, D]
The result is a TableTensor with one value per query row and preprocessed numerical feature.
Larger values indicate greater local sensitivity.