Harika22 commited on
Commit
0b26c9b
ยท
verified ยท
1 Parent(s): 51077d9

Update pages/12_Logistic_Regression.py

Browse files
Files changed (1) hide show
  1. pages/12_Logistic_Regression.py +165 -0
pages/12_Logistic_Regression.py CHANGED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ st.set_page_config(page_title="Logistic Regression", page_icon="๐Ÿค–", layout="wide")
4
+
5
+ st.markdown("""
6
+ <style>
7
+ .stApp {
8
+ background-color: #1e1e1e;
9
+ color: white;
10
+ }
11
+ h1, h2, h3 {
12
+ color: #FF4C60;
13
+ }
14
+ .sidebar .sidebar-content {
15
+ background-color: #1e1e1e;
16
+ }
17
+ a {
18
+ color: #58a6ff;
19
+ text-decoration: none;
20
+ }
21
+ a:hover {
22
+ color: #1f78d1;
23
+ }
24
+ </style>
25
+ """, unsafe_allow_html=True)
26
+
27
+ st.sidebar.title("๐Ÿค– Logistic Regression")
28
+ st.sidebar.markdown("Explore the theory behind Logistic Regression step-by-step.")
29
+ st.sidebar.markdown("---")
30
+
31
+ st.markdown("<h1 style='text-align: center;'>๐Ÿ“ˆ Logistic Regression (Theory)</h1>", unsafe_allow_html=True)
32
+
33
+ with st.expander("๐Ÿ“˜ What is Logistic Regression?"):
34
+ st.write("""
35
+ Logistic Regression is a **supervised learning algorithm** used for **classification tasks**.
36
+ - Mostly used for **binary classification**
37
+ - Can be extended to **multi-class** using techniques like **Softmax** or **One-vs-Rest (OvR)**
38
+
39
+ ๐Ÿ” The goal is to find a **hyperplane** that linearly separates the classes.
40
+ โš ๏ธ Assumption: The data should be **linearly separable** or nearly so.
41
+ """)
42
+
43
+ with st.expander("โš™๏ธ Step vs Sigmoid Function"):
44
+ st.write("""
45
+ Initially, people used **step function** for classification:
46
+
47
+ ```text
48
+ Step Function Output:
49
+ if z >= 0 โ†’ Class 1
50
+ if z < 0 โ†’ Class 0
51
+ ```
52
+
53
+ โŒ But it's not differentiable โ†’ can't optimize via gradient descent.
54
+ โœ… Instead, we use the **Sigmoid Function**:
55
+ $$
56
+ \\sigma(z) = \\frac{1}{1 + e^{-z}} \\quad \\text{where } z = w^T x + b
57
+ $$
58
+ Properties:
59
+ - \\( z >> 0 \\) โ†’ output โ‰ˆ 1
60
+ - \\( z << 0 \\) โ†’ output โ‰ˆ 0
61
+ - \\( z = 0 \\) โ†’ output = 0.5
62
+ """)
63
+
64
+ with st.expander("๐Ÿงฎ Loss Function in Logistic Regression"):
65
+ st.write("""
66
+ Logistic Regression uses **Log Loss** (or Binary Cross Entropy) for optimization:
67
+ $$
68
+ \\mathcal{L} = - \\left[y \\log(\\hat{y}) + (1 - y) \\log(1 - \\hat{y}) \\right]
69
+ $$
70
+ - \\( y \\) is the actual class (0 or 1)
71
+ - \\( \\hat{y} \\) is the predicted probability from sigmoid
72
+ - Minimize this loss using **Gradient Descent**
73
+ """)
74
+
75
+ with st.expander("๐Ÿ” Gradient Descent & Learning Rate"):
76
+ st.write("""
77
+ Gradient Descent updates weights to minimize loss:
78
+ $$
79
+ w = w - \\alpha \\cdot \\frac{\\partial \\mathcal{L}}{\\partial w}
80
+ $$
81
+
82
+ - \\( \\alpha \\): Learning rate
83
+ โš–๏ธ Choosing Learning Rate:
84
+ - Too high โ†’ overshoots
85
+ - Too low โ†’ slow convergence
86
+ - Recommended: 0.01 or 0.1 for starters
87
+
88
+ ๐Ÿ“ฆ Types:
89
+ - **Batch**: Uses full dataset each update
90
+ - **Stochastic (SGD)**: Updates per data point
91
+ - **Mini-Batch**: Updates per small batch โ†’ widely used
92
+ """)
93
+
94
+ with st.expander("๐Ÿ”ข Multiclass Logistic Regression"):
95
+ st.subheader("1๏ธโƒฃ Softmax Regression")
96
+ st.write("""
97
+ Generalizes binary logistic regression to multi-class using **Softmax Function**:
98
+ $$
99
+ \\text{Softmax}(z_i) = \\frac{e^{z_i}}{\\sum_j e^{z_j}}
100
+ $$
101
+ Each class gets a probability, and we pick the class with the highest.
102
+ Example: If predicted probabilities are:
103
+
104
+ ```text
105
+ [0.02, 0.05, 0.07, 0.10, 0.08, 0.12, 0.10, 0.30, 0.05, 0.11]
106
+ ```
107
+
108
+ โ†’ Predict class **7** (highest probability)
109
+ """)
110
+
111
+ st.subheader("2๏ธโƒฃ One-vs-Rest (OvR) Classification")
112
+ st.write("""
113
+ OvR builds one binary classifier **per class**:
114
+ - Apple vs Not-Apple
115
+ - Banana vs Not-Banana
116
+ - Orange vs Not-Orange
117
+
118
+ Then pick the class with the highest confidence score.
119
+ """)
120
+
121
+ with st.expander("๐Ÿงฒ Regularization in Logistic Regression"):
122
+ st.write("""
123
+ Regularization helps reduce **overfitting** by penalizing large weights.
124
+ - **L1 (Lasso)**: \\( \\lambda \\sum |w| \\) โ†’ Feature selection
125
+ - **L2 (Ridge)**: \\( \\lambda \\sum w^2 \\) โ†’ Smooths weights
126
+ - **ElasticNet**: Combines L1 + L2
127
+
128
+ โš–๏ธ Helps balance **bias vs variance**
129
+ """)
130
+
131
+ with st.expander("๐Ÿ” Detecting Multicollinearity"):
132
+ st.write("""
133
+ Multicollinearity = when predictors are highly correlated.
134
+ ๐ŸŽฏ Check using **VIF (Variance Inflation Factor)**:
135
+ - \\( \\text{VIF} > 10 \\) โ†’ High multicollinearity
136
+
137
+ โœ… Reduce it by:
138
+ - Removing correlated features
139
+ - Applying regularization
140
+ """)
141
+
142
+ with st.expander("โš™๏ธ Hyperparameters in Logistic Regression"):
143
+ st.table([
144
+ ["penalty", "Regularization type ('l1', 'l2', 'elasticnet', None)"],
145
+ ["dual", "Use dual formulation (for liblinear only)"],
146
+ ["tol", "Tolerance for stopping criteria"],
147
+ ["C", "Inverse of regularization strength"],
148
+ ["fit_intercept", "Whether to include the bias term"],
149
+ ["solver", "Optimization algorithm ('lbfgs', 'saga', etc.)"],
150
+ ["multi_class", "'ovr' or 'multinomial' for multi-class"],
151
+ ["max_iter", "Max iterations for convergence"],
152
+ ["class_weight", "'balanced' or manual dictionary"],
153
+ ["n_jobs", "Number of parallel jobs"],
154
+ ["l1_ratio", "Used with ElasticNet"],
155
+ ])
156
+
157
+ st.markdown("---")
158
+ st.markdown("### ๐Ÿ““ Want to see it in action? Open the Colab notebook below:")
159
+ st.markdown("""
160
+ <a href='https://colab.research.google.com/drive/1IiZmfkCcMltcE5-r5PbQqRpj_maR_M-f?usp=sharing' target='_blank'>
161
+ ๐Ÿ”— Open Logistic Regression Notebook
162
+ </a>
163
+ """, unsafe_allow_html=True)
164
+
165
+ st.success("Logistic Regression is simple yet powerful. Master it before diving into complex models!")