ContrastiveShap
Contrastive local explainer: why class P rather than class Q?
Standard attribution methods answer "why did the model predict P?". Contrastive explanations answer the question people actually ask: "why P rather than Q?". This explainer computes Kernel SHAP attributions for both the predicted class (the fact) and a contrast class (the foil), and reports the per-feature difference. Features with a large positive delta are the ones that pushed the model towards the fact and away from the foil.
The foil can be a fixed class name, or the second most probable class of each instance (default).
References
- [1] Miller, T. (2019). "Explanation in Artificial Intelligence: Insights from the Social Sciences." Artificial Intelligence 267. https://arxiv.org/abs/1706.07269
- [2] Lundberg, S.M. & Lee, S.I. (2017). "A Unified Approach to Interpreting Model Predictions." NeurIPS 30. https://arxiv.org/abs/1705.07874
Parameters
- foil_class : string, default=
second_most_probable - Class to contrast against (the foil in 'why P rather than Q?'). Enter an exact class name, or leave 'second_most_probable' to contrast against the runner-up class of each instance.
- fit_parameter_sample_background_data : boolean, default=
True - 'true' if background data must be sampled; otherwise the entire training set is used. Smaller datasets speed up the algorithm runtime.
- fit_parameter_background_fraction : number, default=
0.2 - If 'Sample background data' is selected, fraction of background samples to draw from the training set.
Methods
explain_instance(self, instances)
ContrastiveShapCompute contrastive SHAP attributions for the given instances.
Parameters
- instances : DatasetDict
- Instances to be explained.
Returns
- dict
- Dictionary with, for each instance, the fact and foil classes and the per-feature attribution difference (fact minus foil).
fit(self, background_dataset, sample_background_data=False, background_fraction=None, **kwargs)
ContrastiveShapFit the underlying Kernel SHAP explainer on background data.
Parameters
- background_dataset : Tuple[DatasetDict, DatasetDict]
- Tuple
(x, y)with the dataset splits; the train split is used as SHAP background data. - sample_background_data : bool
- True if the background data must be sampled.
- background_fraction : float
- Fraction of the training samples used as background data when
sample_background_datais True. - **kwargs : Any
- Ignored; present for interface compatibility.
Returns
- ContrastiveShap
- The fitted explainer instance (
self).
plot(self, explanation: dict) -> List[DashAI.back.core.artifacts.GroupedArtifacts]
ContrastiveShapRender each instance as a contrastive bar plot plus a text summary.
Parameters
- explanation : dict
- Dictionary with the explanation generated by the explainer.
Returns
- List[GroupedArtifacts]
- A single grouped artifact with one group per explained instance, each holding that instance's contrastive plot and text summary.
get_schema(cls) -> dict
ConfigObjectGenerates the component related Json Schema.
Returns
- dict
- Dictionary representing the Json Schema of the component.
validate_and_transform(self, raw_data: dict) -> dict
ConfigObjectIt takes the data given by the user to initialize the model and returns it with all the objects that the model needs to work.
Parameters
- raw_data : dict
- A dictionary with the data provided by the user to initialize the model.
Returns
- dict
- A validated dictionary with the necessary objects.