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.0.tar.gz (62.3 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.0-cp312-cp312-macosx_11_0_arm64.whl (444.1 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for treeshap-0.1.0.tar.gz
Algorithm Hash digest
SHA256 245e0c8dab8d8d80fa1e2346d2a7753ef46c5dd194df677bab8fc412e116e5b5
MD5 b0b3c02c5ac72f5dc5cb8baa377afb5d
BLAKE2b-256 c3dc91ac47d96aab4356d20ca82b1de90b43131549964a825776ca49bbb05da6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for treeshap-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1b7c76c9d9190f581d0820f5e9fde8bd26a4dcd1cdf2103352a024fcbe7a940c
MD5 e2ddd68fe0d64000935179b1e821a67a
BLAKE2b-256 24f85d40a853b2e28d0b7ba2fd724d6bd61eba3d5ea0e3c48d2171ce3fdb426a

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