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

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.

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

Documentation

https://explainy.readthedocs.io

Install explainy

pip install explainy

Usage

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)
from explainy.explanation.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(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).

explainer.plot()

Permutation Feature Importance

explainer.save(sample_index)

Model Explanations

Method Type Explanations Classification Regression
Permutation Feature Importance Non-contrastive global :star: :star:
Shapley Values Non-contrastive local :star: :star:
Global Surrogate Model Contrastive global :star: :star:
Counterfactual Example Contrastive local :star: :star:

Description

  • global:
  • local:
  • contrastive:
  • non-contrastive:

Features

TODO

Explanations

Permutation Feature Importance

Permutation feature importance measures the increase in the prediction error of the model after we permuted the feature's values, which breaks the relationship between the feature and the true outcome [1].

Characteristics

  • global
  • non-contrastive

Permutation Feature Importance

Shapley Values

A prediction can be explained by assuming that each feature value of the instance is a "player" in a game where the prediction is the payout. Shapley values (a method from coalitional game theory) tells us how to fairly distribute the "payout" among the features. The Shapley value is the average marginal contribution of a feature value across all possible coalitions [1].

Characteristics

  • local
  • non-contrastive

Shapley Values

Counterfactual explanations

Counterfactual explanations tell us how the values of an instance have to change to significantly change its prediction. A counterfactual explanation of a prediction describes the smallest change to the feature values that changes the prediction to a predefined output. By creating counterfactual instances, we learn about how the model makes its predictions and can explain individual predictions [1].

Characteristics

  • local
  • contrastive

Counterfactual Example

Global Surrogate Model (Decision Tree)

A global surrogate model is an interpretable model that is trained to approximate the predictions of a black box model. We can draw conclusions about the black box model by interpreting the surrogate model [1].

Characteristics

  • global
  • contrastive

Global Surrogate Model

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.8.tar.gz (286.0 kB view hashes)

Uploaded Source

Built Distribution

explainy-0.1.8-py2.py3-none-any.whl (22.2 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