Dun3Co commited on
Commit
10b2ce6
Β·
verified Β·
1 Parent(s): 1e7248f

Delete pages 4_πŸ’ƒ_Model_interpretation.py

Browse files
pages/4_πŸ’ƒ_Model_interpretation.py DELETED
@@ -1,114 +0,0 @@
1
- import streamlit as st
2
- import shap
3
- import matplotlib.pyplot as plt
4
- import numpy as np
5
- import joblib
6
- import pandas as pd
7
- from sklearn.metrics import roc_curve, roc_auc_score, precision_recall_curve, auc
8
-
9
- st.set_page_config(page_title="Model Analysis Dashboard", layout="wide")
10
- st.title("Model Analysis Dashboard")
11
-
12
- # --- Load model and test data ---
13
- @st.cache_data
14
- def load_model_and_data():
15
- model = joblib.load("model_1mvp.pkl")
16
- df_test = pd.read_csv("test_data.csv")
17
- return model, df_test
18
-
19
- model, df_test = load_model_and_data()
20
-
21
- target = "y"
22
- X_test = df_test.drop(columns=[target])
23
- y_test = df_test[target]
24
-
25
- preprocessor = model.named_steps["preprocessor"]
26
- feature_names = preprocessor.get_feature_names_out()
27
- X_test_transformed = preprocessor.transform(X_test)
28
-
29
- # --- SHAP Explainer (precompute for efficiency) ---
30
- explainer = shap.LinearExplainer(model.named_steps["classifier"], X_test_transformed, feature_names=feature_names)
31
- shap_values = explainer.shap_values(X_test_transformed)
32
- expected_value = explainer.expected_value
33
-
34
- # --- Sidebar: Plot selection and controls ---
35
- with st.sidebar.form("plot_selector"):
36
- st.markdown("## Select plots to display")
37
- show_coeff = st.checkbox("Logistic Regression Coefficients", value=True)
38
- show_shap_global = st.checkbox("SHAP Global (summary plot)", value=True)
39
- show_shap_local = st.checkbox("SHAP Local (waterfall plot)", value=False)
40
- show_roc = st.checkbox("ROC/PR Curves", value=True)
41
- top_n = st.slider("Number of top features for LogReg coeffecients", 5, 30, 15)
42
- local_idx = st.number_input("Local SHAP sample index", min_value=0, max_value=len(X_test)-1, value=0)
43
- submitted = st.form_submit_button("Update plots")
44
-
45
- # --- Logistic Regression Coefficient Plot ---
46
- if show_coeff and submitted:
47
- st.header("Logistic Regression Coefficients")
48
- logreg_model = model.named_steps["classifier"]
49
- coefficients = logreg_model.coef_[0]
50
- importance = pd.DataFrame({
51
- "feature": feature_names,
52
- "coefficient": coefficients
53
- }).sort_values(by="coefficient", key=abs, ascending=False)
54
- fig, ax = plt.subplots(figsize=(8, 6))
55
- importance.head(top_n).set_index("feature")["coefficient"].plot(kind="barh", ax=ax, color="#4C72B0")
56
- ax.set_title("Logistic Regression Feature Importance (Coefficients)")
57
- ax.set_xlabel("Coefficient Value")
58
- ax.set_ylabel("Feature")
59
- st.pyplot(fig)
60
- st.dataframe(importance.head(top_n).style.format({"coefficient": "{:.3f}"}))
61
-
62
- # --- SHAP Analysis ---
63
- if (show_shap_global or show_shap_local) and submitted:
64
- st.header("SHAP Analysis")
65
- if show_shap_global:
66
- st.subheader("Global Feature Importance (SHAP Summary Plot)")
67
- fig, ax = plt.subplots(figsize=(10, 6))
68
- shap.summary_plot(shap_values, X_test_transformed, feature_names=feature_names, show=False)
69
- st.pyplot(fig)
70
- if show_shap_local:
71
- st.subheader("Local Explanation (SHAP Waterfall Plot)")
72
- fig2, ax2 = plt.subplots(figsize=(10, 6))
73
- shap.plots.waterfall(
74
- shap.Explanation(
75
- values=shap_values[local_idx],
76
- base_values=expected_value,
77
- data=X_test_transformed[local_idx],
78
- feature_names=feature_names
79
- ),
80
- max_display=15,
81
- show=False
82
- )
83
- st.pyplot(fig2)
84
-
85
- # --- ROC and PR Curves ---
86
- if show_roc and submitted:
87
- st.header("Model Performance Metrics (ROC / PR Curves)")
88
- y_pred_proba = model.predict_proba(X_test)[:, 1]
89
- roc_auc = roc_auc_score(y_test, y_pred_proba)
90
- fpr, tpr, _ = roc_curve(y_test, y_pred_proba)
91
- precision, recall, _ = precision_recall_curve(y_test, y_pred_proba)
92
- pr_auc = auc(recall, precision)
93
-
94
- col1, col2 = st.columns(2)
95
- with col1:
96
- st.metric("ROC AUC", f"{roc_auc:.3f}")
97
- with col2:
98
- st.metric("PR AUC", f"{pr_auc:.3f}")
99
-
100
- fig1, ax1 = plt.subplots(figsize=(5, 5))
101
- ax1.plot(fpr, tpr, color="darkorange", lw=2, label=f"ROC curve (AUC = {roc_auc:.3f})")
102
- ax1.plot([0, 1], [0, 1], color="navy", lw=2, linestyle="--", label="Random Guess")
103
- ax1.set_xlabel("False Positive Rate")
104
- ax1.set_ylabel("True Positive Rate")
105
- ax1.set_title("ROC Curve")
106
- ax1.legend()
107
- st.pyplot(fig1)
108
-
109
- fig2, ax2 = plt.subplots(figsize=(5, 5))
110
- ax2.plot(recall, precision, color="#C44E52")
111
- ax2.set_xlabel("Recall")
112
- ax2.set_ylabel("Precision")
113
- ax2.set_title("Precision-Recall Curve")
114
- st.pyplot(fig2)