explainy - black-box model explanations for humans
explainy is a library for generating machine learning models explanations in Python. It uses methods from Machine Learning Explainability and provides a standardized API to create feature importance explanations for samples.
The API is inspired by scikit-learn and has two core methods explain() and plot(). The explanations are generated in the form of texts and plots.
explainy comes with four different algorithms to create either global or local and contrastive or non-contrastive model explanations.
Documentation¶
Install explainy¶
pip install explainy
Usage¶
📚 A comprehensive example of the explainy API can be found in this
.. image:: https://github.com/MauroLuzzatto/explainy/blob/main/examples/01-explainy-intro.ipynb
- target
https://github.com/MauroLuzzatto/explainy/blob/main/examples/01-explainy-intro.ipynb
- alt
Jupyter Notebook
📖 Or in the example section of the documentation
Initialize and train a scikit-learn model:
import pandas as pd
from sklearn.datasets import load_diabetes
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
diabetes = load_diabetes()
X_train, X_test, y_train, y_test = train_test_split(
diabetes.data, diabetes.target, random_state=0
)
X_test = pd.DataFrame(X_test, columns=diabetes.feature_names)
y_test = pd.DataFrame(y_test)
model = RandomForestRegressor(random_state=0).fit(X_train, y_train)
Initialize the PermutationExplanation (or any other explanation) object and pass in the trained model and the to be explained dataset.
Addtionally, define the number of features used in the explanation. This allows you to configure the verbosity of your exaplanation.
Set the index of the sample that should be explained.
from explainy.explanations import PermutationExplanation
number_of_features = 4
sample_index = 1
explainer = PermutationExplanation(
X_test, y_test, model, number_of_features
)
Call the explain() method and print the explanation for the sample (in case of a local explanation every sample has a different explanation).
explanation = explainer.explain(sample_index=sample_index)
print(explanation)
The RandomForestRegressor used 10 features to produce the predictions. The prediction of this sample was 251.8.
The feature importance was calculated using the Permutation Feature Importance method.
The four features which were most important for the predictions were (from highest to lowest): ‘bmi’ (0.15), ‘s5’ (0.12), ‘bp’ (0.03), and ‘age’ (0.02).
Use the plot() method to create a feature importance plot of that sample.
explainer.plot()
If your prefer, you can also create another type of plot, as for example a boxplot.
explainer.plot(kind='box')
Model Explanations¶
Method |
Type |
Explanations |
Classification |
Regression |
|---|---|---|---|---|
non-contrastive |
global |
|
|
|
non-contrastive |
local |
|
|
|
contrastive |
global |
|
|
|
contrastive |
local |
|
|
Description
global: explanation of system functionality (all samples have the same explanation)
local: explanation of decision rationale (each sample has its own explanation)
contrastive: tracing of decision path (differences to other outcomes are described)
non-contrastive: parameter weighting (the feature importance is reported)
Features¶
Algorithms for inspecting black-box machine learning models
Support for the machine learning frameworks
scikit-learnandxgboostexplainy offers a standrdized API with three core methods
explain(),plot(),importance()
Other Machine Learning Explainability libraries to watch¶
shap: A game theoretic approach to explain the output of any machine learning model
eli5: A library for debugging/inspecting machine learning classifiers and explaining their predictions
alibi: Algorithms for explaining machine learning models
interpret: Fit interpretable models. Explain blackbox machine learning
Source¶
Molnar, Christoph. “Interpretable machine learning. A Guide for Making Black Box Models Explainable”, 2019. https://christophm.github.io/interpretable-ml-book/