Spaces:
Runtime error
Runtime error
Delete pages 5_π²_Magenment_deck.py
Browse files
pages/5_π²_Magenment_deck.py
DELETED
|
@@ -1,73 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
import joblib
|
| 5 |
-
|
| 6 |
-
st.set_page_config(page_title="π² Management Deck: Cost Analysis", layout="wide")
|
| 7 |
-
st.title("π² Management Deck: Cost Analysis of Model Decisions")
|
| 8 |
-
|
| 9 |
-
# --- Load model and test data ---
|
| 10 |
-
@st.cache_data
|
| 11 |
-
def load_model_and_data():
|
| 12 |
-
model = joblib.load("model_1mvp.pkl")
|
| 13 |
-
df_test = pd.read_csv("test_data.csv")
|
| 14 |
-
return model, df_test
|
| 15 |
-
|
| 16 |
-
model, df_test = load_model_and_data()
|
| 17 |
-
target = "y"
|
| 18 |
-
X_test = df_test.drop(columns=[target])
|
| 19 |
-
y_test = df_test[target]
|
| 20 |
-
|
| 21 |
-
# --- Sidebar: Cost selection ---
|
| 22 |
-
with st.sidebar.form("cost_selector"):
|
| 23 |
-
st.markdown("## Set Cost Parameters")
|
| 24 |
-
cost_fp = st.number_input("Cost of False Positive (FP)", min_value=0, value=5, step=1)
|
| 25 |
-
cost_fn = st.number_input("Cost of False Negative (FN)", min_value=0, value=30, step=1)
|
| 26 |
-
submitted = st.form_submit_button("Update Cost Analysis")
|
| 27 |
-
|
| 28 |
-
if submitted:
|
| 29 |
-
st.subheader("Cost Analysis Based on Model Predictions")
|
| 30 |
-
|
| 31 |
-
# Predict probabilities and classes
|
| 32 |
-
y_pred_proba = model.predict_proba(X_test)[:, 1]
|
| 33 |
-
threshold = 0.5
|
| 34 |
-
y_pred = (y_pred_proba >= threshold).astype(int)
|
| 35 |
-
|
| 36 |
-
# Confusion matrix components
|
| 37 |
-
FP = np.sum((y_pred == 1) & (y_test == 0))
|
| 38 |
-
FN = np.sum((y_pred == 0) & (y_test == 1))
|
| 39 |
-
TP = np.sum((y_pred == 1) & (y_test == 1))
|
| 40 |
-
TN = np.sum((y_pred == 0) & (y_test == 0))
|
| 41 |
-
|
| 42 |
-
total_cost = FP * cost_fp + FN * cost_fn
|
| 43 |
-
|
| 44 |
-
st.markdown(f"""
|
| 45 |
-
**Threshold:** {threshold:.2f}
|
| 46 |
-
- **False Positives (FP):** {FP} Γ {cost_fp} = {FP * cost_fp}
|
| 47 |
-
- **False Negatives (FN):** {FN} Γ {cost_fn} = {FN * cost_fn}
|
| 48 |
-
- **True Positives (TP):** {TP}
|
| 49 |
-
- **True Negatives (TN):** {TN}
|
| 50 |
-
---
|
| 51 |
-
## **Total Cost: {total_cost}**
|
| 52 |
-
""")
|
| 53 |
-
|
| 54 |
-
# Optional: Show cost as a function of threshold
|
| 55 |
-
st.subheader("Cost vs. Threshold")
|
| 56 |
-
thresholds = np.linspace(0, 1, 120)
|
| 57 |
-
costs = []
|
| 58 |
-
for t in thresholds:
|
| 59 |
-
y_pred_t = (y_pred_proba >= t).astype(int)
|
| 60 |
-
FP_t = np.sum((y_pred_t == 1) & (y_test == 0))
|
| 61 |
-
FN_t = np.sum((y_pred_t == 0) & (y_test == 1))
|
| 62 |
-
costs.append(FP_t * cost_fp + FN_t * cost_fn)
|
| 63 |
-
import matplotlib.pyplot as plt
|
| 64 |
-
fig, ax = plt.subplots(figsize=(8, 4))
|
| 65 |
-
ax.plot(thresholds, costs, label="Total Cost")
|
| 66 |
-
ax.axvline(threshold, color="red", linestyle="--", label=f"Current threshold = {threshold:.2f}")
|
| 67 |
-
ax.set_xlabel("Threshold")
|
| 68 |
-
ax.set_ylabel("Total Cost")
|
| 69 |
-
ax.set_title("Total Cost vs. Classification Threshold")
|
| 70 |
-
ax.legend()
|
| 71 |
-
st.pyplot(fig)
|
| 72 |
-
|
| 73 |
-
st.caption("You can adjust the costs in the sidebar to see their impact on the total cost and optimal threshold.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|