Update app.py
Browse files
app.py
CHANGED
|
@@ -269,7 +269,7 @@ if df_original is not None:
|
|
| 269 |
# 2. Pré-processar
|
| 270 |
X, y = preprocess_data(df_sample)
|
| 271 |
|
| 272 |
-
#
|
| 273 |
feature_names = X.columns.tolist()
|
| 274 |
|
| 275 |
# 3. Dividir (Train/Test)
|
|
@@ -277,9 +277,7 @@ if df_original is not None:
|
|
| 277 |
X, y, test_size=test_split_pct, random_state=42, stratify=y
|
| 278 |
)
|
| 279 |
|
| 280 |
-
# 4. Escalonar
|
| 281 |
-
# Nota: RL com 'liblinear' não precisa de escalonamento,
|
| 282 |
-
# mas vamos manter para consistência e performance.
|
| 283 |
scaler = StandardScaler()
|
| 284 |
X_train_scaled = scaler.fit_transform(X_train)
|
| 285 |
X_test_scaled = scaler.transform(X_test)
|
|
@@ -338,12 +336,10 @@ if df_original is not None:
|
|
| 338 |
st.subheader("Relatório de Classificação Detalhado")
|
| 339 |
st.dataframe(report_df.style.format("{:.3f}"))
|
| 340 |
|
| 341 |
-
# --- [NOVA SEÇÃO ADICIONADA] ---
|
| 342 |
# Interpretação específica da Regressão Logística
|
| 343 |
if algorithm == "Regressão Logística":
|
| 344 |
st.subheader("Análise de Coeficientes (Interpretabilidade)")
|
| 345 |
|
| 346 |
-
# Captura coeficientes e odds ratios
|
| 347 |
coefs = model.coef_[0]
|
| 348 |
odds_ratios = np.exp(coefs)
|
| 349 |
|
|
@@ -353,14 +349,16 @@ if df_original is not None:
|
|
| 353 |
'Odds Ratio (Razão de Chances)': odds_ratios
|
| 354 |
})
|
| 355 |
|
| 356 |
-
|
|
|
|
|
|
|
| 357 |
|
| 358 |
st.dataframe(df_coef.style.format({
|
| 359 |
'Coeficiente (Log-Odds)': '{:.4f}',
|
| 360 |
'Odds Ratio (Razão de Chances)': '{:.3f}'
|
| 361 |
}).background_gradient(
|
| 362 |
cmap='RdBu_r',
|
| 363 |
-
subset=['Odds Ratio', 'Coeficiente (Log-Odds)'])
|
| 364 |
)
|
| 365 |
|
| 366 |
st.markdown("""
|
|
@@ -371,8 +369,6 @@ if df_original is not None:
|
|
| 371 |
* *Exemplo: Se `deposit_type_Non Refund` tem Odds Ratio de 0.20, ter um depósito não-reembolsável reduz a chance de cancelar em 80%.*
|
| 372 |
* **Odds Ratio = 1:** Não tem efeito.
|
| 373 |
""")
|
| 374 |
-
# --- [FIM DA NOVA SEÇÃO] ---
|
| 375 |
-
|
| 376 |
|
| 377 |
# --- Interpretação Gerencial Automática ---
|
| 378 |
st.header("💡 Interpretação Gerencial e Recomendações")
|
|
|
|
| 269 |
# 2. Pré-processar
|
| 270 |
X, y = preprocess_data(df_sample)
|
| 271 |
|
| 272 |
+
# Captura os nomes das features APÓS o get_dummies
|
| 273 |
feature_names = X.columns.tolist()
|
| 274 |
|
| 275 |
# 3. Dividir (Train/Test)
|
|
|
|
| 277 |
X, y, test_size=test_split_pct, random_state=42, stratify=y
|
| 278 |
)
|
| 279 |
|
| 280 |
+
# 4. Escalonar
|
|
|
|
|
|
|
| 281 |
scaler = StandardScaler()
|
| 282 |
X_train_scaled = scaler.fit_transform(X_train)
|
| 283 |
X_test_scaled = scaler.transform(X_test)
|
|
|
|
| 336 |
st.subheader("Relatório de Classificação Detalhado")
|
| 337 |
st.dataframe(report_df.style.format("{:.3f}"))
|
| 338 |
|
|
|
|
| 339 |
# Interpretação específica da Regressão Logística
|
| 340 |
if algorithm == "Regressão Logística":
|
| 341 |
st.subheader("Análise de Coeficientes (Interpretabilidade)")
|
| 342 |
|
|
|
|
| 343 |
coefs = model.coef_[0]
|
| 344 |
odds_ratios = np.exp(coefs)
|
| 345 |
|
|
|
|
| 349 |
'Odds Ratio (Razão de Chances)': odds_ratios
|
| 350 |
})
|
| 351 |
|
| 352 |
+
# ***** [LINHA CORRIGIDA] *****
|
| 353 |
+
# O nome da coluna no 'by=' agora bate com o nome da coluna no DataFrame
|
| 354 |
+
df_coef = df_coef.sort_values(by="Odds Ratio (Razão de Chances)", ascending=False)
|
| 355 |
|
| 356 |
st.dataframe(df_coef.style.format({
|
| 357 |
'Coeficiente (Log-Odds)': '{:.4f}',
|
| 358 |
'Odds Ratio (Razão de Chances)': '{:.3f}'
|
| 359 |
}).background_gradient(
|
| 360 |
cmap='RdBu_r',
|
| 361 |
+
subset=['Odds Ratio (Razão de Chances)', 'Coeficiente (Log-Odds)'])
|
| 362 |
)
|
| 363 |
|
| 364 |
st.markdown("""
|
|
|
|
| 369 |
* *Exemplo: Se `deposit_type_Non Refund` tem Odds Ratio de 0.20, ter um depósito não-reembolsável reduz a chance de cancelar em 80%.*
|
| 370 |
* **Odds Ratio = 1:** Não tem efeito.
|
| 371 |
""")
|
|
|
|
|
|
|
| 372 |
|
| 373 |
# --- Interpretação Gerencial Automática ---
|
| 374 |
st.header("💡 Interpretação Gerencial e Recomendações")
|