Skip to main content

Log explainable ML models (predictor + SHAP explainer) to MLflow as a single self-contained pyfunc artifact.

Project description

mlflow-explainable

Log explainable ML models (predictor + SHAP explainer) to MLflow as a single self-contained mlflow.pyfunc artifact. Designed for a multi-model serving runtime that should not need to know whether the underlying model is a sklearn estimator, an XGBoost booster, or a custom torch network.

Installation

pip install mlflow-explainable

Quick start

sklearn-style model

import mlflow
from sklearn.ensemble import RandomForestClassifier
from mlflow_explainable import log_explainable_model

model = RandomForestClassifier().fit(X_train, y_train)

with mlflow.start_run():
    mlflow.log_metric("accuracy", acc)
    log_explainable_model(
        model,
        X_train,
        registered_name="sample-rf-crc",
    )

Gradient Boosting (sklearn)

from sklearn.ensemble import GradientBoostingClassifier
from mlflow_explainable import log_explainable_model

model = GradientBoostingClassifier().fit(X_train, y_train)
with mlflow.start_run():
    log_explainable_model(model, X_train, registered_name="sample-gb-crc")

XGBoost

import xgboost as xgb
from mlflow_explainable import log_explainable_model

model = xgb.XGBClassifier(eval_metric="logloss").fit(X_train, y_train)
with mlflow.start_run():
    log_explainable_model(
        model,
        X_train,
        registered_name="sample-xgboost-crc",
        extra_pip_requirements=["xgboost"],
    )

Note: as of shap 0.49 the meta-Explainer no longer auto-detects XGBClassifier. log_explainable_model transparently retries with model.predict_proba when this happens, so the user-facing API stays the same.

Custom torch model (or any callable wrapper)

from mlflow_explainable import log_explainable_model

wrapper = GCNTabularWrapper(gcn, edge_index, device, feature_names)

with mlflow.start_run():
    mlflow.log_metric("accuracy", acc)
    log_explainable_model(
        wrapper,
        X_train,
        registered_name="sample-gcn-crc",
        explainer_kwargs={"algorithm": "permutation"},
        extra_pip_requirements=["torch", "torch_geometric"],
    )

The library walks the predictor's object graph, collects source files of any user-defined classes (skipping stdlib and well-known third-party prefixes), and packs them into the artifact via MLflow's code_path. The serving runtime never needs to import those classes from its own codebase.

Loading at serving time

loaded = mlflow.pyfunc.load_model(model_uri)
impl   = loaded._model_impl.python_model    # ExplainableModel instance

# 1) standard pyfunc predict — returns DataFrame[Y_proba, Y_class]
result = loaded.predict(X)

# 2) SHAP explanation — uniform shape across all explainer types
explanation = impl.shap_explain(X)
# explanation["values"]      shape (n, n_features) or (n, n_features, n_classes)
# explanation["base_values"] shape ()              or (n_classes,) or (n, n_classes)
# explanation["data"]        shape (n, n_features)

Why a contract?

mlflow.sklearn.load_model ties the runtime to the sklearn API. Adding a torch model means another branch, another set of attribute assumptions (predict_proba, expected_value, ...), and another way for kserve to break when the SHAP version changes.

mlflow-explainable standardises the runtime-facing surface to two methods — predict and shap_explain — and pushes all framework-specific glue into the artifact itself.

License

MIT

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_explainable-1.0.0.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

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

mlflow_explainable-1.0.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file mlflow_explainable-1.0.0.tar.gz.

File metadata

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

File hashes

Hashes for mlflow_explainable-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0bc220cb05c48ad430db001eb048d5d01c540e195dab24b2f442610686db5dad
MD5 02cab50aefef99d2d2368a1c13436f2d
BLAKE2b-256 045cfe258aa274a05183758f3c4089a3b476375f1c6331f4c7a2156962146da1

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlflow_explainable-1.0.0.tar.gz:

Publisher: publish.yml on princepongsakorn/mlflow-explainable

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_explainable-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mlflow_explainable-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d9e4fd20b9ee4d5985bda50bfad463c4baf9baa2522756fbb0251ebeabef11e1
MD5 0e315299c0da188eaecfc0f5631f6670
BLAKE2b-256 0c15177a0cee21721773e644ad649bfb4434c9348d43ad18398be8997bd4a4dc

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlflow_explainable-1.0.0-py3-none-any.whl:

Publisher: publish.yml on princepongsakorn/mlflow-explainable

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