Saltar al contenido principal

ContrastiveShap

LocalExplainer
DashAI.back.explainability.explainers.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

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)

Defined on ContrastiveShap

Compute 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)

Defined on ContrastiveShap

Fit 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_data is 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]

Defined on ContrastiveShap

Render 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

Defined on ConfigObject

Generates the component related Json Schema.

Returns

dict
Dictionary representing the Json Schema of the component.

validate_and_transform(self, raw_data: dict) -> dict

Defined on ConfigObject

It 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.

Compatible with