Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +205 -181
src/streamlit_app.py
CHANGED
|
@@ -1,196 +1,220 @@
|
|
| 1 |
#!/usr/bin/env python
|
| 2 |
# coding: utf-8
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
import numpy as np
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
from scipy.stats import shapiro, levene, kruskal
|
| 16 |
-
from statsmodels.formula.api import ols
|
| 17 |
-
import statsmodels.api as sm
|
| 18 |
-
from statsmodels.stats.diagnostic import het_breuschpagan
|
| 19 |
-
from statsmodels.stats.outliers_influence import variance_inflation_factor
|
| 20 |
from sklearn.model_selection import train_test_split
|
| 21 |
-
from sklearn.
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# -----------------------------------------------------
|
| 26 |
-
st.set_page_config(
|
| 27 |
-
page_title="Dashboard - AmesHousing (Tarefa 4)",
|
| 28 |
-
layout="wide",
|
| 29 |
-
initial_sidebar_state="expanded"
|
| 30 |
-
)
|
| 31 |
-
st.query_params.clear()
|
| 32 |
-
|
| 33 |
-
st.markdown("<h1 style='text-align:center;color:#003366;'>Análise do Dataset AmesHousing</h1>", unsafe_allow_html=True)
|
| 34 |
-
st.markdown("---")
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
#
|
| 38 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
@st.cache_data
|
| 40 |
-
def
|
| 41 |
-
|
| 42 |
-
"
|
| 43 |
-
"/
|
| 44 |
-
"
|
|
|
|
|
|
|
| 45 |
]
|
| 46 |
-
|
|
|
|
| 47 |
try:
|
| 48 |
df = pd.read_csv(p)
|
| 49 |
-
return df
|
| 50 |
-
except:
|
|
|
|
| 51 |
continue
|
| 52 |
-
return pd.DataFrame()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# -----------------------------
|
| 58 |
-
#
|
| 59 |
# -----------------------------
|
| 60 |
-
st.
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
if
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
if
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
["
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
#
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
if
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
sm.qqplot(residuos, line='45', fit=True, ax=ax_q)
|
| 192 |
-
st.pyplot(fig_q, clear_figure=True, use_container_width=False)
|
| 193 |
-
with cols[2]:
|
| 194 |
-
fig_h, ax_h = plt.subplots(figsize=(3,3))
|
| 195 |
-
sns.histplot(residuos, kde=True, ax=ax_h)
|
| 196 |
-
st.pyplot(fig_h, clear_figure=True, use_container_width=False)
|
|
|
|
| 1 |
#!/usr/bin/env python
|
| 2 |
# coding: utf-8
|
| 3 |
+
"""
|
| 4 |
+
Streamlit – Churn (Logistic Regression) for Hugging Face Spaces
|
| 5 |
+
---------------------------------------------------------------
|
| 6 |
+
- Loads "Dados/Churn_Modelling.csv"
|
| 7 |
+
- Lets the user choose features (numeric / categorical)
|
| 8 |
+
- Trains Logistic Regression
|
| 9 |
+
- Shows coefficients, odds ratios, and quick interpretations
|
| 10 |
+
- Provides an interactive control panel to simulate a customer's probability of churn
|
| 11 |
+
|
| 12 |
+
Obs.: Esta versão atende ao item (a) da Tarefa 5: modelagem com Regressão Logística
|
| 13 |
+
e interpretação dos coeficientes/odds ratio.
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import os
|
| 17 |
import numpy as np
|
| 18 |
+
import pandas as pd
|
| 19 |
+
import streamlit as st
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
from sklearn.model_selection import train_test_split
|
| 22 |
+
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
| 23 |
+
from sklearn.compose import ColumnTransformer
|
| 24 |
+
from sklearn.linear_model import LogisticRegression
|
| 25 |
+
from sklearn.pipeline import Pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# -----------------------------
|
| 28 |
+
# Page config
|
| 29 |
+
# -----------------------------
|
| 30 |
+
st.set_page_config(page_title="Churn – Regressão Logística (PPCA/UnB)", layout="wide", initial_sidebar_state="expanded")
|
| 31 |
+
st.title("Churn – Regressão Logística (PPCA/UnB)")
|
| 32 |
+
st.caption("Item (a) – Modelagem da Retenção de Clientes e interpretação de coeficientes/odds ratio.")
|
| 33 |
+
|
| 34 |
+
# -----------------------------
|
| 35 |
+
# Data loader (cache)
|
| 36 |
+
# -----------------------------
|
| 37 |
@st.cache_data
|
| 38 |
+
def load_data():
|
| 39 |
+
tried = [
|
| 40 |
+
"Dados/Churn_Modelling.csv",
|
| 41 |
+
"./Dados/Churn_Modelling.csv",
|
| 42 |
+
"/mnt/data/Dados/Churn_Modelling.csv",
|
| 43 |
+
"Churn_Modelling.csv",
|
| 44 |
+
"./Churn_Modelling.csv"
|
| 45 |
]
|
| 46 |
+
last_err = None
|
| 47 |
+
for p in tried:
|
| 48 |
try:
|
| 49 |
df = pd.read_csv(p)
|
| 50 |
+
return df, p
|
| 51 |
+
except Exception as e:
|
| 52 |
+
last_err = e
|
| 53 |
continue
|
| 54 |
+
return pd.DataFrame(), str(last_err)
|
| 55 |
+
|
| 56 |
+
df, data_info = load_data()
|
| 57 |
+
|
| 58 |
+
if df.empty:
|
| 59 |
+
st.error("Não foi possível carregar o arquivo **Churn_Modelling.csv**. "
|
| 60 |
+
"Certifique-se de que ele está em `Dados/Churn_Modelling.csv` dentro do Space.")
|
| 61 |
+
st.stop()
|
| 62 |
|
| 63 |
+
st.success(f"Dataset carregado de: `{data_info}`")
|
| 64 |
+
|
| 65 |
+
# Normalizar nomes de colunas
|
| 66 |
+
df.columns = [c.strip() for c in df.columns]
|
| 67 |
+
|
| 68 |
+
# -----------------------------
|
| 69 |
+
# Target and candidate features (dataset padrão do Kaggle)
|
| 70 |
+
# -----------------------------
|
| 71 |
+
TARGET = "Exited" # 1 = saiu, 0 = permaneceu
|
| 72 |
+
candidates_num = [c for c in ["CreditScore","Age","Tenure","Balance","NumOfProducts","HasCrCard","IsActiveMember","EstimatedSalary"] if c in df.columns]
|
| 73 |
+
candidates_cat = [c for c in ["Geography","Gender"] if c in df.columns]
|
| 74 |
+
|
| 75 |
+
# Sidebar: feature selection & model hyperparams
|
| 76 |
+
st.sidebar.header("Configuração do Modelo")
|
| 77 |
+
use_num = st.sidebar.multiselect("Variáveis numéricas", options=candidates_num, default=[c for c in ["Age","Balance","NumOfProducts","IsActiveMember"] if c in candidates_num])
|
| 78 |
+
use_cat = st.sidebar.multiselect("Variáveis categóricas", options=candidates_cat, default=[c for c in ["Geography","Gender"] if c in candidates_cat])
|
| 79 |
+
|
| 80 |
+
test_size = st.sidebar.slider("Proporção de teste", 0.1, 0.4, 0.2, 0.05)
|
| 81 |
+
reg_strength = st.sidebar.slider("Força de regularização (C)", 0.05, 5.0, 1.0, 0.05)
|
| 82 |
+
class_balanced = st.sidebar.checkbox("Class weight = 'balanced' (útil se desbalanceado)", value=True)
|
| 83 |
+
max_iter = st.sidebar.slider("Max iter", 200, 2000, 1000, 100)
|
| 84 |
+
|
| 85 |
+
train_btn = st.sidebar.button("Treinar modelo")
|
| 86 |
|
| 87 |
# -----------------------------
|
| 88 |
+
# Quick EDA block (compact)
|
| 89 |
# -----------------------------
|
| 90 |
+
st.subheader("Visão rápida do conjunto de dados")
|
| 91 |
+
col_a, col_b = st.columns([2,1])
|
| 92 |
+
with col_a:
|
| 93 |
+
st.dataframe(df.sample(min(10, len(df))), use_container_width=True)
|
| 94 |
+
with col_b:
|
| 95 |
+
if TARGET in df.columns:
|
| 96 |
+
n1 = int(df[TARGET].sum())
|
| 97 |
+
n0 = int((1 - df[TARGET]).sum())
|
| 98 |
+
st.metric("Clientes que saíram (1)", n1)
|
| 99 |
+
st.metric("Clientes que ficaram (0)", n0)
|
| 100 |
+
|
| 101 |
+
# -----------------------------
|
| 102 |
+
# Training
|
| 103 |
+
# -----------------------------
|
| 104 |
+
def build_pipeline(num_cols, cat_cols, C=1.0, class_weight=None, max_iter=1000):
|
| 105 |
+
preprocess = ColumnTransformer(
|
| 106 |
+
transformers=[
|
| 107 |
+
("num", StandardScaler(with_mean=True, with_std=True), num_cols),
|
| 108 |
+
("cat", OneHotEncoder(drop="first", handle_unknown="ignore"), cat_cols),
|
| 109 |
+
],
|
| 110 |
+
remainder="drop"
|
| 111 |
+
)
|
| 112 |
+
lr = LogisticRegression(C=C, penalty="l2", solver="lbfgs", max_iter=max_iter, class_weight=class_weight, n_jobs=None)
|
| 113 |
+
pipe = Pipeline(steps=[("prep", preprocess), ("clf", lr)])
|
| 114 |
+
return pipe
|
| 115 |
+
|
| 116 |
+
def get_feature_names(preprocess, num_cols, cat_cols):
|
| 117 |
+
names = []
|
| 118 |
+
if num_cols:
|
| 119 |
+
names.extend(num_cols)
|
| 120 |
+
if cat_cols:
|
| 121 |
+
ohe = preprocess.named_transformers_["cat"]
|
| 122 |
+
cat_names = ohe.get_feature_names_out(cat_cols).tolist()
|
| 123 |
+
names.extend(cat_names)
|
| 124 |
+
return names
|
| 125 |
+
|
| 126 |
+
if train_btn:
|
| 127 |
+
if not use_num and not use_cat:
|
| 128 |
+
st.warning("Selecione pelo menos **uma** variável explicativa (numérica ou categórica).")
|
| 129 |
+
st.stop()
|
| 130 |
+
|
| 131 |
+
cols_needed = [TARGET] + use_num + use_cat
|
| 132 |
+
df_model = df[cols_needed].dropna().copy()
|
| 133 |
+
|
| 134 |
+
X = df_model[use_num + use_cat]
|
| 135 |
+
y = df_model[TARGET]
|
| 136 |
+
|
| 137 |
+
cw = "balanced" if class_balanced else None
|
| 138 |
+
pipe = build_pipeline(use_num, use_cat, C=reg_strength, class_weight=cw, max_iter=max_iter)
|
| 139 |
+
|
| 140 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=42, stratify=y)
|
| 141 |
+
pipe.fit(X_train, y_train)
|
| 142 |
+
|
| 143 |
+
# -------------------------
|
| 144 |
+
# Coefficients & Odds Ratios
|
| 145 |
+
# -------------------------
|
| 146 |
+
lr = pipe.named_steps["clf"]
|
| 147 |
+
preprocess = pipe.named_steps["prep"]
|
| 148 |
+
|
| 149 |
+
feat_names = get_feature_names(preprocess, use_num, use_cat)
|
| 150 |
+
coefs = lr.coef_.ravel()
|
| 151 |
+
odds = np.exp(coefs)
|
| 152 |
+
|
| 153 |
+
coef_table = pd.DataFrame({
|
| 154 |
+
"Variável": feat_names,
|
| 155 |
+
"Coeficiente (β)": coefs,
|
| 156 |
+
"Odds Ratio (e^β)": odds
|
| 157 |
+
}).sort_values(by="Odds Ratio (e^β)", ascending=False)
|
| 158 |
+
|
| 159 |
+
st.subheader("Coeficientes e Odds Ratio")
|
| 160 |
+
st.write(
|
| 161 |
+
"Interpretação: mantendo as demais variáveis constantes, um aumento de uma unidade na variável (ou mudança para a categoria indicada) "
|
| 162 |
+
"multiplica as *odds* de churn por `e^β`. Se `e^β > 1`, o risco de churn aumenta; se `< 1`, diminui."
|
| 163 |
+
)
|
| 164 |
+
st.dataframe(coef_table, use_container_width=True, height=380)
|
| 165 |
+
|
| 166 |
+
# Acurácia simples (para referência rápida no item a)
|
| 167 |
+
acc = pipe.score(X_test, y_test)
|
| 168 |
+
st.info(f"**Acurácia (holdout)**: {acc:.3f} | Amostras de treino: {len(X_train)} | Amostras de teste: {len(X_test)}")
|
| 169 |
+
|
| 170 |
+
# -------------------------
|
| 171 |
+
# Interactive prediction
|
| 172 |
+
# -------------------------
|
| 173 |
+
st.subheader("Simulação: probabilidade de churn para um perfil de cliente")
|
| 174 |
+
with st.expander("Abrir painel de controle do cliente", expanded=True):
|
| 175 |
+
# Build controls dynamically from current selections
|
| 176 |
+
inputs = {}
|
| 177 |
+
cols = st.columns(2)
|
| 178 |
+
|
| 179 |
+
# Numeric controls
|
| 180 |
+
for i, col in enumerate(use_num):
|
| 181 |
+
with cols[i % 2]:
|
| 182 |
+
vmin = float(np.nanmin(df[col])) if np.isfinite(df[col]).all() else 0.0
|
| 183 |
+
vmax = float(np.nanmax(df[col])) if np.isfinite(df[col]).all() else 1.0
|
| 184 |
+
vmean = float(np.nanmean(df[col])) if np.isfinite(df[col]).all() else (vmin + vmax)/2.0
|
| 185 |
+
step = (vmax - vmin) / 100.0 if vmax > vmin else 1.0
|
| 186 |
+
inputs[col] = st.number_input(f"{col}", value=round(vmean, 2), step=step, min_value=vmin, max_value=vmax, format="%.2f")
|
| 187 |
+
|
| 188 |
+
# Categorical controls
|
| 189 |
+
for i, col in enumerate(use_cat):
|
| 190 |
+
with cols[i % 2]:
|
| 191 |
+
opts = sorted([o for o in df[col].dropna().unique().tolist()])
|
| 192 |
+
default_idx = 0 if opts else None
|
| 193 |
+
inputs[col] = st.selectbox(f"{col}", options=opts, index=default_idx if default_idx is not None else 0)
|
| 194 |
+
|
| 195 |
+
# Compose a single-row DataFrame
|
| 196 |
+
if inputs:
|
| 197 |
+
row = pd.DataFrame([inputs])
|
| 198 |
+
proba = float(pipe.predict_proba(row)[0, 1])
|
| 199 |
+
st.metric("Probabilidade de churn (sair do banco)", f"{proba:.1%}")
|
| 200 |
+
st.caption("Dica: ajuste os controles e observe como a probabilidade muda.")
|
| 201 |
+
|
| 202 |
+
# -------------------------
|
| 203 |
+
# Textual help / interpretation
|
| 204 |
+
# -------------------------
|
| 205 |
+
st.subheader("Como interpretar os coeficientes")
|
| 206 |
+
st.markdown("""
|
| 207 |
+
- **Sinal de β**: positivo ⇒ aumenta as *odds* de churn; negativo ⇒ reduz.
|
| 208 |
+
- **Magnitude**: valores maiores em módulo indicam maior impacto, dado o mesmo escalonamento.
|
| 209 |
+
- **Odds Ratio `e^β`**: fator multiplicativo nas *odds*. Ex.: `e^β = 1.30` ⇒ as *odds* aumentam **30%**.
|
| 210 |
+
- Em variáveis **categóricas**, o β refere-se à **categoria de referência vs. a categoria exibida** (depois do one-hot com `drop='first'`).
|
| 211 |
+
""")
|
| 212 |
+
|
| 213 |
+
else:
|
| 214 |
+
st.info("Selecione as variáveis na barra lateral e clique em **Treinar modelo** para começar.")
|
| 215 |
+
|
| 216 |
+
# -----------------------------
|
| 217 |
+
# Footer
|
| 218 |
+
# -----------------------------
|
| 219 |
+
st.markdown("---")
|
| 220 |
+
st.caption("PPCA/UnB • Tarefa 5 – Item (a) • Regressão Logística + Odds Ratio • Feito para rodar em Hugging Face Spaces (Streamlit).")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|