shapiq.explainer.Explainer¶
- class shapiq.explainer.Explainer(model, data=None, class_index=None, index='SV', max_order=1, **kwargs)[source]¶
Bases:
objectThe main Explainer class for a simpler user interface.
shapiq.Explainer is a simplified interface for the
shapiqpackage. It detects betweenTabularExplainer,TreeExplainer, andTabPFNExplainer. 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 inTabularExplainerorTabPFNExplainer. This is a 2-dimensional NumPy array with shape(n_samples, n_features). Can be empty for theTreeExplainer, which does not require background data.class_index (
int|None) – The class index of the model to explain. Defaults toNone, which will set the class index to1per 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 setmax_orderaccordingly. Options are: -"SV": Shapley value -"k-SII": k-Shapley Interaction Index -"FSII": Faithful Shapley Interaction Index -"FBII": Faithful Banzhaf Interaction Index (becomesBVfor order 1) -"STII": Shapley Taylor Interaction Index -"SII": Shapley Interaction Indexmax_order (
int) – The maximum order of interactions to be computed. Set to1for 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:
- Return type:
- 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
explainmethod. It allows to explain multiple predictions at once. It is a convenience method that uses thejobliblibrary 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 forjoblib.Parallel. Defaults toNone, 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 toNone.verbose (
bool) – Whether to print a progress bar. Defaults toFalse.**kwargs (
Any) – Additional keyword-only arguments passed to the explainer’sexplain_functionmethod.
- Return type:
- 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:
- Return type:
- Returns:
The interaction values of the prediction.
- 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.
- 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).