Diverse Counterfactual Explanations (DiCE) for Machine Learning

May 10, 2021 | Data Science

In the world of machine learning, understanding why a model arrived at a particular decision is paramount, especially in sensitive sectors like finance, healthcare, and criminal justice. Have you ever wondered how to explain a machine learning model’s decisions in a way that is both accurate and comprehensible? This is where Diverse Counterfactual Explanations (DiCE) come into play! Let’s dive into how to utilize DiCE for your machine learning models in a user-friendly manner.

What is DiCE?

DiCE offers a framework for generating counterfactual explanations. The idea is to present “what-if” scenarios—showing what changes would result in a different prediction from the model—thus helping both users and developers understand model behavior. Think of it like a magic mirror that not only reflects the reality of your machine learning model’s decisions but also suggests how things could have been different.

Installation of DiCE

Ready to integrate DiCE into your environment? Here’s how to get started:

  • To install DiCE using pip, run:
  • pip install dice-ml
  • For conda users, you can install it by executing:
  • conda install -c conda-forge dice-ml
  • To install the latest development version, clone the repository and then:
  • pip install -e .
  • If you encounter any issues, consider installing dependencies manually:
  • pip install -r requirements.txt
    pip install -r requirements-deeplearning.txt
    pip install -r requirements-test.txt

Getting Started with DiCE

Generating explanations with DiCE follows a simple three-step process:

  1. Set up your dataset.
  2. Train a machine learning model.
  3. Invoke DiCE to generate counterfactual examples.

Here’s a brief illustration using code:

import dice_ml
from dice_ml.utils import helpers 
from sklearn.model_selection import train_test_split 

dataset = helpers.load_adult_income_dataset()
target = dataset['income'] 
train_dataset, test_dataset, _, _ = train_test_split(dataset, target, test_size=0.2, random_state=0, stratify=target)

d = dice_ml.Data(dataframe=train_dataset, 
                  continuous_features=['age', 'hours_per_week'], 
                  outcome_name='income')        

m = dice_ml.Model(model_path=dice_ml.utils.helpers.get_adult_income_modelpath(), 
                  backend='TF2', func='ohe-min-max') 
exp = dice_ml.Dice(d, m)

Generating Counterfactual Examples

Let’s take a look at how to generate counterfactual explanations for a specific input:

query_instance = test_dataset.drop(columns='income')[0:1]
dice_exp = exp.generate_counterfactuals(query_instance, total_CFs=4, desired_class='opposite') 
dice_exp.visualize_as_dataframe()

This code provides a what-if analysis and displays the changes required to achieve a different classification.

Troubleshooting Tips

If you experience any challenges during installation or while running DiCE, here are a few troubleshooting ideas:

  • Ensure that your environment supports Python 3+
  • Double-check the versions of the dependencies mentioned in the requirements.
  • Make sure you are using a compatible ML model.
  • If issues persist, visiting fxis.ai could provide additional insights and collaboration opportunities.

Conclusion

By deploying DiCE, you open doors to greater transparency within your machine learning models. With its ability to generate meaningful counterfactual explanations, DiCE is a powerful tool for both model users and developers. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox