Spaces:
Sleeping
Sleeping
Create 16_Metrics.py
Browse files- pages/16_Metrics.py +122 -0
pages/16_Metrics.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
st.set_page_config(page_title="Model Evaluation Metrics", page_icon="📊", layout="wide")
|
| 4 |
+
|
| 5 |
+
# Custom styling
|
| 6 |
+
st.markdown("""
|
| 7 |
+
<style>
|
| 8 |
+
.stApp {
|
| 9 |
+
background-color: #1e1e1e;
|
| 10 |
+
color: white;
|
| 11 |
+
}
|
| 12 |
+
h1, h2, h3 {
|
| 13 |
+
color: #FF4C60;
|
| 14 |
+
}
|
| 15 |
+
.sidebar .sidebar-content {
|
| 16 |
+
background-color: #1e1e1e;
|
| 17 |
+
}
|
| 18 |
+
a {
|
| 19 |
+
color: #58a6ff;
|
| 20 |
+
}
|
| 21 |
+
</style>
|
| 22 |
+
""", unsafe_allow_html=True)
|
| 23 |
+
|
| 24 |
+
st.sidebar.title("📊 Evaluation Metrics")
|
| 25 |
+
st.sidebar.markdown("Learn how to evaluate model performance in classification and regression.")
|
| 26 |
+
|
| 27 |
+
# Title
|
| 28 |
+
st.markdown("<h1 style='text-align: center;'>📏 Model Evaluation Metrics</h1>", unsafe_allow_html=True)
|
| 29 |
+
|
| 30 |
+
# Classification Metrics
|
| 31 |
+
with st.expander("🎯 Classification Metrics"):
|
| 32 |
+
st.write("""
|
| 33 |
+
Classification metrics help assess how well your model performs in classifying data correctly.
|
| 34 |
+
""")
|
| 35 |
+
|
| 36 |
+
st.markdown("### 1. Accuracy")
|
| 37 |
+
st.write("""
|
| 38 |
+
Accuracy = (Correct Predictions) / (Total Predictions)
|
| 39 |
+
|
| 40 |
+
⚠️ Don't use accuracy if your dataset is **imbalanced** or predictions are **probabilistic**.
|
| 41 |
+
""")
|
| 42 |
+
|
| 43 |
+
st.markdown("### 2. Confusion Matrix")
|
| 44 |
+
st.write("""
|
| 45 |
+
A confusion matrix shows actual vs predicted classifications:
|
| 46 |
+
|
| 47 |
+
| | Predicted Positive | Predicted Negative |
|
| 48 |
+
|-------|--------------------|--------------------|
|
| 49 |
+
| Actual Positive | TP (True Positive) | FN (False Negative) |
|
| 50 |
+
| Actual Negative | FP (False Positive) | TN (True Negative) |
|
| 51 |
+
|
| 52 |
+
✅ Use this when you have **imbalanced classes**.
|
| 53 |
+
⚠️ Don't use if your model outputs probabilities.
|
| 54 |
+
""")
|
| 55 |
+
|
| 56 |
+
st.markdown("### 3. Precision")
|
| 57 |
+
st.latex(r"Precision = \frac{TP}{TP + FP}")
|
| 58 |
+
st.write("Precision is the proportion of true positives among all predicted positives.")
|
| 59 |
+
|
| 60 |
+
st.markdown("### 4. Recall")
|
| 61 |
+
st.latex(r"Recall = \frac{TP}{TP + FN}")
|
| 62 |
+
st.write("Recall is the proportion of actual positives correctly identified.")
|
| 63 |
+
|
| 64 |
+
st.markdown("### 5. F1 Score")
|
| 65 |
+
st.latex(r"F1 = 2 \cdot \frac{Precision \cdot Recall}{Precision + Recall}")
|
| 66 |
+
st.write("F1 Score is the harmonic mean of Precision and Recall.")
|
| 67 |
+
|
| 68 |
+
st.markdown("### 6. ROC Curve & AUC")
|
| 69 |
+
st.write("""
|
| 70 |
+
- **ROC Curve**: Plot of TPR vs. FPR.
|
| 71 |
+
- **AUC**: Area Under ROC Curve → higher is better.
|
| 72 |
+
|
| 73 |
+
Ideal ROC curve hugs the top-left corner.
|
| 74 |
+
""")
|
| 75 |
+
|
| 76 |
+
st.markdown("### 7. Log Loss")
|
| 77 |
+
st.latex(r"LogLoss = -\frac{1}{n} \sum \left[ y \log(\hat{y}) + (1 - y) \log(1 - \hat{y}) \right]")
|
| 78 |
+
st.write("""
|
| 79 |
+
- Penalizes wrong predictions more if they're confident.
|
| 80 |
+
- Best for **probability-based models**.
|
| 81 |
+
|
| 82 |
+
🔥 Lower Log Loss = better performance.
|
| 83 |
+
""")
|
| 84 |
+
|
| 85 |
+
# Regression Metrics
|
| 86 |
+
with st.expander("📈 Regression Metrics"):
|
| 87 |
+
st.write("Evaluate how close the predictions are to the actual continuous values.")
|
| 88 |
+
|
| 89 |
+
st.markdown("### 1. Mean Squared Error (MSE)")
|
| 90 |
+
st.latex(r"MSE = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2")
|
| 91 |
+
st.write("Measures average squared difference. Sensitive to outliers.")
|
| 92 |
+
|
| 93 |
+
st.markdown("### 2. Mean Absolute Error (MAE)")
|
| 94 |
+
st.latex(r"MAE = \frac{1}{n} \sum_{i=1}^{n} |y_i - \hat{y}_i|")
|
| 95 |
+
st.write("Measures average absolute difference. More robust to outliers.")
|
| 96 |
+
|
| 97 |
+
st.markdown("### 3. Root Mean Squared Error (RMSE)")
|
| 98 |
+
st.latex(r"RMSE = \sqrt{MSE}")
|
| 99 |
+
st.write("Same as MSE, but in original units.")
|
| 100 |
+
|
| 101 |
+
st.markdown("### 4. R² Score (Coefficient of Determination)")
|
| 102 |
+
st.latex(r"R^2 = 1 - \frac{SS_{res}}{SS_{tot}}")
|
| 103 |
+
st.write("""
|
| 104 |
+
Indicates how well the model explains the variance:
|
| 105 |
+
|
| 106 |
+
- **R² = 1** → Perfect model
|
| 107 |
+
- **0 < R² < 1** → Good model
|
| 108 |
+
- **R² = 0** → No better than the mean
|
| 109 |
+
- **R² < 0** → Worse than just predicting the mean
|
| 110 |
+
""")
|
| 111 |
+
|
| 112 |
+
# Summary
|
| 113 |
+
st.markdown("---")
|
| 114 |
+
st.markdown("### ✅ Choosing the Right Metric")
|
| 115 |
+
st.write("""
|
| 116 |
+
- For **Classification**: Use **F1-score**, **Log Loss**, and **Confusion Matrix**.
|
| 117 |
+
- For **Regression**: Use **R²**, **MAE**, or **RMSE**.
|
| 118 |
+
- ⚠️ **Avoid accuracy** in imbalanced datasets or when predicting probabilities.
|
| 119 |
+
- Always compare your model against a **baseline (dummy) model**.
|
| 120 |
+
""")
|
| 121 |
+
|
| 122 |
+
st.success("By understanding metrics well, you can evaluate and improve your models with confidence!")
|