Spaces:
Sleeping
Sleeping
Update pages/13_Linear_Regression.py
Browse files- pages/13_Linear_Regression.py +74 -115
pages/13_Linear_Regression.py
CHANGED
|
@@ -1,141 +1,100 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
st.set_page_config(page_title="Linear Regression", page_icon="π", layout="wide")
|
| 4 |
|
| 5 |
-
st.
|
| 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 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
- The goal is to fit the **best straight line** that minimizes the error.
|
| 28 |
-
""")
|
| 29 |
|
| 30 |
-
|
| 31 |
-
st.
|
| 32 |
-
st.
|
| 33 |
-
|
| 34 |
-
|
| 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 == "
|
| 46 |
-
st.
|
| 47 |
-
st.
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 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 == "
|
| 61 |
-
st.
|
| 62 |
-
st.
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
\
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
""")
|
| 72 |
|
| 73 |
-
elif section == "
|
| 74 |
-
st.
|
| 75 |
-
st.
|
| 76 |
-
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
""")
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 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 |
-
|
| 105 |
-
|
| 106 |
-
|
| 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 |
-
|
| 115 |
-
|
| 116 |
-
st.
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 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 == "
|
| 133 |
-
st.
|
| 134 |
st.markdown("""
|
| 135 |
<a href='https://colab.research.google.com/drive/11-Rv7BC2PhOqk5hnpdXo6QjqLLYLDvTD?usp=sharing' target='_blank'>
|
| 136 |
-
|
| 137 |
</a>
|
| 138 |
""", unsafe_allow_html=True)
|
| 139 |
|
| 140 |
st.markdown("---")
|
| 141 |
-
st.success("
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
from sklearn.linear_model import LinearRegression
|
| 6 |
+
from sklearn.metrics import mean_squared_error, r2_score
|
| 7 |
|
| 8 |
st.set_page_config(page_title="Linear Regression", page_icon="π", layout="wide")
|
| 9 |
|
| 10 |
+
st.title("π Linear Regression Explorer")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
section = st.radio(
|
| 13 |
+
"Navigate the Theory and Visuals",
|
| 14 |
+
["Introduction", "Best Fit Line", "Simple vs Multiple", "Gradient Descent", "Assumptions", "Evaluation Metrics", "Interactive Example", "Colab Notebook"],
|
| 15 |
+
horizontal=True
|
| 16 |
+
)
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
if section == "Introduction":
|
| 19 |
+
st.header("π What is Linear Regression?")
|
| 20 |
+
st.markdown("""
|
| 21 |
+
Linear Regression is a **Supervised Learning** algorithm used for predicting **continuous outcomes**.
|
| 22 |
+
The idea is to fit a line that best captures the relationship between input variables and the output variable.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
""")
|
| 24 |
|
| 25 |
+
elif section == "Best Fit Line":
|
| 26 |
+
st.header("π Best Fit Line")
|
| 27 |
+
st.latex(r"\hat{y} = w_1 x + w_0")
|
| 28 |
+
st.markdown("""
|
| 29 |
+
- \( w_1 \): Slope (how much \( y \) changes with \( x \))
|
| 30 |
+
- \( w_0 \): Intercept
|
| 31 |
+
- Found using **Ordinary Least Squares** or **Gradient Descent**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
""")
|
| 33 |
|
| 34 |
+
elif section == "Simple vs Multiple":
|
| 35 |
+
st.header("π§ Simple vs Multiple Linear Regression")
|
| 36 |
+
st.subheader("Simple Linear Regression")
|
| 37 |
+
st.latex(r"\hat{y} = w_1 x + w_0")
|
| 38 |
+
st.subheader("Multiple Linear Regression")
|
| 39 |
+
st.latex(r"\hat{y} = w_1 x_1 + w_2 x_2 + \dots + w_n x_n + w_0")
|
| 40 |
|
| 41 |
+
elif section == "Gradient Descent":
|
| 42 |
+
st.header("βοΈ Gradient Descent")
|
| 43 |
+
st.latex(r"w := w - \alpha \cdot \frac{\partial \text{Loss}}{\partial w}")
|
| 44 |
+
st.markdown("""
|
| 45 |
+
- \( \alpha \): Learning Rate
|
| 46 |
+
- Goal: Minimize **Mean Squared Error**
|
| 47 |
+
""")
|
| 48 |
|
| 49 |
+
elif section == "Assumptions":
|
| 50 |
+
st.header("π Assumptions of Linear Regression")
|
| 51 |
+
st.markdown("""
|
| 52 |
+
1. Linearity
|
| 53 |
+
2. No Multicollinearity
|
| 54 |
+
3. Homoscedasticity
|
| 55 |
+
4. Normality of residuals
|
| 56 |
+
5. No autocorrelation
|
| 57 |
""")
|
| 58 |
|
| 59 |
+
elif section == "Evaluation Metrics":
|
| 60 |
+
st.header("π Evaluation Metrics")
|
| 61 |
+
st.latex(r"MSE = \frac{1}{n} \sum (\hat{y}_i - y_i)^2")
|
| 62 |
+
st.latex(r"MAE = \frac{1}{n} \sum |\hat{y}_i - y_i|")
|
| 63 |
+
st.latex(r"R^2 = 1 - \frac{\text{SS}_{res}}{\text{SS}_{tot}}")
|
| 64 |
|
| 65 |
+
elif section == "Interactive Example":
|
| 66 |
+
st.header("π― Try Linear Regression on Real Data")
|
| 67 |
+
|
| 68 |
+
df = px.data.tips() # Load sample dataset
|
| 69 |
+
st.write("Dataset preview:", df.head())
|
| 70 |
|
| 71 |
+
x_feature = st.selectbox("Select Independent Variable (X)", df.select_dtypes(include=np.number).columns)
|
| 72 |
+
y_feature = st.selectbox("Select Dependent Variable (Y)", df.select_dtypes(include=np.number).columns, index=1)
|
|
|
|
| 73 |
|
| 74 |
+
X = df[[x_feature]]
|
| 75 |
+
y = df[y_feature]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
model = LinearRegression()
|
| 78 |
+
model.fit(X, y)
|
| 79 |
+
y_pred = model.predict(X)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
fig = px.scatter(df, x=x_feature, y=y_feature, title="Scatter Plot with Regression Line")
|
| 82 |
+
fig.add_scatter(x=df[x_feature], y=y_pred, mode='lines', name='Best Fit Line')
|
| 83 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 84 |
+
|
| 85 |
+
st.subheader("Model Performance")
|
| 86 |
+
st.write(f"**Slope (wβ)**: {model.coef_[0]:.4f}")
|
| 87 |
+
st.write(f"**Intercept (wβ)**: {model.intercept_:.4f}")
|
| 88 |
+
st.write(f"**RΒ² Score**: {r2_score(y, y_pred):.4f}")
|
| 89 |
+
st.write(f"**MSE**: {mean_squared_error(y, y_pred):.4f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
elif section == "Colab Notebook":
|
| 92 |
+
st.header("π Open in Google Colab")
|
| 93 |
st.markdown("""
|
| 94 |
<a href='https://colab.research.google.com/drive/11-Rv7BC2PhOqk5hnpdXo6QjqLLYLDvTD?usp=sharing' target='_blank'>
|
| 95 |
+
π Open Linear Regression Colab Notebook
|
| 96 |
</a>
|
| 97 |
""", unsafe_allow_html=True)
|
| 98 |
|
| 99 |
st.markdown("---")
|
| 100 |
+
st.success("This app blends theory with visuals and interaction to help you master Linear Regression!")
|