streamlit-shap
This component provides a wrapper to display SHAP plots in Streamlit.
Installation
First install Streamlit (of course!) then pip install this library:
pip install streamlit
pip install streamlit-shap
Example
import streamlit as st
from streamlit_shap import st_shap
import shap
from sklearn.model_selection import train_test_split
import xgboost
import numpy as np
import pandas as pd
@st.experimental_memo
def load_data():
return shap.datasets.adult()
@st.experimental_memo
def load_model(X, y):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
d_train = xgboost.DMatrix(X_train, label=y_train)
d_test = xgboost.DMatrix(X_test, label=y_test)
params = {
"eta": 0.01,
"objective": "binary:logistic",
"subsample": 0.5,
"base_score": np.mean(y_train),
"eval_metric": "logloss",
"n_jobs": -1,
}
model = xgboost.train(params, d_train, 10, evals = [(d_test, "test")], verbose_eval=100, early_stopping_rounds=20)
return model
st.title("SHAP in Streamlit")
# train XGBoost model
X,y = load_data()
X_display,y_display = shap.datasets.adult(display=True)
model = load_model(X, y)
# compute SHAP values
explainer = shap.Explainer(model, X)
shap_values = explainer(X)
st_shap(shap.plots.waterfall(shap_values[0]), height=300)
st_shap(shap.plots.beeswarm(shap_values), height=300)
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X)
st_shap(shap.force_plot(explainer.expected_value, shap_values[0,:], X_display.iloc[0,:]), height=200, width=1000)
st_shap(shap.force_plot(explainer.expected_value, shap_values[:1000,:], X_display.iloc[:1000,:]), height=400, width=1000)
Notes
Colorbar changes in matplotlib>3.4.3 introduced bugs (#22625, #22087) that cause the colorbar of certain shap plots (e.g. beeswarm) to not display properly. If colorbars are not displayed properly, try downgrading matplotlib to 3.4.3.
Metadata
Release files for streamlit-shap 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| streamlit-shap-1.0.2.tar.gz | 4.4 kB | Without provenance | Details for streamlit-shap-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| streamlit_shap-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for streamlit_shap-1.0.2-py3-none-any.whl |
Total release size: 9.1 kB
Release files / streamlit-shap-1.0.2.tar.gz
| Download URL | streamlit-shap-1.0.2.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d48d4b0ee54c8576cc942949e18416b288bd6ac791051cc55345fe125fec1ab3
|
|
BLAKE2b-256 checksum How to use checksums |
a405a7e8543ebc2e1ea47995ba2eb58de8cfa06152c779bf0c03508b24a6d97f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.10
|
Release files / streamlit_shap-1.0.2-py3-none-any.whl
| Download URL | streamlit_shap-1.0.2-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1762aca13393bab8a98eececd07985d9f9ab53b8866d48fb81186ce123bde7d2
|
|
BLAKE2b-256 checksum How to use checksums |
a55a1481baf35b7c86010196ce418f3c07c05e95c8e48c1824668d4b82262a75
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.10
|
Security for streamlit-shap 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.