Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -145,8 +145,10 @@ def plot_relacoes_bivariadas(df, variaveis_selecionadas):
|
|
| 145 |
fig, axes = plt.subplots(n_rows, n_cols, figsize=(15, 4 * n_rows))
|
| 146 |
|
| 147 |
# Garante que axes seja sempre uma lista 2D
|
| 148 |
-
if n_rows == 1:
|
| 149 |
-
axes = [axes]
|
|
|
|
|
|
|
| 150 |
else:
|
| 151 |
axes = axes.ravel()
|
| 152 |
|
|
@@ -279,6 +281,13 @@ def treinar_modelo(variaveis_selecionadas, usar_modelo_robusto=False):
|
|
| 279 |
|
| 280 |
def plot_importance_features(feature_importance_df):
|
| 281 |
"""Plota a importância das features para Random Forest"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
fig, ax = plt.subplots(figsize=(10, 5))
|
| 283 |
sns.barplot(data=feature_importance_df, x='Importance', y='Feature', ax=ax)
|
| 284 |
ax.set_title('Importância das Variáveis - Random Forest', fontsize=12)
|
|
@@ -316,7 +325,12 @@ def analise_completa(variaveis_selecionadas, usar_modelo_robusto=False):
|
|
| 316 |
if usar_modelo_robusto and resultados['feature_importance'] is not None:
|
| 317 |
fig_importance = plot_importance_features(resultados['feature_importance'])
|
| 318 |
else:
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
# Estatísticas descritivas
|
| 322 |
stats_descritivas = df_model['SalePrice'].describe().reset_index()
|
|
@@ -509,18 +523,39 @@ with gr.Blocks(
|
|
| 509 |
variaveis_selecionadas = get_selected_variables(*checkbox_values)
|
| 510 |
|
| 511 |
if not variaveis_selecionadas:
|
| 512 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 513 |
|
| 514 |
resultados = analise_completa(variaveis_selecionadas, usar_robusto)
|
| 515 |
-
output = list(resultados[:7])
|
| 516 |
-
output.append(resultados[8])
|
| 517 |
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
return output
|
|
|
|
| 522 |
except Exception as e:
|
| 523 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 524 |
|
| 525 |
def predizer_preco_wrapper(*args):
|
| 526 |
try:
|
|
@@ -577,4 +612,4 @@ with gr.Blocks(
|
|
| 577 |
|
| 578 |
# Configuração para Spaces
|
| 579 |
if __name__ == "__main__":
|
| 580 |
-
demo.launch()
|
|
|
|
| 145 |
fig, axes = plt.subplots(n_rows, n_cols, figsize=(15, 4 * n_rows))
|
| 146 |
|
| 147 |
# Garante que axes seja sempre uma lista 2D
|
| 148 |
+
if n_rows == 1 and n_cols == 1:
|
| 149 |
+
axes = np.array([axes])
|
| 150 |
+
elif n_rows == 1:
|
| 151 |
+
axes = axes
|
| 152 |
else:
|
| 153 |
axes = axes.ravel()
|
| 154 |
|
|
|
|
| 281 |
|
| 282 |
def plot_importance_features(feature_importance_df):
|
| 283 |
"""Plota a importância das features para Random Forest"""
|
| 284 |
+
if feature_importance_df is None or len(feature_importance_df) == 0:
|
| 285 |
+
fig, ax = plt.subplots(figsize=(8, 4))
|
| 286 |
+
ax.text(0.5, 0.5, 'Nenhuma importância disponível',
|
| 287 |
+
ha='center', va='center', transform=ax.transAxes, fontsize=12)
|
| 288 |
+
ax.set_title('Importância das Variáveis')
|
| 289 |
+
return fig
|
| 290 |
+
|
| 291 |
fig, ax = plt.subplots(figsize=(10, 5))
|
| 292 |
sns.barplot(data=feature_importance_df, x='Importance', y='Feature', ax=ax)
|
| 293 |
ax.set_title('Importância das Variáveis - Random Forest', fontsize=12)
|
|
|
|
| 325 |
if usar_modelo_robusto and resultados['feature_importance'] is not None:
|
| 326 |
fig_importance = plot_importance_features(resultados['feature_importance'])
|
| 327 |
else:
|
| 328 |
+
# Cria um gráfico vazio quando não há feature importance
|
| 329 |
+
fig_importance, ax = plt.subplots(figsize=(8, 4))
|
| 330 |
+
ax.text(0.5, 0.5, 'Feature Importance disponível apenas para Random Forest',
|
| 331 |
+
ha='center', va='center', transform=ax.transAxes, fontsize=12)
|
| 332 |
+
ax.set_title('Importância das Variáveis')
|
| 333 |
+
plt.tight_layout()
|
| 334 |
|
| 335 |
# Estatísticas descritivas
|
| 336 |
stats_descritivas = df_model['SalePrice'].describe().reset_index()
|
|
|
|
| 523 |
variaveis_selecionadas = get_selected_variables(*checkbox_values)
|
| 524 |
|
| 525 |
if not variaveis_selecionadas:
|
| 526 |
+
# Retorna valores padrão se nenhuma variável selecionada
|
| 527 |
+
empty_fig, ax = plt.subplots(figsize=(8, 4))
|
| 528 |
+
ax.text(0.5, 0.5, 'Selecione variáveis para ver a análise',
|
| 529 |
+
ha='center', va='center', transform=ax.transAxes, fontsize=12)
|
| 530 |
+
ax.set_title('Aguardando seleção')
|
| 531 |
+
plt.tight_layout()
|
| 532 |
+
|
| 533 |
+
empty_data = [["Selecione variáveis", "Selecione variáveis"]]
|
| 534 |
+
empty_json = {"Status": "Selecione variáveis para análise"}
|
| 535 |
+
|
| 536 |
+
return (empty_fig, empty_fig, empty_fig, empty_fig, empty_fig,
|
| 537 |
+
empty_json, empty_data, empty_data, "Nenhum modelo")
|
| 538 |
|
| 539 |
resultados = analise_completa(variaveis_selecionadas, usar_robusto)
|
|
|
|
|
|
|
| 540 |
|
| 541 |
+
# Garante que retornamos exatamente 9 valores
|
| 542 |
+
output = list(resultados)
|
| 543 |
+
|
| 544 |
return output
|
| 545 |
+
|
| 546 |
except Exception as e:
|
| 547 |
+
# Retorna gráficos de erro em caso de falha
|
| 548 |
+
error_fig, ax = plt.subplots(figsize=(8, 4))
|
| 549 |
+
ax.text(0.5, 0.5, f'Erro: {str(e)}',
|
| 550 |
+
ha='center', va='center', transform=ax.transAxes, fontsize=10)
|
| 551 |
+
ax.set_title('Erro na análise')
|
| 552 |
+
plt.tight_layout()
|
| 553 |
+
|
| 554 |
+
error_data = [["Erro", str(e)]]
|
| 555 |
+
error_json = {"Erro": str(e)}
|
| 556 |
+
|
| 557 |
+
return (error_fig, error_fig, error_fig, error_fig, error_fig,
|
| 558 |
+
error_json, error_data, error_data, "Erro")
|
| 559 |
|
| 560 |
def predizer_preco_wrapper(*args):
|
| 561 |
try:
|
|
|
|
| 612 |
|
| 613 |
# Configuração para Spaces
|
| 614 |
if __name__ == "__main__":
|
| 615 |
+
demo.launch(debug=True)
|