Log and load SHAP explainers as MLflow artifacts with a one-liner.
Project description
mlflow-shap
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab841c6850732cbc9dcb3e8a2f321bc693d4d41cc0de1a79312807667a1a103d
|
|
| MD5 |
7235e2779aaf8feb4adcd51ee90dfa2e
|
|
| BLAKE2b-256 |
0ce8731a19a6e7de050f95a57087d2ed8f682255541b7f636d5e67c4368095fe
|
Provenance
The following attestation bundles were made for mlflow_shap-0.1.1.tar.gz:
Publisher:
publish.yml on princepongsakorn/mlflow-shap
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mlflow_shap-0.1.1.tar.gz -
Subject digest:
ab841c6850732cbc9dcb3e8a2f321bc693d4d41cc0de1a79312807667a1a103d - Sigstore transparency entry: 1503609315
- Sigstore integration time:
-
Permalink:
princepongsakorn/mlflow-shap@edc1391272885efecddb48ee2131470ca303b6e2 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/princepongsakorn
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@edc1391272885efecddb48ee2131470ca303b6e2 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86a84b2201dddbe9b14219a786b7212b64ff7ccb418709aa054c74fb6ed7d7b7
|
|
| MD5 |
026a170a39818217ce0a8df4e77c3976
|
|
| BLAKE2b-256 |
204c6278c208e3f3abb3f00f0e72fb5728c354ba8611b8586d5f054089012376
|
Provenance
The following attestation bundles were made for mlflow_shap-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on princepongsakorn/mlflow-shap
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mlflow_shap-0.1.1-py3-none-any.whl -
Subject digest:
86a84b2201dddbe9b14219a786b7212b64ff7ccb418709aa054c74fb6ed7d7b7 - Sigstore transparency entry: 1503609867
- Sigstore integration time:
-
Permalink:
princepongsakorn/mlflow-shap@edc1391272885efecddb48ee2131470ca303b6e2 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/princepongsakorn
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@edc1391272885efecddb48ee2131470ca303b6e2 -
Trigger Event:
push
-
Statement type: