Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.


example workflow

Baby Shap is a stripped and opiniated version of SHAP (SHapley Additive exPlanations), a game theoretic approach to explain the output of any machine learning model by Scott Lundberg. It connects optimal credit allocation with local explanations using the classic Shapley values from game theory and their related extensions (see papers for details and citations).

Baby Shap solely implements and maintains the Linear and Kernel Explainer and a limited range of plots, while limiting the number of dependencies, conflicts and raised warnings and errors.

Install

Baby SHAP can be installed from either PyPI:

pip install baby-shap

Model agnostic example with KernelExplainer (explains any function)

Kernel SHAP uses a specially-weighted local linear regression to estimate SHAP values for any model. Below is a simple example for explaining a multi-class SVM on the classic iris dataset.

import baby_shap
from sklearn import datasets, svm, model_selection

# print the JS visualization code to the notebook
baby_shap.initjs()

# train a SVM classifier
d = datasets.load_iris()
X = pd.DataFrame(data=d.data, columns=d.feature_names)
y = d.target

X_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, y, test_size=0.2, random_state=0)
clf = svm.SVC(kernel='rbf', probability=True)
clf.fit(X_train.to_numpy(), Y_train)

# use Kernel SHAP to explain test set predictions
explainer = baby_shap.KernelExplainer(svm.predict_proba, X_train, link="logit")
shap_values = explainer.shap_values(X_test, nsamples=100)

# plot the SHAP values for the Setosa output of the first instance
baby_shap.force_plot(explainer.expected_value[0], shap_values[0][0,:], X_test.iloc[0,:], link="logit")

The above explanation shows four features each contributing to push the model output from the base value (the average model output over the training dataset we passed) towards zero. If there were any features pushing the class label higher they would be shown in red.

If we take many explanations such as the one shown above, rotate them 90 degrees, and then stack them horizontally, we can see explanations for an entire dataset. This is exactly what we do below for all the examples in the iris test set:

# plot the SHAP values for the Setosa output of all instances
baby_shap.force_plot(explainer.expected_value[0], shap_values[0], X_test, link="logit")

Metadata

Release files for baby-shap 0.0.6

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

Source distribution (sdist)

Source distribution for baby-shap 0.0.6
File Size Uploaded
baby_shap-0.0.6.tar.gz 182.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for baby-shap 0.0.6
File Interpreter ABI Platform
baby_shap-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 373.5 kB

Release files / baby_shap-0.0.6.tar.gz

Download URL baby_shap-0.0.6.tar.gz
Size 182.9 kB
Tags Source
SHA-256 checksum
How to use checksums
76f7b24d5807ed88e34dd5d63dd6445d25b5b1a44541b07c3c80381a6acdf8a8
BLAKE2b-256 checksum
How to use checksums
f76b8499710ecf5543ed7e6a0c662d22fca05aba579bf1da5838d8bedad4289e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.10.9 Darwin/22.3.0

Release files / baby_shap-0.0.6-py3-none-any.whl

Download URL baby_shap-0.0.6-py3-none-any.whl
Size 190.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a4e4c58d5ccae5117dbbb057ab01741f14604916988fd4b0c4e8fbbf9f46aa96
BLAKE2b-256 checksum
How to use checksums
80b7ab881676900cf15d4f543a7f63a460e597b3efd9c984a19dfc9bcadbf4b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.10.9 Darwin/22.3.0

Release history Release notifications | RSS feed

This release

0.0.6 This release

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