Skip to main content

explainy - black-box model explanations for humans

pypi version codecov docs Supported versions Code style: black Imports: isort Downloads

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 three core methods explain(), plot() and, importance(). 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.

Method Type Explanations Classification Regression
Permutation Feature Importance non-contrastive global ⭐ ⭐
Shap Values non-contrastive local ⭐ ⭐
Surrogate Model contrastive global ⭐ ⭐
Counterfactual Example 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)

Documentation

https://explainy.readthedocs.io

Install explainy

pip install explainy

Further, install graphviz (version 9.0.0 or later) for plotting tree surrogate models:

Windows

choco install graphviz

Mac

brew install graphviz

Linux: Ubuntu packages

sudo apt install graphviz

Further details on how to install graphviz can be found in the official graphviz docs.

Also, make sure that the folder with the dot executable is added to your systems PATH. You can find further details here.

Usage

📚 A comprehensive example of the explainy API can be found in this 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)
model.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

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=1)
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()

Permutation Feature Importance

If your prefer, you can also create another type of plot, as for example a boxplot.

explainer.plot(kind='box')

Permutation Feature Importance BoxPlot

Finally, you can also look at the importance values of the features (in form of a pd.DataFrame).

feature_importance = explainer.importance()
print(feature_importance)
  Feature  Importance
0     bmi        0.15
1      s5        0.12
2      bp        0.03
3     age        0.02
4      s2       -0.00
5     sex       -0.00
6      s3       -0.00
7      s1       -0.01
8      s6       -0.01
9      s4       -0.01

Features

  • Algorithms for inspecting black-box machine learning models
  • Support for the machine learning frameworks scikit-learn and xgboost
  • explainy offers a standardized 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/

Author

Mauro Luzzatto - Maurol

Metadata

Release files for explainy 0.2.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for explainy 0.2.12
File Size Uploaded
explainy-0.2.12.tar.gz 654.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for explainy 0.2.12
File Interpreter ABI Platform
explainy-0.2.12-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 683.1 kB

Release files / explainy-0.2.12.tar.gz

Download URL explainy-0.2.12.tar.gz
Size 654.9 kB
Tags Source
SHA-256 checksum
How to use checksums
49ceaf243ebe0cd8502088b657937ff520464d9797dcab39123df377b69a2176
BLAKE2b-256 checksum
How to use checksums
ffaa10800a79c5cb00d9927862566e37ed467bbbe1179976f36d0a73c204af1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.9.6 requests/2.31.0 setuptools/65.5.0 requests-toolbelt/1.0.0 tqdm/4.66.1 CPython/3.11.3

Release files / explainy-0.2.12-py2.py3-none-any.whl

Download URL explainy-0.2.12-py2.py3-none-any.whl
Size 28.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
04357526c810330f3f215fb5f530cd86898e1d9f6fd26400fdbcc7fef5bd3f44
BLAKE2b-256 checksum
How to use checksums
e84fea46fa92bbdc4e3b66b4f99af7b6de122e48a9bc1ea07539906c81e8fea5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.9.6 requests/2.31.0 setuptools/65.5.0 requests-toolbelt/1.0.0 tqdm/4.66.1 CPython/3.11.3

Release history Release notifications | RSS feed

This release

0.2.12 This release

2 release files

0.2.11

2 release files

0.2.10

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.14

2 release files

0.1.12

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.4

2 release files

0.1.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page