Skip to main content

shap-rs: fast SHAP values, written in Rust

PyPI Python tests License: MIT

SHAP values taking too long? Add one line and your existing shap code runs 10-80x faster on XGBoost, LightGBM, CatBoost, random forest and scikit-learn models. Same API, same Explanation objects, same plots, same SHAP values.

import shap
import shap_rs

shap_rs.patch()   # shap's TreeExplainer and KernelExplainer now compute in Rust

explainer = shap.TreeExplainer(model)
shap_values = explainer(X)          # unchanged shap code
shap.plots.beeswarm(shap_values)    # every shap plot still works

shap-rs is a drop-in Rust engine for the shap Python library. It is not a fork and not a rewrite of the API. It replaces only the slow inner loops (TreeSHAP and KernelSHAP), and leaves everything else to shap itself.

Install

pip install shap-rs

Prebuilt wheels for Linux (x86_64, aarch64), macOS (Apple Silicon, Intel) and Windows (x64), for Python 3.12 and 3.13. No Rust toolchain needed.

shap-rs is verified against shap 0.54.1 and installs it for you. On any other shap version, patch() warns and leaves shap untouched.

Benchmarks

Measured on clean GitHub Actions runners. Each row is the same model and data, run through real shap 0.54.1 and through shap-rs. The script is benchmarks/bench.py, and the bench workflow re-runs it on GitHub Actions.

Workload shap 0.54.1 shap-rs Speedup (5-core Mac) Speedup (2-core Linux)
XGBoost, 500 trees, interventional, 1,000 rows 7.6 s 0.09 s 82x 24x
Random forest interaction values, 200 rows 15.3 s 0.35 s 44x 13x
Random forest regressor, 200 trees, depth 12, 500 rows 17.7 s 0.46 s 38x 13x
Random forest classifier, 2,000 rows 7.9 s 0.27 s 30x 9x
LightGBM, 500 trees, interventional, 500 rows 5.7 s 0.20 s 29x 10x
KernelExplainer, logistic regression, 100 rows 2.0 s 1.0 s 2x 3x

Times are from the Mac runner (Apple M2 Pro, 5 cores). shap-rs uses every CPU core, so the gain grows with core count. On a single core it is still 7-28x faster for tree models.

Are the SHAP values the same?

Yes. Every result is checked against real shap:

  • 2,055 randomized test cases (fuzz harness) (random data, random model, random explainer settings) match shap on macOS, Linux and Windows.
  • Path-dependent TreeSHAP and interaction values match to about 1e-13 (floating point noise).
  • The fast interventional path matches to about 6e-8. If you need bit-for-bit output, set SHAP_RS_NO_FAST=1 for the exact mode.
  • Where shap raises an error, shap-rs raises the same error.

Why is SHAP slow?

TreeSHAP walks every tree for every row, and with interventional (background data) explanations it does that again for every background row. A 500-tree XGBoost model with 100 background rows and 1,000 rows to explain means about 50 million tree walks. shap runs much of this on one CPU core.

shap-rs runs the same algorithms in Rust, uses all your cores (via rayon), and uses faster algorithms for the interventional case that give the same values.

What gets faster

TreeExplainer, for scikit-learn decision trees, random forests, extra trees, gradient boosting, HistGradientBoosting and IsolationForest, plus XGBoost, LightGBM and CatBoost:

  • path-dependent SHAP values
  • interventional SHAP values (raw, probability, log_loss)
  • SHAP interaction values
  • approximate=True

KernelExplainer: coalition sampling, expected values, l1 feature selection and the weighted least squares all run in Rust. Your model is called exactly as shap calls it, so for slow models most of the time is still your model.

