Skip to main content

shaply

Tests codecov PyPI version Python versions PyPI downloads License Docs

beeswarm_ranges

Usual SHAP explainability figures, rendered as interactive Plotly charts.

shaply reproduces the familiar figures from the shap library - bar, beeswarm, waterfall, dependence (scatter) and heatmap - but returns plotly.graph_objects.Figure objects instead of matplotlib axes, so the plots are interactive and embeddable out of the box.

It does not depend on shap: every plotting function accepts a shap.Explanation-like object, a raw NumPy array of SHAP values, or a pandas DataFrame.

Install

uv add shaply
# or
pip install shaply

Optional dependencies (extras)

shaply itself only needs numpy, plotly and pydantic. Extra features and the example notebook pull in heavier packages, grouped as installable extras:

Extra Installs Purpose
pandas pandas Pass SHAP values as aDataFrame (column names become feature names)
examples scikit-learn, xgboost, lightgbm, shap, pandas, ipykernel, nbformat Everything needed to runexamples/shaply_demo.ipynb
uv add "shaply[pandas]"       # DataFrame support
uv add "shaply[examples]"     # run the demo notebook
# or with pip
pip install "shaply[examples]"

Building the wheel from source (use the package without PyPI)

To build and install shaply straight from a clone of this repository, without going through PyPI:

git clone https://github.com/antoine126/shaply.git
cd shaply
uv build --wheel     # produces dist/shaply-<version>-py3-none-any.whl

Then install the wheel wherever you need it:

uv add /path/to/shaply/dist/shaply-<version>-py3-none-any.whl
# or with pip, in any environment
pip install /path/to/shaply/dist/shaply-<version>-py3-none-any.whl

The wheel is self-contained (it ships py.typed, so type annotations reach the installed package) and does not require uv or the source tree at runtime.

Quick start

import shaply

# `explanation` can be a shap.Explanation, an ndarray, or a DataFrame
fig = shaply.beeswarm(explanation)
fig.show()

fig = shaply.bar(explanation)
fig = shaply.waterfall(explanation, sample_index=0)
fig = shaply.scatter(explanation, feature="income", color_feature="age")
fig = shaply.heatmap(explanation)

Every function takes an optional typed config from shaply.config:

from shaply.config import BeeswarmConfig
from shaply.enums import ColorScale, FeatureOrdering

cfg = BeeswarmConfig(
    max_display=15,
    ordering=FeatureOrdering.IMPORTANCE,
    color_scale=ColorScale.RED_BLUE,
)
fig = shaply.beeswarm(explanation, config=cfg)

Available plots

Function SHAP equivalent Purpose
shaply.bar shap.plots.bar Global feature importance
shaply.beeswarm shap.plots.beeswarm Summary of per-sample contributions
shaply.waterfall shap.plots.waterfall Single-prediction explanation
shaply.scatter shap.plots.scatter Dependence plot
shaply.heatmap shap.plots.heatmap SHAP values across instances
shaply.force shap.plots.force Additive force layout (one instance)
shaply.decision shap.decision_plot Cumulative decision paths

Advanced tools - beyond the usual SHAP plots

These are shaply-only figures aimed at engineers and business-facing data scientists who want to act on SHAP, not just explain a model. They cross SHAP values with the real data to surface operating ranges, tipping points, coupled effects and failure drivers.

Read them as associational, not causal. SHAP measures a feature's contribution to the model's output, not to reality. Wording is deliberately cautious ("associated with", "tipping point of the model") - a strong signal here is a lead to investigate, not a proven cause.

Function What it shows Insight
shaply.beeswarm_ranges Beeswarm**+** real value distribution (violin + box, true min/max) per feature Read impactand concrete operating range on the same line
shaply.scatter_ranges Dependence scatter for one feature, framed byboth axes' distributions (box + violin each) Read the relationshipand both distributions without a second figure
shaply.response_curve Smoothed mean SHAP vs a feature's value, with a ±1 std band and auto-detected zero-crossings The tipping point where a feature flips from lowering to raising the output
shaply.interaction_heatmap Matrix of mean|SHAP interaction| between feature pairs Which featuresact together (coupled effects), diagonal hidden by default
shaply.error_analysis Mean SHAP per feature,correct vs mis-predicted cohorts, ranked by gap What the model relies on differentlywhen it is wrong
shaply.shap_surface Mean SHAP of a feature over the 2D plane of two features Theoperating regions where a feature helps or hurts, and how a second one modulates it
shaply.importance_by_cohort mean(|SHAP|) per feature, split by cohort (explicit or quantile-binned) A feature candominate in one regime and be negligible in another
shaply.feature_clustering Clustered heatmap of SHAP correlation between features Redundant features (correlated SHAP) that could be dropped
shaply.explanation_archetypes Mean SHAP profile of each k-means cluster of instances The model's recurringdecision patterns / failure modes
shaply.importance_ci Global importance bars withbootstrap confidence intervals Whether an importance ranking isrobust or fragile
shaply.monotonicity_check Spearman correlation between each feature's value and its SHAP Cleanmonotonic effects vs suspicious non-monotonic ones (interaction/noise)
# Beeswarm + real value ranges (needs feature values via data=...)
shaply.beeswarm_ranges(explanation).show()

