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
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()
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 |
---|---|---|---|---|
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
- Mauro Luzzatto - Maurol
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for explainy-0.1.12-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e39d07bc75f5f0869201d85f61e99f210a93ac32e94ce2845f378e925b8f830 |
|
MD5 | f26b033175de71ed4897650ca3bed4bd |
|
BLAKE2b-256 | 4c76cb672bf2750a9da97e3b81275d33fb82c048b25ec1a3aed8687b8682ee07 |