[Python] How to Get Feature Attribution Explanations for Two-Class Logistic Regression...

Discussão em 'Python' iniciado por Stack, Julho 29, 2025.

  1. Stack

    Stack Membro Participativo

    Hello all,


    I am working on a lead scoring task in Azure Machine Learning Studio and need help integrating model interpretability into my deployed solution. Please find the details below:

    Background


    • Objective:
      Train a model on historical leads data, predict outcomes (Won/Lost) for new leads, and generate human-readable explanations for each prediction (i.e., explain why a lead is classified as won or lost, highlighting the most influential attributes).


    • Environment:
      Azure Machine Learning Studio (using the Designer interface).


    • Workflow:
      • Imported data and trained a model using the following Designer components:
        • Two-class logistic regression
        • Train Model
        • Score Model
        • Evaluate Model
      • Successfully deployed the model as a web service; I can call the API and get predictions for new lead data.
    Problem


    • I am unable to get feature-level explanations for predictions through the web API.


    • I tried to use the model interpretability features in Azure ML:

      • Approach 1:
        Wrote a Python script in a notebook to use the explainer on the trained model from Designer, but encountered an error:

        NotADirectoryError: [Errno 20] Not a directory: 'model.pkl/model.pkl'

      • Approach 2:
        Tried registering the trained model programmatically with local data and running the explainer, but encountered another error at the registration/explanation stage (details and code snippets below).

    explainer = TabularExplainer(
    model=clf,
    initialization_examples=new_lead,
    features=new_lead.columns.tolist(),
    classes=[0, 1]
    )

    print('Done')

    # The Below code is not returning the explanation. No error as well.
    explanation = explainer.explain_local(new_lead)


    Questions


    1. How can I generate explanations on why a lead is classified as won or lost using the trained model in Azure ML Designer?


    2. Is it possible to attach/model interpretability to a Designer pipeline model and expose explanations via the REST API?


    3. What is causing the maker.pk FileNotFoundError when trying to use the explainer/notebook on a trained Designer model?


    4. Are there any step-by-step resources or best practices for integrating "model interpretability" with Designer-trained, deployed models (including how to correctly load the model and run SHAP or similar explainers)?


    5. How do I generate explanations in clear, human language for each prediction, suitable for showing to business users?
    Code Snippet to Get Explanation Working on Local Data


    import pandas as pd
    import numpy as np
    from sklearn.linear_model import LogisticRegression
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import StandardScaler
    from sklearn.pipeline import Pipeline
    import joblib

    # Step 1: Generate dummy lead scoring dataset
    df = pd.DataFrame({
    "company_size": np.random.randint(10, 500, 100),
    "annual_revenue": np.random.randint(100000, 10000000, 100),
    "engagement_score": np.random.randint(0, 100, 100),
    "last_contact_days_ago": np.random.randint(1, 60, 100),
    "qualified": np.random.choice([0, 1], 100)
    })

    X = df.drop(columns=["qualified"])
    y = df["qualified"]

    # Step 2: Train model with pipeline
    pipeline = Pipeline([
    ("scaler", StandardScaler()),
    ("clf", LogisticRegression())
    ])

    pipeline.fit(X, y)

    # Step 3: Save the model
    joblib.dump(pipeline, "model.pkl")
    print('Done')

    # Step 4: Register model in Azure ML
    from azureml.core import Workspace, Model

    # Load workspace from config
    ws = Workspace.from_config()

    # Step 5: Load model back from registry
    import os
    model = Model(ws, name="lead-logreg-vai1")
    model_dir = model.download(exist_ok=True)
    print("model_dir:", model_dir)

    clf = joblib.load(model_dir)
    print("Loaded model type:", type(clf))

    # Step 6: Make prediction and interpret with TabularExplainer
    from interpret.ext.blackbox import TabularExplainer

    # New lead sample
    new_lead = pd.DataFrame([{
    "company_size": 10,
    "annual_revenue": 20000,
    "engagement_score": 100,
    "last_contact_days_ago": 100
    }])

    # Predict probability and class
    proba = clf.predict_proba(new_lead)
    pred = clf.predict(new_lead)
    print(f"Predicted probability: {proba[0][1]:.2f}, Class: {pred[0]}")

    explainer = TabularExplainer(
    model=clf,
    initialization_examples=new_lead,
    features=new_lead.columns.tolist(),
    classes=[0, 1]
    )

    print('Done')

    # The below code is not returning the explanation. No error as well.
    explanation = explainer.explain_local(new_lead)

    About Me


    I am relatively new to Azure Machine Learning, especially when it comes to interpretability and deployment aspects.

    Request for Guidance


    Could someone please provide guidance or point me to a working example for achieving the following in Azure ML Designer:

    • A way to get explanations per prediction for leads via API or notebook.
    • Help resolving the errors I'm encountering with the explainer.

    Any pointers to documentation, tutorials, or sample notebooks would be greatly appreciated!

    Thank you in advance for your support!


    Let me know if you'd like me to include or redact any specific error messages or code snippets. This draft is ready for posting to Microsoft Q&A, Stack Overflow (with the [azure-machine-learning] tag), or the Microsoft Tech Community, as previously advised.

    Continue reading...

Compartilhe esta Página