shapiq.explainer.Explainer¶

class shapiq.explainer.Explainer(model, data=None, class_index=None, index='SV', max_order=1, **kwargs)[source]¶

Bases: object

The main Explainer class for a simpler user interface.

shapiq.Explainer is a simplified interface for the shapiq package. It detects between TabularExplainer, TreeExplainer, and TabPFNExplainer. For a detailed description of the different explainers, see the respective classes.

Initialize the Explainer class.

Parameters:
  • model (Any | Game | Callable[[ndarray], ndarray]) – The model object to be explained.

  • data (ndarray | None) – A background dataset to be used for imputation in TabularExplainer or TabPFNExplainer. This is a 2-dimensional NumPy array with shape (n_samples, n_features). Can be empty for the TreeExplainer, which does not require background data.

  • class_index (int | None) – The class index of the model to explain. Defaults to None, which will set the class index to 1 per default for classification models and is ignored for regression models. Note, it is important to specify the class index for your classification model.

  • index (Literal['SV', 'SII', 'k-SII', 'STII', 'FSII', 'BV', 'BII', 'FBII']) – The type of Shapley interaction index to use. Defaults to "SV", which computes the Shapley value. To compute interactions, pass an interaction index explicitly and set max_order accordingly. Options are: - "SV": Shapley value - "k-SII": k-Shapley Interaction Index - "FSII": Faithful Shapley Interaction Index - "FBII": Faithful Banzhaf Interaction Index (becomes BV for order 1) - "STII": Shapley Taylor Interaction Index - "SII": Shapley Interaction Index

  • max_order (int) – The maximum order of interactions to be computed. Set to 1 for no interactions (i.e, for Shapley values "SV" or Banzhaf values "BV").

  • **kwargs (Any) – Additional keyword-only arguments passed to the specific explainer classes.

explain(x=None, **kwargs)[source]¶

Explain a single prediction in terms of interaction values.

Parameters:
  • x (ndarray | None) – A numpy array of a data point to be explained.

  • **kwargs (Any) – Additional keyword-only arguments passed to the specific explainer’s explain_function method.

Return type:

InteractionValues

Returns:

The interaction values of the prediction.

explain_X(X, *, n_jobs=None, random_state=None, verbose=False, **kwargs)[source]¶

Explain multiple predictions at once.

This method is a wrapper around the explain method. It allows to explain multiple predictions at once. It is a convenience method that uses the joblib library to parallelize the computation of the interaction values.

Parameters:
  • X (ndarray) – A 2-dimensional matrix of inputs to be explained with shape (n_samples, n_features).

  • n_jobs (int | None) – Number of jobs for joblib.Parallel. Defaults to None, which will use no parallelization. If set to -1, all available cores will be used.

  • random_state (int | None) – The random state to re-initialize Imputer and Approximator with. Defaults to None.

  • verbose (bool) – Whether to print a progress bar. Defaults to False.

  • **kwargs (Any) – Additional keyword-only arguments passed to the explainer’s explain_function method.

Return type:

list[InteractionValues]

Returns:

A list of interaction values for each prediction in the input matrix X.

abstractmethod explain_function(x, *args, **kwargs)[source]¶

Explain a single prediction in terms of interaction values.

Parameters:
  • x (ndarray | None) – A numpy array of a data point to be explained.

  • *args (Any) – Additional positional arguments passed to the explainer.

  • **kwargs (Any) – Additional keyword-only arguments passed to the explainer.

Return type:

InteractionValues

Returns:

The interaction values of the prediction.

predict(x)[source]¶

Provides a unified prediction interface for the explainer.

Parameters:

x (ndarray) – An instance/point/sample/observation to be explained.

Return type:

ndarray

Returns:

The model’s prediction for the given data point as a vector.

set_random_state(random_state=None)[source]¶

Set the random state for the explainer and its components.

Note

Setting the random state in the explainer will also overwrite the random state in the approximator and imputer, if they are set.

Parameters:

random_state (int | None) – The random state to set. If None, no random state is set.

Return type:

None

property approximator: Approximator¶

The approximator used by the explainer (or None in the base class).

property exact_computer: ExactComputer¶

The exact computer used by the explainer (or None in the base class).

property imputer: Imputer¶

The imputer used by the explainer (or None in the base class).

property index: Literal['SV', 'SII', 'k-SII', 'STII', 'FSII', 'BV', 'BII', 'FBII']¶

The type of Shapley interaction index the explainer is using.

property max_order: int¶

The maximum interaction order the explainer is using.

model: Any | Game | Callable[[ndarray], ndarray]¶

The model to be explained, either as a Model instance or a callable function.