Skip to main content

Exact TreeSHAP in Rust — fast Shapley values for XGBoost, LightGBM, and ONNX tree ensembles

Project description

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

Project details


Download files

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

Source Distribution

treeshap-0.1.2.tar.gz (62.4 kB view details)

Uploaded Source

Built Distribution

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

treeshap-0.1.2-cp312-cp312-macosx_11_0_arm64.whl (442.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file treeshap-0.1.2.tar.gz.

File metadata

  • Download URL: treeshap-0.1.2.tar.gz
  • Upload date:
  • Size: 62.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for treeshap-0.1.2.tar.gz
Algorithm Hash digest
SHA256 8fa6e1b0ae65344454d65f2d89fa87472778d9a69973df5011e567e45012e5f0
MD5 e02a79c03a0c6f9e52c64edd50d48e3f
BLAKE2b-256 a04a21fd896cd655fdc53706860f9bd9225fcedb14738426a80427b437852804

See more details on using hashes here.

File details

Details for the file treeshap-0.1.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for treeshap-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bc17383d037158573e753a4074dab437eef7bc7862acdfca5b65ae63c998d171
MD5 c56108d1245cfe262b2e2a72d75f3d92
BLAKE2b-256 3ee4f327e26d969d0318826fc40f598c3241c5b108d2413b479228df710b9cca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page