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.1.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.1-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.1.tar.gz.

File metadata

  • Download URL: treeshap-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 cc26d1499cdb965904023e9cd2a0d8813331b20deed333f32754b88f36665c89
MD5 7ed3d5463e7dcb3024413731cffd61a8
BLAKE2b-256 d2fe36f35d7d9fd14641effc63ebc5382d659f799088df855a43085b8c40ff78

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for treeshap-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dd8ce7f716af77de1df7ac0c2d20589c8409e58acdbaad68032e856e79eafe00
MD5 228f5210daab719876cf7e5c59af0521
BLAKE2b-256 318410dc0b191deea093112aeb085c4bf7dbec9824462f9c793fd5c0837e21e9

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