File size: 2,263 Bytes
4aa5f86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import gradio as gr
import matplotlib.pyplot as plt
import shap
import hopsworks
import pandas as pd
import joblib
from sklearn.pipeline import make_pipeline

df = pd.DataFrame(
    [[20, 20, 30, 40]],
    columns=["age", "bmi", "hba1c_level", "blood_glucose_level"],
)


# Assuming the hopsworks login and model retrieval code works as expected
project = hopsworks.login(
    project="SonyaStern_Lab1",
    api_key_value="c9StuuVQPoMUeXWe.jB2XeWcI8poKUN59W13MxAbMemzY7SChOnX151GtTFNhysBBUPMRuEp5IK7SE3i1",
)
mr = project.get_model_registry()
model = mr.get_model("diabetes_model", version=1)
model_dir = model.download()
model = joblib.load(model_dir + "/diabetes_model.pkl")
print("printing model pipeline:", model)

rf_classifier = model.named_steps["randomforestclassifier"]

transformer_pipeline = make_pipeline(
    *[
        step
        for name, step in model.named_steps.items()
        if name != "randomforestclassifier"
    ]
)

transformed_df = transformer_pipeline.transform(df)


# rf_model = model.steps[-1][1]  # Load your model


def generate_plots():
    # Create the first plot as before
    fig1, ax1 = plt.subplots()
    ax1.plot([1, 2, 3], [4, 5, 6])
    ax1.set_title("Plot 1")

    # Generate the SHAP waterfall plot for fig2
    explainer = shap.TreeExplainer(rf_classifier)

    shap_values = explainer.shap_values(transformed_df)
    predicted_class = rf_classifier.predict(transformed_df)[0]
    shap_values_for_predicted_class = shap_values[predicted_class]
    # base_value = explainer.expected_value[1]

    fig2 = plt.figure()  # Create a new figure for SHAP plot
    shap_explanation = shap.Explanation(
        values=shap_values_for_predicted_class[0],
        base_values=explainer.expected_value[predicted_class],
        data=transformed_df[0],
        feature_names=df.columns.tolist(),
    )
    shap.waterfall_plot(shap_explanation)
    plt.title("SHAP Waterfall Plot")  # Optionally set a title for the SHAP plot

    return fig1, fig2


# Generate plots once and store them
fig1, fig2 = generate_plots()

with gr.Blocks() as demo:
    with gr.Row():
        gr.Plot(fig1)  # Display first plot in the first row

    with gr.Row():
        gr.Plot(fig2)  # Display SHAP waterfall plot in the second row

demo.launch()