Spaces:
Sleeping
Sleeping
Update pages/Logistic Regression.py
Browse files- pages/Logistic Regression.py +113 -0
pages/Logistic Regression.py
CHANGED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
st.set_page_config(page_title="Logistic Regression Explained", page_icon="๐", layout="wide")
|
| 4 |
+
|
| 5 |
+
# Header
|
| 6 |
+
st.title("๐ Logistic Regression (Classification)")
|
| 7 |
+
|
| 8 |
+
st.markdown("""
|
| 9 |
+
Logistic Regression is a **classification algorithm** used to predict the probability of a binary outcome.
|
| 10 |
+
Despite its name, itโs used for **classification**, not regression!
|
| 11 |
+
""")
|
| 12 |
+
|
| 13 |
+
# SECTION 1 โ Concept
|
| 14 |
+
with st.container():
|
| 15 |
+
st.header("๐ง Core Idea")
|
| 16 |
+
st.markdown("""
|
| 17 |
+
Logistic Regression predicts the **probability** of an event happening.
|
| 18 |
+
It applies the **Sigmoid (Logistic)** function to a linear equation.
|
| 19 |
+
|
| 20 |
+
**General Formula:**
|
| 21 |
+
""")
|
| 22 |
+
st.latex(r"\hat{y} = \sigma(z) = \frac{1}{1 + e^{-z}}")
|
| 23 |
+
st.latex(r"z = w_0 + w_1x_1 + w_2x_2 + ... + w_nx_n")
|
| 24 |
+
st.markdown("""
|
| 25 |
+
- \\( \hat{y} \\): predicted probability
|
| 26 |
+
- \\( z \\): linear combination of inputs
|
| 27 |
+
- \\( \sigma(z) \\): Sigmoid function that squashes output between 0 and 1
|
| 28 |
+
""")
|
| 29 |
+
|
| 30 |
+
# SECTION 2 โ How it Works
|
| 31 |
+
with st.container():
|
| 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 |
+
- ๐ Spam Detection
|
| 53 |
+
- ๐งฌ Disease Diagnosis
|
| 54 |
+
- ๐ณ Credit Card Fraud
|
| 55 |
+
- ๐ Churn Prediction
|
| 56 |
+
- ๐ Search Ranking Classification
|
| 57 |
+
""")
|
| 58 |
+
|
| 59 |
+
# SECTION 4 โ Loss Function
|
| 60 |
+
with st.container():
|
| 61 |
+
st.header("โ Loss Function: Binary Cross Entropy")
|
| 62 |
+
st.markdown("Logistic regression minimizes this loss function:")
|
| 63 |
+
st.latex(r"Loss = - \left[ y \cdot \log(\hat{y}) + (1 - y) \cdot \log(1 - \hat{y}) \right]")
|
| 64 |
+
st.markdown("""
|
| 65 |
+
- Encourages the model to assign high probabilities to the correct class.
|
| 66 |
+
- Used to update weights via **gradient descent**.
|
| 67 |
+
""")
|
| 68 |
+
|
| 69 |
+
# SECTION 5 โ Evaluation Metrics
|
| 70 |
+
st.header("๐ Evaluation Metrics")
|
| 71 |
+
|
| 72 |
+
col1, col2, col3 = st.columns(3)
|
| 73 |
+
|
| 74 |
+
with col1:
|
| 75 |
+
st.subheader("โ๏ธ Accuracy")
|
| 76 |
+
st.latex(r"Accuracy = \frac{TP + TN}{TP + TN + FP + FN}")
|
| 77 |
+
st.caption("Correct predictions over all predictions")
|
| 78 |
+
|
| 79 |
+
with col2:
|
| 80 |
+
st.subheader("๐ฏ Precision")
|
| 81 |
+
st.latex(r"Precision = \frac{TP}{TP + FP}")
|
| 82 |
+
st.caption("How many predicted positives were actually positive?")
|
| 83 |
+
|
| 84 |
+
with col3:
|
| 85 |
+
st.subheader("๐ Recall")
|
| 86 |
+
st.latex(r"Recall = \frac{TP}{TP + FN}")
|
| 87 |
+
st.caption("How many actual positives did we catch?")
|
| 88 |
+
|
| 89 |
+
col4, col5 = st.columns(2)
|
| 90 |
+
|
| 91 |
+
with col4:
|
| 92 |
+
st.subheader("๐ F1-Score")
|
| 93 |
+
st.latex(r"F1 = 2 \cdot \frac{Precision \cdot Recall}{Precision + Recall}")
|
| 94 |
+
st.caption("Balances precision and recall")
|
| 95 |
+
|
| 96 |
+
with col5:
|
| 97 |
+
st.subheader("๐ง ROC-AUC")
|
| 98 |
+
st.markdown("""
|
| 99 |
+
- Measures performance across all thresholds
|
| 100 |
+
- Plots TPR vs FPR
|
| 101 |
+
- AUC = area under ROC curve
|
| 102 |
+
""")
|
| 103 |
+
|
| 104 |
+
# SECTION 6 โ Key Takeaways
|
| 105 |
+
st.header("๐ Summary & Tips")
|
| 106 |
+
st.markdown("""
|
| 107 |
+
- Logistic Regression is simple, interpretable, and works well for **linearly separable data**
|
| 108 |
+
- Assumes no multicollinearity, independent features
|
| 109 |
+
- Works best when:
|
| 110 |
+
- You need quick, interpretable results
|
| 111 |
+
- The relationship between features and class is linear in log-odds
|
| 112 |
+
- Use regularization (`L1`, `L2`) to avoid overfitting
|
| 113 |
+
""")
|