Harika22 commited on
Commit
638b2d1
Β·
verified Β·
1 Parent(s): 523fbe0

Update pages/13_Linear_Regression.py

Browse files
Files changed (1) hide show
  1. pages/13_Linear_Regression.py +141 -0
pages/13_Linear_Regression.py CHANGED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ st.set_page_config(page_title="Linear Regression", page_icon="πŸ“Š", layout="wide")
4
+
5
+ st.markdown("<h1 style='text-align: center;'>πŸ“ˆ Linear Regression: A Visual and Theoretical Guide</h1>", unsafe_allow_html=True)
6
+
7
+ section = st.sidebar.radio(
8
+ "πŸ” Explore Topics",
9
+ [
10
+ "πŸ“˜ What is Linear Regression?",
11
+ "πŸ“ Best Fit Line",
12
+ "πŸ”§ Training (Simple Linear Regression)",
13
+ "πŸ” Testing Phase",
14
+ "πŸ“Š Multiple Linear Regression",
15
+ "βš™οΈ Gradient Descent",
16
+ "πŸ“ Assumptions",
17
+ "πŸ“Š Evaluation Metrics",
18
+ "πŸ““ Colab Notebook",
19
+ ]
20
+ )
21
+
22
+ if section == "πŸ“˜ What is Linear Regression?":
23
+ st.subheader("πŸ“˜ What is Linear Regression?")
24
+ st.write("""
25
+ Linear Regression is a **Supervised Learning Algorithm** used to predict **continuous values**.
26
+ - It models the relationship between the **dependent variable (target)** and one or more **independent variables (features)**.
27
+ - The goal is to fit the **best straight line** that minimizes the error.
28
+ """)
29
+
30
+ elif section == "πŸ“ Best Fit Line":
31
+ st.subheader("πŸ“ What is the Best Fit Line?")
32
+ st.write("""
33
+ A **best fit line**:
34
+ - Minimizes the **Mean Squared Error (MSE)**
35
+ - Can be found using **Ordinary Least Squares (OLS)** or **Gradient Descent**
36
+
37
+ #### Simple Linear Equation:
38
+ $$
39
+ \hat{y} = w_1 x + w_0
40
+ $$
41
+ - \( w_1 \): slope (coefficient)
42
+ - \( w_0 \): intercept (bias)
43
+ """)
44
+
45
+ elif section == "πŸ”§ Training (Simple Linear Regression)":
46
+ st.subheader("πŸ”§ Training: Simple Linear Regression")
47
+ st.write("""
48
+ Used when there’s only **one feature**.
49
+
50
+ **Steps to Train:**
51
+ 1. Initialize weights: \( w_1, w_0 \)
52
+ 2. Predict: \( \hat{y} = w_1 x + w_0 \)
53
+ 3. Calculate **Mean Squared Error (MSE)**:
54
+ $$
55
+ \text{MSE} = \frac{1}{n} \sum (\hat{y}_i - y_i)^2
56
+ $$
57
+ 4. Optimize weights using **Gradient Descent**
58
+ """)
59
+
60
+ elif section == "πŸ” Testing Phase":
61
+ st.subheader("πŸ” Prediction (Testing Phase)")
62
+ st.write("""
63
+ Once trained, the model can predict new outcomes:
64
+
65
+ **Given new input \( x \):**
66
+ $$
67
+ \hat{y} = w_1 x + w_0
68
+ $$
69
+
70
+ - Compare predicted \( \hat{y} \) with actual \( y \) (if known)
71
+ """)
72
+
73
+ elif section == "πŸ“Š Multiple Linear Regression":
74
+ st.subheader("πŸ“Š Multiple Linear Regression")
75
+ st.write("""
76
+ Predicts using **multiple features**.
77
+
78
+ #### Equation:
79
+ $$
80
+ \hat{y} = w_1 x_1 + w_2 x_2 + \dots + w_n x_n + w_0
81
+ $$
82
+
83
+ - Each input feature has its own weight
84
+ - Use same process: predict β†’ calculate loss β†’ optimize
85
+ """)
86
+
87
+ elif section == "βš™οΈ Gradient Descent":
88
+ st.subheader("βš™οΈ Gradient Descent Optimization")
89
+ st.write("""
90
+ **Goal:** Minimize the loss function (like MSE)
91
+
92
+ #### Update Rule:
93
+ $$
94
+ w := w - \alpha \cdot \frac{\partial \text{MSE}}{\partial w}
95
+ $$
96
+
97
+ - \( \alpha \): learning rate
98
+ - Choose carefully:
99
+ - Too high β†’ overshoot
100
+ - Too low β†’ slow convergence
101
+ - Common choices: 0.01, 0.1
102
+ """)
103
+
104
+ elif section == "πŸ“ Assumptions":
105
+ st.subheader("πŸ“ Assumptions of Linear Regression")
106
+ st.write("""
107
+ 1. **Linearity**: Relationship between variables is linear
108
+ 2. **No Multicollinearity**: Features shouldn't be highly correlated
109
+ 3. **Homoscedasticity**: Constant variance of residuals
110
+ 4. **Normality of Errors**: Errors are normally distributed
111
+ 5. **No Autocorrelation**: Errors should not be related across observations
112
+ """)
113
+
114
+ elif section == "πŸ“Š Evaluation Metrics":
115
+ st.subheader("πŸ“Š Evaluation Metrics for Linear Regression")
116
+ st.write("""
117
+ - **Mean Squared Error (MSE)**:
118
+ $$
119
+ \text{MSE} = \frac{1}{n} \sum (\hat{y}_i - y_i)^2
120
+ $$
121
+ - **Mean Absolute Error (MAE)**:
122
+ $$
123
+ \text{MAE} = \frac{1}{n} \sum |\hat{y}_i - y_i|
124
+ $$
125
+ - **R-squared ( \( R^2 \) )**:
126
+ $$
127
+ R^2 = 1 - \frac{SS_{res}}{SS_{tot}}
128
+ $$
129
+ Measures how well the model explains the variance in data.
130
+ """)
131
+
132
+ elif section == "πŸ““ Colab Notebook":
133
+ st.subheader("πŸ““ Hands-On Implementation in Google Colab")
134
+ st.markdown("""
135
+ <a href='https://colab.research.google.com/drive/11-Rv7BC2PhOqk5hnpdXo6QjqLLYLDvTD?usp=sharing' target='_blank'>
136
+ πŸ”— Click here to open the Linear Regression Notebook in Colab
137
+ </a>
138
+ """, unsafe_allow_html=True)
139
+
140
+ st.markdown("---")
141
+ st.success("Mastering Linear Regression is essential β€” it's the foundation for many advanced models in machine learning!")