Skip to main content

Log and load SHAP explainers as MLflow artifacts with a one-liner.

Project description

mlflow-shap

PyPI version Python versions License: MIT

Log and load SHAP explainers as MLflow artifacts with a one-liner. Drop-in for any model that shap.Explainer accepts (tree models, linear models, deep nets, etc.).


Installation

pip install mlflow-shap

Quick start

Log an explainer

import mlflow
from sklearn.ensemble import RandomForestClassifier
from mlflow_shap import log_explainer

with mlflow.start_run() as run:
    model = RandomForestClassifier().fit(X_train, y_train)
    mlflow.sklearn.log_model(model, artifact_path="model")
    log_explainer(model, X_train)

Load it back

from mlflow_shap import load_explainer

explainer = load_explainer(run_id="abc123")
shap_values = explainer(X_test)

That's it. The explainer is serialized with joblib and stored under the run's shap_explainer/ artifact directory.


API

log_explainer

log_explainer(
    model,
    data,
    *,
    explainer=None,
    artifact_path="shap_explainer",
    file_name="shap_explainer.pkl",
    explainer_kwargs=None,
    run_id=None,
) -> str

Returns the artifact URI (runs:/<id>/<artifact_path>/<file_name>).

Argument Description
model Trained model. Ignored when explainer is supplied.
data Background data passed to shap.Explainer.
explainer Pre-built shap.Explainer (e.g. TreeExplainer, DeepExplainer). Skips auto-construction.
artifact_path Sub-directory inside the run's artifact root. Default shap_explainer.
file_name Pickle file name. Default shap_explainer.pkl.
explainer_kwargs Extra kwargs forwarded to shap.Explainer(...).
run_id Target run. Default uses the active run.

load_explainer

load_explainer(
    run_id,
    artifact_path="shap_explainer/shap_explainer.pkl",
    *,
    dst_path=None,
) -> shap.Explainer

Downloads the artifact and deserializes via joblib.


Advanced usage

Use a specific explainer

import shap
from mlflow_shap import log_explainer

tree_explainer = shap.TreeExplainer(model)
log_explainer(model, X_train, explainer=tree_explainer)

Log into a specific run (no active run)

log_explainer(model, X_train, run_id="abc123")

Custom artifact location

log_explainer(model, X_train, artifact_path="explainability/shap", file_name="rf_v2.pkl")

Exceptions

Exception When raised
NoActiveRunError No active MLflow run and no run_id was passed.
ExplainerCreationError shap.Explainer(model, data) failed. Wraps the original error.
MLflowSHAPError Base class for the above.

Compatibility

  • Python: 3.9, 3.10, 3.11, 3.12
  • MLflow: >= 2.0
  • SHAP: >= 0.42

Development

git clone https://github.com/princepongsakorn/mlflow-shap.git
cd mlflow-shap
pip install -e ".[dev]"
pytest

License

MIT — see LICENSE.

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

mlflow_shap-0.1.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

mlflow_shap-0.1.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file mlflow_shap-0.1.1.tar.gz.

File metadata

  • Download URL: mlflow_shap-0.1.1.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mlflow_shap-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ab841c6850732cbc9dcb3e8a2f321bc693d4d41cc0de1a79312807667a1a103d
MD5 7235e2779aaf8feb4adcd51ee90dfa2e
BLAKE2b-256 0ce8731a19a6e7de050f95a57087d2ed8f682255541b7f636d5e67c4368095fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlflow_shap-0.1.1.tar.gz:

Publisher: publish.yml on princepongsakorn/mlflow-shap

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mlflow_shap-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mlflow_shap-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mlflow_shap-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 86a84b2201dddbe9b14219a786b7212b64ff7ccb418709aa054c74fb6ed7d7b7
MD5 026a170a39818217ce0a8df4e77c3976
BLAKE2b-256 204c6278c208e3f3abb3f00f0e72fb5728c354ba8611b8586d5f054089012376

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlflow_shap-0.1.1-py3-none-any.whl:

Publisher: publish.yml on princepongsakorn/mlflow-shap

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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