Spaces:
Sleeping
Sleeping
Update pages/Logistic Regression.py
Browse files- pages/Logistic Regression.py +76 -82
pages/Logistic Regression.py
CHANGED
|
@@ -1,113 +1,107 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
-
st.set_page_config(page_title="Logistic Regression
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
st.
|
| 7 |
|
|
|
|
|
|
|
| 8 |
st.markdown("""
|
| 9 |
-
Logistic Regression is a **classification algorithm** used to predict
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
""")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
st.markdown("""
|
| 25 |
-
-
|
| 26 |
-
-
|
| 27 |
-
- \\( \sigma(z) \\): Sigmoid function that squashes output between 0 and 1
|
| 28 |
""")
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
st.header("โ๏ธ How Logistic Regression Works")
|
| 33 |
-
col1, col2 = st.columns(2)
|
| 34 |
-
|
| 35 |
-
with col1:
|
| 36 |
-
st.markdown("### ๐งฎ Step-by-step:")
|
| 37 |
-
st.markdown("""
|
| 38 |
-
1. Compute linear equation \\( z \\)
|
| 39 |
-
2. Apply sigmoid: \\( \hat{y} = \frac{1}{1 + e^{-z}} \\)
|
| 40 |
-
3. Threshold output:
|
| 41 |
-
- \\( \hat{y} > 0.5 \\) โ Class 1
|
| 42 |
-
- \\( \hat{y} โค 0.5 \\) โ Class 0
|
| 43 |
-
""")
|
| 44 |
-
with col2:
|
| 45 |
-
st.image("https://upload.wikimedia.org/wikipedia/commons/8/88/Logistic-curve.svg",
|
| 46 |
-
caption="Sigmoid Curve โ S-Shaped", use_column_width=True)
|
| 47 |
-
|
| 48 |
-
# SECTION 3 โ Use Cases
|
| 49 |
-
with st.container():
|
| 50 |
-
st.header("๐ Real-World Use Cases")
|
| 51 |
st.markdown("""
|
| 52 |
-
-
|
| 53 |
-
-
|
| 54 |
-
|
| 55 |
-
- ๐ Churn Prediction
|
| 56 |
-
- ๐ Search Ranking Classification
|
| 57 |
""")
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
st.latex(r"Loss = - \left[ y \cdot \log(\hat{y}) + (1 - y) \cdot \log(1 - \hat{y}) \right]")
|
| 64 |
st.markdown("""
|
| 65 |
-
-
|
| 66 |
-
-
|
| 67 |
""")
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
st.header("
|
|
|
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
st.latex(r"Accuracy = \frac{TP + TN}{TP + TN + FP + FN}")
|
| 77 |
-
st.
|
| 78 |
|
| 79 |
-
with
|
| 80 |
-
st.subheader("๐ฏ Precision")
|
| 81 |
st.latex(r"Precision = \frac{TP}{TP + FP}")
|
| 82 |
-
st.
|
| 83 |
|
| 84 |
-
with
|
| 85 |
-
st.subheader("๐ Recall")
|
| 86 |
st.latex(r"Recall = \frac{TP}{TP + FN}")
|
| 87 |
-
st.
|
| 88 |
-
|
| 89 |
-
col4, col5 = st.columns(2)
|
| 90 |
|
| 91 |
-
with
|
| 92 |
-
st.subheader("๐ F1-Score")
|
| 93 |
st.latex(r"F1 = 2 \cdot \frac{Precision \cdot Recall}{Precision + Recall}")
|
| 94 |
-
st.
|
| 95 |
|
| 96 |
-
with
|
| 97 |
-
st.subheader("๐ง ROC-AUC")
|
| 98 |
st.markdown("""
|
| 99 |
-
-
|
| 100 |
-
-
|
| 101 |
-
- AUC = area under ROC curve
|
| 102 |
""")
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
st.header("
|
| 106 |
st.markdown("""
|
| 107 |
-
- Logistic Regression is
|
| 108 |
-
-
|
| 109 |
-
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
- Use regularization (`L1`, `L2`) to avoid overfitting
|
| 113 |
""")
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
+
st.set_page_config(page_title="Logistic Regression", page_icon="๐", layout="wide")
|
| 4 |
|
| 5 |
+
# Title
|
| 6 |
+
st.markdown("<h1 style='text-align: center;'>๐ Logistic Regression - Made Simple</h1>", unsafe_allow_html=True)
|
| 7 |
|
| 8 |
+
# What is Logistic Regression?
|
| 9 |
+
st.header("๐ What is Logistic Regression?")
|
| 10 |
st.markdown("""
|
| 11 |
+
Logistic Regression is a **classification algorithm** used to predict **discrete outcomes** (like Yes/No, 0/1, Spam/Not Spam).
|
| 12 |
+
|
| 13 |
+
Think of it like:
|
| 14 |
+
> โBased on your features (like age, income, health), should you get insurance? Yes or No?โ
|
| 15 |
+
|
| 16 |
+
### Key Facts:
|
| 17 |
+
- It's a **supervised learning** algorithm.
|
| 18 |
+
- Despite the name, itโs used for **classification**, not regression.
|
| 19 |
+
- It outputs a **probability** between 0 and 1 using the **sigmoid function**.
|
| 20 |
""")
|
| 21 |
|
| 22 |
+
# Use Cases
|
| 23 |
+
st.header("๐ฏ Real-World Use Cases")
|
| 24 |
+
st.markdown("""
|
| 25 |
+
- ๐ง **Spam Detection**: Is this email spam or not?
|
| 26 |
+
- ๐ฅ **Medical Diagnosis**: Does a patient have diabetes or not?
|
| 27 |
+
- ๐ฅ **Customer Churn**: Will a customer leave the company?
|
| 28 |
+
- ๐ณ **Fraud Detection**: Is this transaction fraudulent?
|
| 29 |
+
""")
|
| 30 |
+
|
| 31 |
+
# How it works
|
| 32 |
+
st.header("โ๏ธ How Does Logistic Regression Work?")
|
| 33 |
+
st.markdown("Let's break it down step-by-step:")
|
| 34 |
+
|
| 35 |
+
with st.expander("๐ข Step 1: Make a Linear Equation (just like Linear Regression)"):
|
| 36 |
+
st.latex(r"z = w_0 + w_1x_1 + w_2x_2 + \ldots + w_nx_n")
|
| 37 |
st.markdown("""
|
| 38 |
+
- We calculate a **weighted sum** of the input features.
|
| 39 |
+
- It's just a straight line equation where weights decide how important each feature is.
|
|
|
|
| 40 |
""")
|
| 41 |
|
| 42 |
+
with st.expander("๐งช Step 2: Pass it into a Sigmoid Function"):
|
| 43 |
+
st.latex(r"\sigma(z) = \frac{1}{1 + e^{-z}}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
st.markdown("""
|
| 45 |
+
- The sigmoid turns the output into a **probability** between 0 and 1.
|
| 46 |
+
- If the output is close to 1 โ Class 1
|
| 47 |
+
If close to 0 โ Class 0
|
|
|
|
|
|
|
| 48 |
""")
|
| 49 |
|
| 50 |
+
st.image("https://upload.wikimedia.org/wikipedia/commons/8/88/Logistic-curve.svg",
|
| 51 |
+
caption="S-Shaped Sigmoid Curve", use_column_width=True)
|
| 52 |
+
|
| 53 |
+
with st.expander("๐ง Step 3: Make the Final Prediction"):
|
|
|
|
| 54 |
st.markdown("""
|
| 55 |
+
- If \\( \sigma(z) > 0.5 \\), we predict **Class 1**
|
| 56 |
+
- Else, we predict **Class 0**
|
| 57 |
""")
|
| 58 |
|
| 59 |
+
# Visualize the Flow
|
| 60 |
+
st.header("๐ Visualization of the Process")
|
| 61 |
+
st.markdown("Feature inputs โ Weighted sum โ Sigmoid function โ Probability โ Class label")
|
| 62 |
|
| 63 |
+
# Loss Function
|
| 64 |
+
st.header("โ Loss Function - Binary Cross Entropy")
|
| 65 |
+
st.markdown("To improve the model, we need to know **how wrong it is**. Thatโs where the loss function comes in.")
|
| 66 |
+
st.latex(r"Loss = - [y \cdot \log(\hat{y}) + (1 - y) \cdot \log(1 - \hat{y})]")
|
| 67 |
+
st.markdown("""
|
| 68 |
+
- If predicted probability (\\( \hat{y} \\)) is far from actual label (\\( y \\)), the loss is high.
|
| 69 |
+
- This helps adjust weights to improve predictions.
|
| 70 |
+
""")
|
| 71 |
|
| 72 |
+
# Evaluation Metrics
|
| 73 |
+
st.header("๐ Evaluation Metrics Explained Simply")
|
| 74 |
+
|
| 75 |
+
with st.expander("1๏ธโฃ Accuracy"):
|
| 76 |
st.latex(r"Accuracy = \frac{TP + TN}{TP + TN + FP + FN}")
|
| 77 |
+
st.markdown("**How often** did we get it right?")
|
| 78 |
|
| 79 |
+
with st.expander("2๏ธโฃ Precision (When you care about False Positives)"):
|
|
|
|
| 80 |
st.latex(r"Precision = \frac{TP}{TP + FP}")
|
| 81 |
+
st.markdown("Out of all predicted **Positives**, how many were **correct**?")
|
| 82 |
|
| 83 |
+
with st.expander("3๏ธโฃ Recall (When you care about False Negatives)"):
|
|
|
|
| 84 |
st.latex(r"Recall = \frac{TP}{TP + FN}")
|
| 85 |
+
st.markdown("Out of all actual **Positives**, how many did we catch?")
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
with st.expander("4๏ธโฃ F1 Score (Balance between Precision and Recall)"):
|
|
|
|
| 88 |
st.latex(r"F1 = 2 \cdot \frac{Precision \cdot Recall}{Precision + Recall}")
|
| 89 |
+
st.markdown("It balances both **precision** and **recall**.")
|
| 90 |
|
| 91 |
+
with st.expander("5๏ธโฃ ROC-AUC"):
|
|
|
|
| 92 |
st.markdown("""
|
| 93 |
+
- ๐ ROC: Curve that plots **True Positive Rate** vs **False Positive Rate**
|
| 94 |
+
- ๐ง AUC: Area Under Curve โ closer to 1 = better performance
|
|
|
|
| 95 |
""")
|
| 96 |
|
| 97 |
+
# Summary
|
| 98 |
+
st.header("โ
Quick Summary")
|
| 99 |
st.markdown("""
|
| 100 |
+
- Logistic Regression is great for **binary and multi-class classification**
|
| 101 |
+
- Uses **sigmoid function** to map outputs to probability
|
| 102 |
+
- Optimizes using **log loss**
|
| 103 |
+
- Simple, fast, and interpretable model โ best for **linearly separable** data
|
| 104 |
+
- Evaluate using metrics like Accuracy, Precision, Recall, F1, and ROC-AUC
|
|
|
|
| 105 |
""")
|
| 106 |
+
|
| 107 |
+
st.success("๐ Great job! You've just mastered the basics of Logistic Regression.")
|