Skip to main content

explainy is a library for generating explanations for machine learning models in Python. It uses methods from Machine Learning Explainability and provides a standardized API to create feature importance explanations for samples. The explanations are generated in the form of plots and text.

Project description

explainy - black-box model explanations for humans

pypi version travis docs Supported versions Code style: black 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 explanations are generated in the form of plots and text.

explainy comes with four different algorithms to create either global or local and contrastive or non-contrastive model explanations.

Documentation

https://explainy.readthedocs.io

Install explainy

pip install explainy

Usage

Initialize and train a sklearn 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)

Pass the trained model and the to be explained dataset into a PermutationExplanation (or any other explanation) object.

Define the number of features used in the explanation and the index of the sample that should be explained.

from explainy.explanations.permutation_explanation import PermutationExplanation

number_of_features = 4
sample_index = 1

explainer = PermutationExplanation(
    X_test, y_test, model, number_of_features
)

explanation = explainer.explain(
    sample_index, separator='\n'
)

Print the explanation for the sample. In case of a local explanation every sample as a different explanation.

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).

Plot the feature importance 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

Model Explanations

Method Type Explanations Classification Regression
Permutation Feature Importance non-contrastive global :star: :star:
Shap Values non-contrastive local :star: :star:
Surrogate Model contrastive global :star: WIP
Counterfactual Example contrastive local :star: :star:

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)

Source

[1] Molnar, Christoph. "Interpretable machine learning. A Guide for Making Black Box Models Explainable", 2019. https://christophm.github.io/interpretable-ml-book/

Authors

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

explainy-0.1.12.tar.gz (1.1 MB view hashes)

Uploaded Source

Built Distribution

explainy-0.1.12-py2.py3-none-any.whl (25.3 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page