Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -51,21 +51,29 @@ def get_metrics(model, X, y):
|
|
| 51 |
"""
|
| 52 |
}
|
| 53 |
|
| 54 |
-
|
| 55 |
-
def plot_confusion_matrices(model1, model2, X, y, labels=["Base Model", "Best Model"]):
|
| 56 |
fig, axes = plt.subplots(1, 2, figsize=(16, 8))
|
| 57 |
-
#fig, axes = plt.subplots(1, 2, figsize=(50, 25))
|
| 58 |
|
| 59 |
for i, model in enumerate([model1, model2]):
|
| 60 |
y_pred = model.predict(X)
|
| 61 |
cm = confusion_matrix(y, y_pred)
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
disp.plot(ax=axes[i], cmap='Blues', colorbar=False)
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
for (j, k), val in np.ndenumerate(cm):
|
| 66 |
-
axes[i].text(k, j, f"{val}", ha='center', va='center', fontsize=
|
| 67 |
-
|
| 68 |
-
axes[i].
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
plt.tight_layout()
|
| 71 |
return plt.gcf()
|
|
@@ -84,8 +92,8 @@ def evaluate(file):
|
|
| 84 |
|
| 85 |
# Combined ROC Curve
|
| 86 |
plt.figure()
|
| 87 |
-
plt.plot(base['fpr'], base['tpr'], label=f"
|
| 88 |
-
plt.plot(best['fpr'], best['tpr'], label=f"
|
| 89 |
plt.plot([0, 1], [0, 1], 'k--', alpha=0.5)
|
| 90 |
plt.xlabel("False Positive Rate")
|
| 91 |
plt.ylabel("True Positive Rate")
|
|
@@ -95,8 +103,8 @@ def evaluate(file):
|
|
| 95 |
|
| 96 |
# Combined PR Curve
|
| 97 |
plt.figure()
|
| 98 |
-
plt.plot(base['recall_vals'], base['precision_vals'], label=f"
|
| 99 |
-
plt.plot(best['recall_vals'], best['precision_vals'], label=f"
|
| 100 |
plt.xlabel("Recall")
|
| 101 |
plt.ylabel("Precision")
|
| 102 |
plt.title("Combined Precision-Recall Curve")
|
|
@@ -104,9 +112,9 @@ def evaluate(file):
|
|
| 104 |
pr_fig = plt.gcf()
|
| 105 |
|
| 106 |
# Confusion Matrices
|
| 107 |
-
cm_fig = plot_confusion_matrices(base_model, best_model, X, y)
|
| 108 |
|
| 109 |
-
combined_metrics = f"π Base Model:\n{base['metrics']}\n\nπ Best Model:\n{best['metrics']}"
|
| 110 |
|
| 111 |
return combined_metrics, roc_fig, pr_fig, cm_fig
|
| 112 |
|
|
|
|
| 51 |
"""
|
| 52 |
}
|
| 53 |
|
| 54 |
+
def plot_confusion_matrices(model1, model2, X, y, labels=["Logistic Regression", "Gradient Boosting"], class_names=None, value_fontsize=28, value_color='red'):
|
|
|
|
| 55 |
fig, axes = plt.subplots(1, 2, figsize=(16, 8))
|
|
|
|
| 56 |
|
| 57 |
for i, model in enumerate([model1, model2]):
|
| 58 |
y_pred = model.predict(X)
|
| 59 |
cm = confusion_matrix(y, y_pred)
|
| 60 |
+
|
| 61 |
+
# Plot with default text
|
| 62 |
+
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=class_names)
|
| 63 |
disp.plot(ax=axes[i], cmap='Blues', colorbar=False)
|
| 64 |
+
|
| 65 |
+
# Remove default text artists
|
| 66 |
+
for artist in axes[i].texts:
|
| 67 |
+
artist.set_visible(False)
|
| 68 |
+
|
| 69 |
+
# Manually annotate with custom font and color
|
| 70 |
for (j, k), val in np.ndenumerate(cm):
|
| 71 |
+
axes[i].text(k, j, f"{val}", ha='center', va='center', fontsize=value_fontsize, color=value_color)
|
| 72 |
+
|
| 73 |
+
axes[i].set_title(f"{labels[i]}", fontsize=30)
|
| 74 |
+
axes[i].tick_params(axis='both', labelsize=24)
|
| 75 |
+
axes[i].set_xlabel("Predicted Default", fontsize=24)
|
| 76 |
+
axes[i].set_ylabel("Actual Default", fontsize=24)
|
| 77 |
|
| 78 |
plt.tight_layout()
|
| 79 |
return plt.gcf()
|
|
|
|
| 92 |
|
| 93 |
# Combined ROC Curve
|
| 94 |
plt.figure()
|
| 95 |
+
plt.plot(base['fpr'], base['tpr'], label=f"Logistic Regression (AUC={auc(base['fpr'], base['tpr']):.2f})", linestyle='--')
|
| 96 |
+
plt.plot(best['fpr'], best['tpr'], label=f"Gradient Boosting (AUC={auc(best['fpr'], best['tpr']):.2f})", linestyle='-')
|
| 97 |
plt.plot([0, 1], [0, 1], 'k--', alpha=0.5)
|
| 98 |
plt.xlabel("False Positive Rate")
|
| 99 |
plt.ylabel("True Positive Rate")
|
|
|
|
| 103 |
|
| 104 |
# Combined PR Curve
|
| 105 |
plt.figure()
|
| 106 |
+
plt.plot(base['recall_vals'], base['precision_vals'], label=f"Logistic Regression (AUC={auc(base['recall_vals'], base['precision_vals']):.2f})", linestyle='--')
|
| 107 |
+
plt.plot(best['recall_vals'], best['precision_vals'], label=f"Gradient Boosting (AUC={auc(best['recall_vals'], best['precision_vals']):.2f})", linestyle='-')
|
| 108 |
plt.xlabel("Recall")
|
| 109 |
plt.ylabel("Precision")
|
| 110 |
plt.title("Combined Precision-Recall Curve")
|
|
|
|
| 112 |
pr_fig = plt.gcf()
|
| 113 |
|
| 114 |
# Confusion Matrices
|
| 115 |
+
cm_fig = plot_confusion_matrices(base_model, best_model, X, y, class_names=["Negative", "Positive"], value_fontsize=32, value_color='red')
|
| 116 |
|
| 117 |
+
combined_metrics = f"π Base Model:\n(Logistic Regression){base['metrics']}\n\nπ Best Model:\n(Gradient Boosting){best['metrics']}"
|
| 118 |
|
| 119 |
return combined_metrics, roc_fig, pr_fig, cm_fig
|
| 120 |
|