Not changed (shap's own code runs): Linear, Permutation, Exact, Partition, Deep and GPUTree explainers.

Usage

import shap_rs

shap_rs.patch()        # returns True when patched
shap_rs.is_patched()   # True
shap_rs.unpatch()      # puts back shap's original code exactly

You can also use the standalone classes without patching:

from shap_rs import TreeExplainer, KernelExplainer

shap_values = TreeExplainer(model).shap_values(X)

Full guide: docs/USAGE.md.

FAQ

How do I speed up SHAP for XGBoost or LightGBM? pip install shap-rs, then call shap_rs.patch() before you build your TreeExplainer. Nothing else in your code changes.

Do I need a GPU? No. shap-rs runs on your CPU and uses all of its cores.

Does it work in Jupyter notebooks? Yes. Patch once at the top of the notebook.

Which shap version do I need? shap 0.54.1. The patch depends on shap's internals, so new shap releases need a matching shap-rs release.

Limits

  • Python 3.12 and 3.13 are tested. Python 3.14, PyPy and Windows on ARM are not tested yet.
  • Tied to shap 0.54.1 (see above).
  • shap's progress line for long interventional runs is not printed.

About

shap-rs is built by Atomic Labs. It is an independent project and is not affiliated with the shap maintainers. All credit for SHAP and the shap library goes to Scott Lundberg and the shap contributors.

MIT licensed. Issues and pull requests welcome.

Metadata

Release files for shap-rs 0.1.0

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

Source distribution (sdist)

Source distribution for shap-rs 0.1.0
File Size Uploaded Provenance Actions
shap_rs-0.1.0.tar.gz 84.2 kB Without provenance Details for shap_rs-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for shap-rs 0.1.0
File Actions
shap_rs-0.1.0-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details for shap_rs-0.1.0-cp313-cp313-win_amd64.whl
shap_rs-0.1.0-cp313-cp313-manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.28+ x86-64 Details for shap_rs-0.1.0-cp313-cp313-manylinux_2_28_x86_64.whl
shap_rs-0.1.0-cp313-cp313-manylinux_2_28_aarch64.whl CPython 3.13 CPython 3.13 Linux glibc 2.28+ ARM64 Details for shap_rs-0.1.0-cp313-cp313-manylinux_2_28_aarch64.whl
shap_rs-0.1.0-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details for shap_rs-0.1.0-cp313-cp313-macosx_11_0_arm64.whl
shap_rs-0.1.0-cp313-cp313-macosx_10_12_x86_64.whl CPython 3.13 CPython 3.13 macOS 10.12+ x86-64 Details for shap_rs-0.1.0-cp313-cp313-macosx_10_12_x86_64.whl
shap_rs-0.1.0-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details for shap_rs-0.1.0-cp312-cp312-win_amd64.whl
shap_rs-0.1.0-cp312-cp312-manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ x86-64 Details for shap_rs-0.1.0-cp312-cp312-manylinux_2_28_x86_64.whl
shap_rs-0.1.0-cp312-cp312-manylinux_2_28_aarch64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ ARM64 Details for shap_rs-0.1.0-cp312-cp312-manylinux_2_28_aarch64.whl
shap_rs-0.1.0-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details for shap_rs-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
shap_rs-0.1.0-cp312-cp312-macosx_10_12_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.12+ x86-64 Details for shap_rs-0.1.0-cp312-cp312-macosx_10_12_x86_64.whl

Total release size: 5.7 MB

Release files / shap_rs-0.1.0.tar.gz

Download URL shap_rs-0.1.0.tar.gz
Size 84.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2b1243f656b624e34a8eed838b582bee8c13ec09241311a279213270563b1b1a
BLAKE2b-256 checksum
How to use checksums
d900870bc296a3a2d4049aa71b90d397fa6b922ac76acc8f2b2bbb449eed10d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / shap_rs-0.1.0-cp313-cp313-win_amd64.whl

Download URL shap_rs-0.1.0-cp313-cp313-win_amd64.whl
Size 510.9 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
c27aac2961795578d6b1516fe00caacb0b7a9c6ca61deb9441e6daf6d7ddcb26
BLAKE2b-256 checksum
How to use checksums
bde11b301be30f035ba4d254fb0061e470aa0d0197a590b6edd83cc5dd9f43b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / shap_rs-0.1.0-cp313-cp313-manylinux_2_28_x86_64.whl

Download URL shap_rs-0.1.0-cp313-cp313-manylinux_2_28_x86_64.whl
Size 613.7 kB
Tags CPython 3.13 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
8fe1240e4ba718df2fcb2e69b3aff4589e401e0f7ffcc454b60ddef26cf0b69d
BLAKE2b-256 checksum
How to use checksums
f4c36878f21bfa8609b1d43785cfe74ce0e464528c9a32ebab2e3601e0c8fb03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / shap_rs-0.1.0-cp313-cp313-manylinux_2_28_aarch64.whl

Download URL shap_rs-0.1.0-cp313-cp313-manylinux_2_28_aarch64.whl
Size 578.1 kB
Tags CPython 3.13 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
6619eabac2bd6dbf0e0baf571251232bb0978226262294d4a8299ad179731cbb
BLAKE2b-256 checksum
How to use checksums
d31116eec165949d9578da67006ef892c5317c099d70fc59b39099ccf4f270b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / shap_rs-0.1.0-cp313-cp313-macosx_11_0_arm64.whl

Download URL shap_rs-0.1.0-cp313-cp313-macosx_11_0_arm64.whl
Size 542.0 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
eed07d2124aaa11f89b722175f55688b6393fa3d628e67f4957d5711e25fa068
BLAKE2b-256 checksum
How to use checksums
e721c29b47e8a80e4c2bea7f41a6cb4b4bf6107d12f3fdb6e629d6cd5a4d549b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / shap_rs-0.1.0-cp313-cp313-macosx_10_12_x86_64.whl

Download URL shap_rs-0.1.0-cp313-cp313-macosx_10_12_x86_64.whl
Size 579.5 kB
Tags CPython 3.13 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
16e5d55c704d10c07d31267d67ef4157326cc0b31c670f4130f19b87d052d5cd
BLAKE2b-256 checksum
How to use checksums
0a503808233bd08db3e3505daf718e2772966e0ab632edaf158809e28b7a1eab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / shap_rs-0.1.0-cp312-cp312-win_amd64.whl

Download URL shap_rs-0.1.0-cp312-cp312-win_amd64.whl
Size 510.9 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
7724253ec7ec1515681fb6f89f5587d1fcb838c78562d1c2b52fe08dccffe561
BLAKE2b-256 checksum
How to use checksums
8e1b4f274d3a8fc58d453f48c8e0c51b459d9d8719ccc90558c38770aee880b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / shap_rs-0.1.0-cp312-cp312-manylinux_2_28_x86_64.whl

Download URL shap_rs-0.1.0-cp312-cp312-manylinux_2_28_x86_64.whl
Size 613.8 kB
Tags CPython 3.12 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
b7044ba3c4c9499c187b2ea50d0cc4178b322845243f487e77f380ce8a427e13
BLAKE2b-256 checksum
How to use checksums
0cf6dc399c4249bc8310a518578b9307c50ab57e1643573853058e5358d369e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / shap_rs-0.1.0-cp312-cp312-manylinux_2_28_aarch64.whl

Download URL shap_rs-0.1.0-cp312-cp312-manylinux_2_28_aarch64.whl
Size 578.1 kB
Tags CPython 3.12 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
858ef3c3462e57df43d2890b853ec14835877fb66ca592eac383f1e1679eba9b
BLAKE2b-256 checksum
How to use checksums
35c63ddb99e6746e95742effc1344c98414738f6bbd568685c0d6642eb8735d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / shap_rs-0.1.0-cp312-cp312-macosx_11_0_arm64.whl

Download URL shap_rs-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Size 542.1 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ad47eeee1e337848711040cadf199e2761a33720c7cbf128aaa61d25c2572dd4
BLAKE2b-256 checksum
How to use checksums
ff09f06fe81c822e898aac3b48bce918ff0cd02147effd93523338e7e664b145
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / shap_rs-0.1.0-cp312-cp312-macosx_10_12_x86_64.whl

Download URL shap_rs-0.1.0-cp312-cp312-macosx_10_12_x86_64.whl
Size 579.5 kB
Tags CPython 3.12 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
323abe1ae2be8c805e3da422d541eeb9a0bd0887733adfb814dd18e57d0dfc8c
BLAKE2b-256 checksum
How to use checksums
fc38a81dceaa885e626516acca4890f881372df87abc00b9a4b5271c089b3433
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for shap-rs 0.1.0

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

11 release files No provenance

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