RecontextualizedShapleyExplainer#

class sdm.explain.RecontextualizedShapleyExplainer(**kwargs: Any)#

Bases: object

Compute Shapley values of preprocessed feature columns via recontextualization from the “Interpretable Machine Learning for TabPFN” paper.

The columns outside a subset are dropped from both the in-context and the query examples, so that the model predicts as if it had never seen them.

Note

ShapleyExplainer requires shapiq>=1.8. See here on how to install.

import sdm
import torch

x_context = sdm.TableTensor.from_tensor(
    torch.tensor([
        [-3., 0.],
        [-2., 1.],
        [-1., 2.],
        [ 1., 0.],
        [ 2., 1.],
        [ 3., 2.],
    ], device="cuda"),
    columns=["signal", "noise"],
)
y_context = sdm.TableTensor.from_tensor(
    2 * x_context["signal"].numerical,
    columns=["target"],
)
x_query = sdm.TableTensor.from_tensor(
    torch.tensor([[-0.5, 1.], [0., 1.], [0.5, 1.]], device="cuda"),
    columns=["signal", "noise"],
)

model = sdm.models.TabICLv2(device="cuda")

explainer = sdm.explain.RecontextualizedShapleyExplainer()
out = explainer(
    model,
    x_context,
    y_context,
    x_query,
    output="q500",
    budget=4,
)

# Feature attributions for the first query row:
dict(zip(out[0].feature_names, out[0].get_n_order_values(1)))
# {"signal": -1.6604, "noise": -0.5296}
out[0].plot_waterfall(out[0].feature_names)
Parameters:

kwargs (Any) – Additional keyword arguments passed to shapiq.explainer.AgnosticExplainer, e.g., index or max_order.

__call__(model: ICLModel, x_context: TableTensor, y_context: TableTensor, x_query: TableTensor, *, output: str, budget: int, **kwargs: Any) → list[InteractionValues]#

Explain the contributions of feature columns.

Parameters:
  • model (ICLModel) – The model.

  • x_context (TableTensor) – The feature tensor of in-context examples with shape [R_context, D] with R_context rows and D columns.

  • y_context (TableTensor) – The targets of in-context examples with shape [R_context, 1].

  • x_query (TableTensor) – The feature tensor of query examples with shape [R_query, D] with R_query rows and D columns.

  • output (str) – The name of the predicted column to explain.

  • budget (int) – The budget to use for the approximation.

  • kwargs (Any) – Additional keyword arguments passed to the model.

Returns:

The InteractionValues of each query row.

Return type:

list[InteractionValues]