Skip to main content

treeshap

Exact TreeSHAP in Rust with Python bindings. Computes Shapley values for XGBoost, LightGBM, and ONNX tree ensemble models.

Installation

pip install treeshap

Pre-built wheels are available for Linux (x86_64, aarch64), macOS (x86_64, Apple Silicon), and Windows (x86_64). Python 3.8+ required.

Usage

import numpy as np
from treeshap import TreeEnsemble, ShapExplainer

# Load a model
model = TreeEnsemble.from_file("model.json", "xgboost")

# Explain predictions
explainer = ShapExplainer(model)
explanation = explainer.explain(X)  # X is a numpy array

# Access results
print(explanation.shap_values)   # numpy array (n_samples, n_features)
print(explanation.base_value)    # float or list[float]
print(explanation.predictions)   # numpy array (n_samples,)

# Verify local accuracy
report = explanation.verify()
assert report.is_pass

# Generate plots (returns SVG bytes)
svg = explanation.plot_waterfall(sample_index=0)
svg = explanation.plot_beeswarm()
svg = explanation.plot_importance()

Supported Formats

Format Loader Example
XGBoost JSON TreeEnsemble.from_file(path, "xgboost") booster.save_model("model.json")
LightGBM text TreeEnsemble.from_file(path, "lightgbm") booster.save_model("model.txt")
ONNX TreeEnsemble.from_file(path, "onnx") TreeEnsembleRegressor/Classifier

Performance

Sub-millisecond single-sample latency. 10,000 samples with 100 trees at depth 6 in 2.8 seconds on Apple M3. GIL is released during computation for full multi-core utilization.

License

MIT OR Apache-2.0

Metadata

Release files for treeshap 0.1.2

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

Source distribution (sdist)

Source distribution for treeshap 0.1.2
File Size Uploaded
treeshap-0.1.2.tar.gz 62.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for treeshap 0.1.2
File Interpreter ABI Platform
treeshap-0.1.2-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details

Total release size: 505.0 kB

Release files / treeshap-0.1.2.tar.gz

Download URL treeshap-0.1.2.tar.gz
Size 62.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8fa6e1b0ae65344454d65f2d89fa87472778d9a69973df5011e567e45012e5f0
BLAKE2b-256 checksum
How to use checksums
a04a21fd896cd655fdc53706860f9bd9225fcedb14738426a80427b437852804
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.12.6

Release files / treeshap-0.1.2-cp312-cp312-macosx_11_0_arm64.whl

Download URL treeshap-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
Size 442.6 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
bc17383d037158573e753a4074dab437eef7bc7862acdfca5b65ae63c998d171
BLAKE2b-256 checksum
How to use checksums
3ee4f327e26d969d0318826fc40f598c3241c5b108d2413b479228df710b9cca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.12.6

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

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