sree4411 commited on
Commit
16bf702
ยท
verified ยท
1 Parent(s): 5cd2c37

Update pages/Logistic Regression.py

Browse files
Files changed (1) hide show
  1. 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 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
  """)
 
 
 
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.")