DiceCounterfactual
Diverse counterfactual explanations via the DiCE library.
For each instance, generates a set of synthetic examples that the model classifies as a different (desired) class while staying close to the original instance, answering "what minimal changes would flip this prediction?". Unlike the Nearest Counterfactual explainer (which returns real training rows), DiCE synthesizes new feature combinations and optimizes for both proximity and diversity.
Note: DiCE queries the underlying estimator directly with raw feature values, so it is intended for datasets with numeric features.
References
- [1] Mothilal, R.K., Sharma, A. & Tan, C. (2020). "Explaining Machine Learning Classifiers through Diverse Counterfactual Explanations." FAT* 2020. https://arxiv.org/abs/1905.07697
- [2] https://github.com/interpretml/DiCE
Parameters
- total_cfs : integer, default=
3 - Number of counterfactual examples to generate per instance.
- method : string, default=
random - Counterfactual search strategy: 'random' (random sampling of feature perturbations), 'genetic' (genetic algorithm optimizing proximity and diversity) or 'kdtree' (closest real training examples).
- desired_class : string, default=
opposite - Class the counterfactuals should reach. Enter an exact class name, or leave 'opposite' to target the runner-up class of each instance.
Methods
explain_instance(self, instances)
DiceCounterfactualGenerate counterfactual examples for each instance.
Parameters
- instances : DatasetDict
- Instances to be explained.
Returns
- dict
- Dictionary with, for each instance, the model prediction and the generated counterfactual examples.
fit(self, background_dataset, **kwargs)
DiceCounterfactualBuild the DiCE data and model interfaces from the train split.
Parameters
- background_dataset : Tuple[DatasetDict, DatasetDict]
- Tuple
(x, y)with the dataset splits. - **kwargs : Any
- Ignored; present for interface compatibility.
Returns
- DiceCounterfactual
- The fitted explainer instance (
self).
plot(self, explanation: dict) -> List[DashAI.back.core.artifacts.GroupedArtifacts]
DiceCounterfactualRender each instance as a comparison table 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 comparison table 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.