Spaces:
Sleeping
Sleeping
Update pages/13_Linear_Regression.py
Browse files- 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!")
|