Update pages/5Linear_Regression.py
Browse files- pages/5Linear_Regression.py +114 -0
pages/5Linear_Regression.py
CHANGED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
# Page configuration
|
4 |
+
st.set_page_config(page_title="Linear Regression", page_icon="🤖", layout="wide")
|
5 |
+
|
6 |
+
# Custom CSS for styling
|
7 |
+
st.markdown("""
|
8 |
+
<style>
|
9 |
+
.stApp {
|
10 |
+
background-color: #ECECEC;
|
11 |
+
}
|
12 |
+
h1, h2, h3 {
|
13 |
+
color: #002244;
|
14 |
+
}
|
15 |
+
.custom-font, p {
|
16 |
+
font-family: 'Arial', sans-serif;
|
17 |
+
font-size: 18px;
|
18 |
+
color: black;
|
19 |
+
line-height: 1.6;
|
20 |
+
}
|
21 |
+
</style>
|
22 |
+
""", unsafe_allow_html=True)
|
23 |
+
|
24 |
+
# Title
|
25 |
+
st.markdown("<h1 style='color: #002244;'>Complete Overview of Linear Regression</h1>", unsafe_allow_html=True)
|
26 |
+
|
27 |
+
st.write("""
|
28 |
+
Linear Regression is a key **Supervised Learning** method mainly used for **regression problems**.
|
29 |
+
It predicts continuous outputs by identifying the best-fit line (or hyperplane) that minimizes the gap between actual and predicted values.
|
30 |
+
""")
|
31 |
+
|
32 |
+
# Understanding the Best Fit Line
|
33 |
+
st.markdown("<h2 style='color: #002244;'>What Defines the Best Fit Line?</h2>", unsafe_allow_html=True)
|
34 |
+
st.write("""
|
35 |
+
The ideal regression line is one that:
|
36 |
+
- Gets as close as possible to all data points.
|
37 |
+
- **Minimizes the error** between actual and predicted values.
|
38 |
+
- Is calculated using optimization techniques like **Ordinary Least Squares (OLS)** or **Gradient Descent**.
|
39 |
+
""")
|
40 |
+
st.image("linearregression.png", width=900)
|
41 |
+
|
42 |
+
# Training in Simple Linear Regression
|
43 |
+
st.markdown("<h2 style='color: #002244;'>Training Process: Simple Linear Regression</h2>", unsafe_allow_html=True)
|
44 |
+
st.write("""
|
45 |
+
Simple Linear Regression is typically used when there's only one or two features.
|
46 |
+
It models the relationship between \( x \) (independent variable) and \( y \) (dependent variable) as:
|
47 |
+
\[ y = w_1x + w_0 \]
|
48 |
+
Where:
|
49 |
+
- \( w_1 \) is the slope (weight)
|
50 |
+
- \( w_0 \) is the intercept (bias)
|
51 |
+
|
52 |
+
**Steps:**
|
53 |
+
- Initialize weights \( w_1 \), \( w_0 \) randomly.
|
54 |
+
- Predict output \( \hat{y} \) for each input \( x \).
|
55 |
+
- Compute **Mean Squared Error (MSE)** to assess prediction error.
|
56 |
+
- Update weights using **Gradient Descent**.
|
57 |
+
- Repeat until the error is minimized.
|
58 |
+
""")
|
59 |
+
|
60 |
+
# Testing Phase
|
61 |
+
st.markdown("<h2 style='color: #002244;'>Testing Phase</h2>", unsafe_allow_html=True)
|
62 |
+
st.write("""
|
63 |
+
Once trained:
|
64 |
+
1. Feed a new input \( x \).
|
65 |
+
2. Use the model to predict \( \hat{y} \).
|
66 |
+
3. Compare predicted value with actual to evaluate model accuracy.
|
67 |
+
""")
|
68 |
+
|
69 |
+
# Multiple Linear Regression
|
70 |
+
st.markdown("<h2 style='color: #002244;'>Multiple Linear Regression</h2>", unsafe_allow_html=True)
|
71 |
+
st.write("""
|
72 |
+
Multiple Linear Regression (MLR) extends simple linear regression by using multiple features to predict the output.
|
73 |
+
- Initialize all weights \( w_1, w_2, ..., w_n \) and \( w_0 \).
|
74 |
+
- Predict \( y \) for each data point.
|
75 |
+
- Calculate loss using **MSE**.
|
76 |
+
- Update weights via **Gradient Descent** to improve model accuracy.
|
77 |
+
""")
|
78 |
+
|
79 |
+
# Gradient Descent Optimization
|
80 |
+
st.markdown("<h2 style='color: #002244;'>Gradient Descent: Optimization Technique</h2>", unsafe_allow_html=True)
|
81 |
+
st.write("""
|
82 |
+
Gradient Descent is used to minimize the loss function:
|
83 |
+
- Begin with random weights and bias.
|
84 |
+
- Compute gradient (derivative) of the loss function.
|
85 |
+
- Update weights using the gradient:
|
86 |
+
\[ w = w - \alpha \frac{dL}{dw} \]
|
87 |
+
- Repeat until convergence (minimum loss is achieved).
|
88 |
+
- Suggested learning rates: **0.1, 0.01** — avoid very large values.
|
89 |
+
""")
|
90 |
+
|
91 |
+
# Assumptions of Linear Regression
|
92 |
+
st.markdown("<h2 style='color: #002244;'>Core Assumptions in Linear Regression</h2>", unsafe_allow_html=True)
|
93 |
+
st.write("""
|
94 |
+
1. **Linearity**: Relationship between input and output is linear.
|
95 |
+
2. **No Multicollinearity**: Features should not be highly correlated.
|
96 |
+
3. **Homoscedasticity**: Errors should have constant variance.
|
97 |
+
4. **Normality of Errors**: Residuals are normally distributed.
|
98 |
+
5. **No Autocorrelation**: Residuals are independent of each other.
|
99 |
+
""")
|
100 |
+
|
101 |
+
# Evaluation Metrics
|
102 |
+
st.markdown("<h2 style='color: #002244;'>Model Evaluation Metrics</h2>", unsafe_allow_html=True)
|
103 |
+
st.write("""
|
104 |
+
- **Mean Squared Error (MSE)**: Average squared error between predicted and actual values.
|
105 |
+
- **Mean Absolute Error (MAE)**: Average absolute difference.
|
106 |
+
- **R-squared (R²)**: Proportion of variance explained by the model.
|
107 |
+
""")
|
108 |
+
|
109 |
+
# Jupyter Notebook Link
|
110 |
+
st.markdown("<h2 style='color: #002244;'>Practice Notebook: Linear Regression Implementation</h2>", unsafe_allow_html=True)
|
111 |
+
st.markdown("<a href='https://colab.research.google.com/drive/11-Rv7BC2PhOqk5hnpdXo6QjqLLYLDvTD?usp=sharing' target='_blank' style='font-size: 16px; color: #002244;'>Click here to open the Jupyter Notebook</a>", unsafe_allow_html=True)
|
112 |
+
|
113 |
+
# Closing Message
|
114 |
+
st.write("Linear Regression remains a simple yet powerful tool. Understanding how it works under the hood—optimization, assumptions, and evaluation—helps in building better models.")
|