Generate Explanations

In this section, we show you how to create explanations using the Python Client

Details about how to plot and store the explanations that will be presented in this section, can be found here:

expai.ExpaiModelExplainer

To initialise this class, it is required to get a ModelFairness object using expai.ExpaiProject.get_model_explainer(). More info here.

expai.ExpaiModelExplainer.get_allowed_explanations

This method returns a ExpaiModelExplainer that enables easy explanation generation.

expai.ExpaiProject.get_allowed_explanations(self)

explainer.get_allowed_explanations()

All following methods will return an ExpaiExplanation object containing the plots used to represent the explanation. See more information here.

expai.ExpaiModelExplainer.explain_model

Explain a model based on a whole dataset or a subset representing some meaningful group for your operations.

expai.ExpaiModelExplainer.explain_model(self, sample_name: str = None, sample_id: str = None, subset_indexes: list = None, target_class: str = None)

subset_indexes allow you to explain your model based only on a meaningful subgroup in your data. You can use expai.ExpaiProject.get_sample() to obtain a dataframe, filter and then use the desired indexes.

exp_model = explainer.explain_model(sample_name="Dataset", subset_indexes=subset_indexes)
exp_model.plot_all()

expai.ExpaiModelExplainer.explain_variable_effect

Explain how a single variable impacts the predictions in your model.

expai.ExpaiModelExplainer.explain_variable_effect(self, sample_name: str = None, sample_id: str = None, subset_indexes: list = None, target_class: str = None, variables: list = None, variables_type: dict = None)

subset_indexes allow you to explain your model based only on a meaningful subgroup in your data. You can use expai.ExpaiProject.get_sample() to obtain a dataframe, filter and then use the desired indexes.

# Variables that we want to explain
variables = ['marital-status', 'capital-gain']

# Type for the variables (categorical or numerical)
variables_type = {'marital-status': 'categorical',
                 'capital-gain': 'numerical'}

exp_var = explainer.explain_variable_effect(sample_name="Dataset", variables=variables, variables_type=variables_type)

exp_var.plot_all()

expai.ExpaiModelExplainer.explain_sample

Explain how the prediction for a certain sample was made and the importance of each variable.

expai.ExpaiModelExplainer.explain_sample(self, sample_name: str = None, sample_id: str = None, index: int = None, subset_indexes: list = None, target_class: str = None)

If you use subset_indexes, make sure they contain the index you want to explain.

# Select the index to be explained. If we filtered, it must be within the filtered dataframe.
index_to_explain = 2

# Obtenemos la explicación
exp_sample = explainer.explain_sample(sample_name="Dataset", index=index_to_explain, subset_indexes=subset_indexes)

# Plot
exp_sample.plot_all()

WHAT IF

This module allows you to validate and activate business actions based on your analytical models on the fly.

expai.ExpaiModelExplainer.what_if

Explain how a variable will change the prediction for a single entry when it takes any of its possible values. Obtain a plot for each variable representing the prediction for each possible value when all remaining variables are kept the same.

expai.ExpaiModelExplainer.what_if(self, sample_name: str = None, sample_id: str = None, index: int = None, subset_indexes: list = None, target_class: str = None, variables: list = None, variables_type: dict = None)

subset_indexes allow you to explain your model based only on a meaningful subgroup in your data. You can use expai.ExpaiProject.get_sample() to obtain a dataframe, filter and then use the desired indexes.

# Select index to analyze
index_to_explain = 2

# Variables to analyze in this sample
variables = ['age', 'capital-gain']

# Type for the variables
variables_type = {'age': 'numerical',
                 'capital-gain': 'numerical'}

exp_what_if = explainer.what_if(sample_name="Dataset", index=index_to_explain, variables=variables, variables_type=variables_type, subset_indexes=subset_indexes)

# Plot
exp_what_if.plot_all()

expai.ExpaiModelExplainer.what_if_battle

With this explanation, you will be able to change the values you want for a single entry and obtain the new prediction and its explanation after the changes. Validate business decisions on the fly.

expai.ExpaiModelExplainer.what_if_battle(self, sample_name: str = None, sample_id: str = None, index: int = None, subset_indexes: list = None, target_class: str = None, replace_dict: dict = None, display_replace_dict: dict = None)

subset_indexes allow you to explain your model based only on a meaningful subgroup in your data. You can use expai.ExpaiProject.get_sample() to obtain a dataframe, filter and then use the desired indexes.

# Select index to analyze
index_to_explain = 2

# Values to be replaced 
replace_dict = {'age': 35}

# Generate explanation
exp_what_if_battle = explainer.what_if_battle(sample_name="Dataset", index=index_to_explain, replace_dict=replace_dict, subset_indexes=subset_indexes)

# Plot
exp_what_if_battle.plot_all()

Last updated