# Dependence scatter for one feature, framed by both axes' distributions
shaply.scatter_ranges(explanation, "temperature").show()

# Response curve of one feature, with tipping-point detection
shaply.response_curve(explanation, "temperature").show()

# Pairwise interaction strength (needs SHAP *interaction* values)
inter = shap.TreeExplainer(model).shap_interaction_values(X)  # (n, f, f) - pick a class if 4D
shaply.interaction_heatmap(inter, feature_names=list(X.columns)).show()

# What drives the model's mistakes
shaply.error_analysis(explanation, y_true=y_test, y_pred=model.predict(X_test)).show()

# 2D SHAP surface over a feature pair
shaply.shap_surface(explanation, "temperature", "pressure").show()

# Importance split by an operating regime (quantiles of another feature)
shaply.importance_by_cohort(explanation, by_feature="load").show()

# Redundant features (correlated SHAP), and typical decision patterns
shaply.feature_clustering(explanation).show()
shaply.explanation_archetypes(explanation).show()

# Robustness of the ranking, and monotonicity of each effect
shaply.importance_ci(explanation).show()
shaply.monotonicity_check(explanation).show()

Each takes a typed config from shaply.config (e.g. ResponseCurveConfig, ShapSurfaceConfig, ImportanceByCohortConfig, FeatureClusteringConfig, ExplanationArchetypesConfig, ImportanceCIConfig, MonotonicityConfig).

The clustering and statistics behind these tools are implemented in pure NumPy, so the advanced tools add no runtime dependency beyond numpy/plotly/pydantic.

Example notebook

examples/shaply_demo.ipynb is a full, executed walkthrough. It builds a synthetic dataset with make_classification (5 informative, 2 redundant and 3 pure-noise features), trains five very different classifiers - RandomForest, XGBoost, LightGBM, LogisticRegression and an RBF SVM - computes SHAP values for each (TreeExplainer, LinearExplainer, KernelExplainer) and renders every shaply figure for all of them, plus the advanced tools (response curve, interaction heatmap, error analysis) and a cross-model importance comparison.

uv sync --extra examples
uv run jupyter lab examples/shaply_demo.ipynb
# regenerate the executed outputs from scratch:
uv run jupyter nbconvert --to notebook --execute --inplace examples/shaply_demo.ipynb

Development

uv sync
uv run ruff check . --fix
uv run ruff format .
uv run mypy
uv run pytest

The package ships a py.typed marker, so all type annotations are available to downstream users.

License

MIT

Download files

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

Source Distribution

shaply-1.1.0.tar.gz (36.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shaply-1.1.0-py3-none-any.whl (60.0 kB view details)

Uploaded Python 3

File details

Details for the file shaply-1.1.0.tar.gz.

File metadata

  • Download URL: shaply-1.1.0.tar.gz
  • Upload date:
  • Size: 36.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.8

File hashes

Hashes for shaply-1.1.0.tar.gz
Algorithm Hash digest
SHA256 e15ed5347f1070ba6fb4c0c3e4afe95a27fec2ac1215e106ce8d91e9161931b2
MD5 c0f5a2e03fc538355e46523a8eccd2c9
BLAKE2b-256 458df2766bc097523f55e426afb2ccddb0df05ecfcc823f2497e4d86221497ab

See more details on using hashes here.

File details

Details for the file shaply-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: shaply-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 60.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.8

File hashes

Hashes for shaply-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 965864d0ab8c33833ba9d9d21e26686049d72a8cf0c98a694a78c6a1168ee15b
MD5 bdee7979154d59e1dd929804188a0221
BLAKE2b-256 146a57d9ff15749809cb63e6f946391c3c00d3d2eaf147b0eadcdfdc0f3d79ce

See more details on using hashes here.

Release history Release notifications | RSS feed

1.1.1

2 files

This release

1.1.0 This release

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 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