Sunday, November 30, 2025

How to perform SHAP integration with MLFlow ?

SHAP Integration

MLflow's built-in SHAP integration provides automatic model explanations and feature importance analysis during evaluation. SHAP (SHapley Additive exPlanations) values help you understand what drives your model's predictions, making your ML models more interpretable and trustworthy.


Quick Start: Automatic SHAP Explanations

Enable SHAP explanations during model evaluation with a simple configuration:



import mlflow

import xgboost as xgb

import shap

from sklearn.model_selection import train_test_split

from mlflow.models import infer_signature


# Load the UCI Adult Dataset

X, y = shap.datasets.adult()

X_train, X_test, y_train, y_test = train_test_split(

    X, y, test_size=0.33, random_state=42

)


# Train model

model = xgb.XGBClassifier().fit(X_train, y_train)


# Create evaluation dataset

eval_data = X_test.copy()

eval_data["label"] = y_test


with mlflow.start_run():

    # Log model

    signature = infer_signature(X_test, model.predict(X_test))

    model_info = mlflow.sklearn.log_model(model, name="model", signature=signature)


    # Evaluate with SHAP explanations enabled

    result = mlflow.evaluate(

        model_info.model_uri,

        eval_data,

        targets="label",

        model_type="classifier",

        evaluators=["default"],

        evaluator_config={"log_explainer": True},  # Enable SHAP logging

    )


    print("SHAP artifacts generated:")

    for artifact_name in result.artifacts:

        if "shap" in artifact_name.lower():

            print(f"  - {artifact_name}")


This automatically generates:


Feature importance plots showing which features matter most

SHAP summary plots displaying feature impact distributions

SHAP explainer model saved for future use on new data

Individual prediction explanations for sample predictions


No comments:

Post a Comment