EXPAI Docs
Search…
Categorical Model Explanation
This is a sample code to use our EXPAI Client for a Categorical Model step by step

Download the files

We highly recommend using Jupyter Lab to work with EXPAI Client

Imports

Remember to install our Python client by using pip install -U expai
1
from expai import ExpaiAccount
2
import numpy as np
3
import pandas as pd
4
import os
5
6
# Trust notebook for correct plots rendering
7
!jupyter trust Categorical\ Model\ Explanation.ipynb
Copied!

Login to your account

1
email = 'YOUR-EMAIL'
2
user_pass = 'YOUR-PASS'
3
expai_client = ExpaiAccount(email=email, user_pass=user_pass)
Copied!

Create a Project

The first step to interact with your account is creating a Project.
1
expai_client.create_project("Fairness Project")
Copied!

Interact with your Project

We will first obtain a Project object that will allow us to interact with its content and generate explanations.
1
project = expai_client.get_project(project_name = "Fairness Project")
Copied!

Create a model

In this step, we use the model provided here. All parameters and their descriptions can be found in the Python client docs.
1
project.create_model(model_path = os.path.abspath("../models/fairness_pipeline.pkl"),
2
model_name="Potential model",
3
model_summary="Annual revenue over 100K$ model",
4
model_library = 'pickle',
5
model_objective = 'classification',
6
model_prediction_type = "binary",
7
output_classes = ["No", "Yes"])
Copied!

Create a sample

In this step, we use the dataset provided here. All parameters and their descriptions can be found in the Python client docs.
1
# Sample - Categorical
2
project.create_sample(sample_path=os.path.abspath("../datasets/fairness.csv"),
3
sample_name="Dataset",
4
sample_encoding='utf-8',
5
sample_separator=";",
6
sample_target_col = "target",
7
protected_columns=['race', 'sex'],
8
drop_columns=['race', 'sex'],
9
is_display=False)
Copied!

Explain the model

Once we have included a model and a sample to our project, we can execute all available explanations. First of all, we must generate a Model Explainer object for our model.
1
# Set global variables for explanation
2
model_name = "Potential model"
3
sample_name = "Dataset"
4
5
# Get Model Explainer object
6
explainer = project.get_model_explainer(model_name)
7
8
# By default, set to None. Use the following section to replace
9
subset_indexes = None
Copied!

Defining a subgroup for explanations

This step can be skipped if you are not interested on defining a subgroup.
Sometimes, we don't want to study how our model works for the whole dataset but for a specific meaningful subgroup. In this case, we could be interested on studying how the model behaves for people under 40 years old.
1
# Get the stored sample for filtering
2
df = project.get_sample(sample_name=sample_name)
3
4
# Filter the dataframe
5
subset = df[df['age']<=40]
6
7
# Obtain the indexes for our samples of interest
8
subset_indexes = list(subset.index)
Copied!

Generate Explanations and Plots

Once we obtained the Model Explainer, we can use it to generate all possible explanations for our model. When an explanation is generated an Explanation object will be returned.

Model Explanation

This explanation represents the importance of each variable in the predictions. It is computed as the increase in the prediction error when this variable is removed.
1
# Generate the explanation
2
exp_model = explainer.explain_model(sample_name=sample_name, subset_indexes=subset_indexes)
3
4
# Plot
5
exp_model.plot_all()
Copied!
Output for Model Explanation

Variable Explanation

In this case, we will explore the effect of a given variable in our predictions. In other words, we will represent the average prediction of the model in terms of the variable.
1
# Variables that we want to explain
2
variables = ['marital-status', 'capital-gain']
3
4
# Type for the variables (categorical or numerical)
5
variables_type = {'marital-status': 'categorical',
6
'capital-gain': 'numerical'}
7
8
# Generate explanation
9
exp_var = explainer.explain_variable_effect(sample_name=sample_name, variables=variables, variables_type=variables_type, subset_indexes=subset_indexes)
10
11
# Plot all of them
12
exp_var.plot_all()
Copied!
Output sample for Variable Explanation

Explain a unique entry

In this explanation, we will be able to understand which was the impact of each variable for the prediction of a unique entry in our dataset.
1
# Select the index to be explained. If we filtered, it must be within the filtered dataframe.
2
index_to_explain = 2
3
4
# Generate explanation
5
exp_sample = explainer.explain_sample(sample_name=sample_name, index=index_to_explain, subset_indexes=subset_indexes)
6
7
# Plot
8
exp_sample.plot_all()
Copied!
Output sample for entry explanation

WHAT IF

This explanation will allow you to see what would happen if we change only one variable in the entry while all remaining variables are kept the same. It will plot all possible values for a variable and the prediction for them.
1
# Select index to analyze
2
index_to_explain = 2
3
4
# Variables to analyze in this sample
5
variables = ['age']
6
7
# Type for the variables
8
variables_type = {'age': 'numerical'}
9
10
exp_what_if = explainer.what_if(sample_name=sample_name, index=index_to_explain, variables=variables, variables_type=variables_type, subset_indexes=subset_indexes)
11
12
# Plot
13
exp_what_if.plot_all()
Copied!
WHAT IF example

WHAT IF BATTLE

This explanation is similar to the previous one but now we can replace all the values we want at the same time and study how predictions will change.
1
# Select index to analyze
2
index_to_explain = 2
3
4
# Values to be replaced
5
replace_dict = {'age': 35,
6
'capital-gain': 5000}
7
8
exp_what_if_battle = explainer.what_if_battle(sample_name=sample_name, index=index_to_explain, replace_dict=replace_dict, subset_indexes=subset_indexes)
9
10
# Plot
11
exp_what_if_battle.plot_all()
Copied!
WHAT IF Battle example.
Last modified 1mo ago