Team Ai
Apppublic

Akhil4839/Machine_learning

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
11.Performance matrix.py139 linesDownload Raw Back to pages
1import streamlit as st2import numpy as np3import pandas as pd4from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, roc_auc_score5from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score6import matplotlib.pyplot as plt7import seaborn as sns8from sklearn.metrics import roc_curve, auc9 10# --- Page Configuration ---11st.set_page_config(page_title="๐Ÿ“Š ML Metrics", page_icon="๐Ÿค–", layout="wide")12 13# Title14st.title("๐Ÿ“Š Performance Metrics in Machine Learning")15 16st.write("""17Performance metrics help evaluate how well a machine learning model performs.18The choice of metric depends on whether you're solving a **classification** or **regression** problem.19""")20 21# --- Classification Metrics Section ---22with st.expander("๐Ÿงฎ Classification Metrics", expanded=True):23    24    # Accuracy25    st.subheader("๐Ÿ”น Accuracy")26    st.write("The ratio of correctly predicted instances to total instances.")27    st.code("Accuracy = (TP + TN) / (TP + TN + FP + FN)")28    st.latex(r'''Accuracy = \frac{TP + TN}{TP + TN + FP + FN}''')29    st.write("""30    **Use case**: Accuracy is best for balanced datasets where the number of classes is similar.31    However, it is not recommended for imbalanced datasets.32    """)33    st.markdown("**Example:** TP=50, TN=40, FP=5, FN=5 โ†’ Accuracy = 0.90")34 35    # Confusion Matrix36    st.subheader("๐Ÿ”น Confusion Matrix")37    st.write("A 2x2 matrix visualizing classification performance.")38    st.markdown("""39    - **TP (True Positives)**: Correctly predicted positive cases  40    - **TN (True Negatives)**: Correctly predicted negative cases  41    - **FP (False Positives)**: Incorrectly predicted positives  42    - **FN (False Negatives)**: Missed positive cases  43    """)44    st.markdown("**Example:**")45    st.code("""[[50, 5], [5, 40]]""")46    st.write("""47    Confusion Matrix provides the count of actual vs predicted classifications. It allows you to identify misclassifications.48    """)49 50    # Precision51    st.subheader("๐Ÿ”น Precision")52    st.write("Precision answers the question: **Of all predicted positive cases, how many were actually positive?**")53    st.code("Precision = TP / (TP + FP)")54    st.latex(r'''Precision = \frac{50}{50 + 5} = \frac{50}{55} \approx 0.91''')55    st.write("""56    **Use case**: Precision is particularly important in scenarios where false positives are costly, e.g., in medical diagnoses.57    """)58 59    # Recall60    st.subheader("๐Ÿ”น Recall (Sensitivity)")61    st.write("Recall answers the question: **Of all actual positive cases, how many were predicted as positive?**")62    st.code("Recall = TP / (TP + FN)")63    st.latex(r'''Recall = \frac{50}{50 + 5} = \frac{50}{55} \approx 0.91''')64    st.write("""65    **Use case**: Recall is important when missing a positive case is critical, e.g., identifying fraud or medical conditions.66    """)67 68    # F1 Score69    st.subheader("๐Ÿ”น F1 Score")70    st.write("F1 Score balances Precision and Recall and is the harmonic mean of the two. It's useful when you need to balance both.")71    st.code("F1 = 2 * (Precision * Recall) / (Precision + Recall)")72    st.latex(r'''F1 = 2 \times \frac{0.91 \times 0.91}{0.91 + 0.91} \approx 0.91''')73    st.write("""74    **Use case**: F1 Score is the best metric when you need to balance Precision and Recall, especially in imbalanced datasets.75    """)76 77    # ROC & AUC78    st.subheader("๐Ÿ”น ROC Curve & AUC")79    st.write("The ROC curve is a plot of the True Positive Rate (TPR) against the False Positive Rate (FPR). AUC is the area under this curve.")80    st.markdown("""81    - **AUC = 1.0**: Perfect model  82    - **AUC = 0.5**: Random guess (no better than chance)  83    """)84    st.markdown("**Example:** AUC of 0.95 means the model is highly capable of distinguishing classes.")85 86    # Log Loss87    st.subheader("๐Ÿ”น Log Loss")88    st.write("Log Loss evaluates how well the model's predicted probabilities align with the true labels. Lower values indicate better performance.")89    st.code("LogLoss = -1/N * sum(y*log(p) + (1-y)*log(1-p))")90    st.write("Log Loss penalizes wrong predictions, especially those that are far from the true probability.")91 92# --- Regression Metrics Section ---93with st.expander("๐Ÿ“ˆ Regression Metrics", expanded=True):94    95    # MSE96    st.subheader("๐Ÿ“Œ Mean Squared Error (MSE)")97    st.write("MSE is the average of the squared differences between the actual and predicted values. It is sensitive to outliers.")98    st.code("MSE = mean((actual - predicted)^2)")99    st.latex(r'''MSE = \frac{(0.5)^2 + (-0.5)^2 + (0)^2}{3} = 0.17''')100    st.write("""101    **Use case**: MSE is commonly used when the magnitude of error matters. It penalizes larger errors more due to squaring.102    """)103 104    # MAE105    st.subheader("๐Ÿ“Œ Mean Absolute Error (MAE)")106    st.write("MAE is the average of the absolute differences between the actual and predicted values. It is less sensitive to outliers.")107    st.code("MAE = mean(|actual - predicted|)")108    st.latex(r'''MAE = \frac{|0.5| + |0.5| + |0|}{3} = 0.33''')109    st.write("""110    **Use case**: MAE is more robust to outliers and provides a linear view of the error, often used when interpreting model errors is critical.111    """)112 113    # RMSE114    st.subheader("๐Ÿ“Œ Root Mean Squared Error (RMSE)")115    st.write("RMSE is the square root of MSE. It has the same unit as the target variable and is sensitive to outliers.")116    st.code("RMSE = sqrt(MSE)")117    st.latex(r'''RMSE = \sqrt{0.17} \approx 0.41''')118    st.write("""119    **Use case**: RMSE is ideal when you want to give more weight to larger errors.120    """)121 122    # R-squared123    st.subheader("๐Ÿ“Œ R-Squared (Rยฒ)")124    st.write("Rยฒ indicates how well the model explains the variance in the data. A higher Rยฒ means a better model fit.")125    st.code("Rยฒ = 1 - (SSR / SST)")126    st.write("""127    **Use case**: Rยฒ is helpful to assess the overall fit of the model, but it can be misleading in some cases (e.g., with non-linear data).128    """)129 130# --- Final Notes ---131st.subheader("๐Ÿ“ Final Notes")132st.markdown("""133- Always choose the metric that aligns with your goal:134    - For **imbalanced classification** โ†’ F1 Score, Precision, Recall135    - For **probability-based predictions** โ†’ Log Loss, AUC136    - For **regression** โ†’ MAE (robust), RMSE (sensitive to outliers), Rยฒ (interpretability)137- Use multiple metrics to evaluate a model holistically.138""")139