Spaces:
Sleeping
Sleeping
Update pages/Linear Regression.py
Browse files- pages/Linear Regression.py +88 -128
pages/Linear Regression.py
CHANGED
|
@@ -1,142 +1,102 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
from sklearn.model_selection import train_test_split
|
| 5 |
-
from sklearn.linear_model import LinearRegression
|
| 6 |
-
from sklearn.metrics import mean_squared_error, r2_score
|
| 7 |
-
import matplotlib.pyplot as plt
|
| 8 |
-
import seaborn as sns
|
| 9 |
|
| 10 |
-
st.set_page_config(page_title="
|
| 11 |
-
st.title("π Linear Regression Explained")
|
| 12 |
|
| 13 |
-
#
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
from sklearn.datasets import fetch_california_housing
|
| 26 |
-
data = fetch_california_housing()
|
| 27 |
-
df = pd.DataFrame(data.data, columns=data.feature_names)
|
| 28 |
-
df['target'] = data.target
|
| 29 |
-
|
| 30 |
-
# Tabs
|
| 31 |
-
|
| 32 |
-
tab1, tab2, tab3 = st.tabs(["π About Linear Regression", "βοΈ Train Model", "π Visualize"])
|
| 33 |
-
|
| 34 |
-
with tab1:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
st.title("π Linear Regression - Intuition & Explanation")
|
| 38 |
-
|
| 39 |
-
st.markdown("""
|
| 40 |
-
Linear Regression is a **supervised machine learning algorithm** used to predict a continuous target variable based on one or more input features.
|
| 41 |
-
|
| 42 |
-
It tries to **fit a straight line** (or hyperplane) through the data that minimizes the error between actual and predicted values.
|
| 43 |
-
""")
|
| 44 |
-
|
| 45 |
-
st.subheader("πΉ Simple Linear Regression Formula")
|
| 46 |
-
|
| 47 |
-
st.latex(r'''
|
| 48 |
-
y = \beta_0 + \beta_1 x + \epsilon
|
| 49 |
-
''')
|
| 50 |
-
|
| 51 |
-
st.markdown("""
|
| 52 |
-
Where:
|
| 53 |
-
- \( y \): Predicted value
|
| 54 |
-
- \( x \): Input feature
|
| 55 |
-
- \( \beta_0 \): Intercept
|
| 56 |
-
- \( \beta_1 \): Slope of the line
|
| 57 |
-
- \( \epsilon \): Error term
|
| 58 |
-
""")
|
| 59 |
-
|
| 60 |
-
st.subheader("πΉ Multiple Linear Regression")
|
| 61 |
-
|
| 62 |
-
st.latex(r'''
|
| 63 |
-
y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \dots + \beta_n x_n + \epsilon
|
| 64 |
-
''')
|
| 65 |
-
|
| 66 |
st.markdown("""
|
| 67 |
-
|
| 68 |
""")
|
| 69 |
-
|
| 70 |
-
st.subheader("π― Objective of Linear Regression")
|
| 71 |
-
st.markdown("To find the best-fit line by minimizing the **sum of squared errors (SSE)**.")
|
| 72 |
-
|
| 73 |
-
st.latex(r'''
|
| 74 |
-
SSE = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2
|
| 75 |
-
''')
|
| 76 |
-
|
| 77 |
-
st.subheader("π Cost Function (Mean Squared Error)")
|
| 78 |
-
|
| 79 |
-
st.latex(r'''
|
| 80 |
-
J(\beta) = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2
|
| 81 |
-
''')
|
| 82 |
-
|
| 83 |
st.markdown("""
|
| 84 |
-
-
|
|
|
|
|
|
|
|
|
|
| 85 |
""")
|
| 86 |
-
|
| 87 |
-
|
| 88 |
st.markdown("""
|
| 89 |
-
|
| 90 |
-
- **Independence**: Observations are independent
|
| 91 |
-
- **Homoscedasticity**: Constant variance of errors
|
| 92 |
-
- **Normality of errors**
|
| 93 |
-
- **No multicollinearity** (for multiple regression)
|
| 94 |
""")
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
st.markdown("""
|
| 98 |
-
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
| 101 |
""")
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
st.set_page_config(page_title="Linear Regression", page_icon="π", layout="wide")
|
|
|
|
| 4 |
|
| 5 |
+
# Page Title
|
| 6 |
+
st.markdown("<h1>π Linear Regression</h1>", unsafe_allow_html=True)
|
| 7 |
|
| 8 |
+
# Introduction
|
| 9 |
+
st.markdown("### π§ What is Linear Regression?")
|
| 10 |
+
st.markdown("""
|
| 11 |
+
Linear Regression is a **supervised learning** algorithm used for predicting a **continuous output**.
|
| 12 |
+
It models the relationship between one or more **independent variables (features)** and a **dependent variable (target)** by fitting a linear equation to the data.
|
| 13 |
+
""")
|
| 14 |
|
| 15 |
+
# How It Works
|
| 16 |
+
st.markdown("### βοΈ How Linear Regression Works")
|
| 17 |
+
with st.expander("Mathematical Model"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
st.markdown("""
|
| 19 |
+
Linear regression fits a line defined by the equation:
|
| 20 |
""")
|
| 21 |
+
st.latex(r"y = \beta_0 + \beta_1x_1 + \beta_2x_2 + ... + \beta_nx_n + \epsilon")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
st.markdown("""
|
| 23 |
+
- $\\beta_0$ is the **intercept**
|
| 24 |
+
- $\\beta_1, ..., \\beta_n$ are the **coefficients (weights)**
|
| 25 |
+
- $x_1, ..., x_n$ are the **feature values**
|
| 26 |
+
- $\\epsilon$ is the **error term**
|
| 27 |
""")
|
| 28 |
+
|
| 29 |
+
with st.expander("Goal of the Algorithm"):
|
| 30 |
st.markdown("""
|
| 31 |
+
Minimize the **residual sum of squares** between actual and predicted values.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
""")
|
| 33 |
+
st.latex(r"RSS = \sum_{i=1}^n (y_i - \hat{y}_i)^2")
|
| 34 |
+
|
| 35 |
+
with st.expander("Training Process"):
|
| 36 |
st.markdown("""
|
| 37 |
+
- Find optimal weights (coefficients) using:
|
| 38 |
+
- **Ordinary Least Squares (OLS)**
|
| 39 |
+
- **Gradient Descent**
|
| 40 |
+
- OLS minimizes the squared differences between actual and predicted values.
|
| 41 |
""")
|
| 42 |
|
| 43 |
+
# Types
|
| 44 |
+
st.markdown("### π Types of Linear Regression")
|
| 45 |
+
st.markdown("""
|
| 46 |
+
- **Simple Linear Regression**: One independent variable
|
| 47 |
+
- **Multiple Linear Regression**: More than one independent variable
|
| 48 |
+
- **Polynomial Regression**: Non-linear relationship modeled with polynomial terms
|
| 49 |
+
- **Ridge, Lasso Regression**: Regularized versions to prevent overfitting
|
| 50 |
+
""")
|
| 51 |
+
|
| 52 |
+
# Assumptions
|
| 53 |
+
st.markdown("### β
Assumptions of Linear Regression")
|
| 54 |
+
st.markdown("""
|
| 55 |
+
- **Linearity**: Relationship between input and output is linear
|
| 56 |
+
- **Independence**: Observations are independent
|
| 57 |
+
- **Homoscedasticity**: Constant variance of residuals
|
| 58 |
+
- **Normality**: Residuals are normally distributed
|
| 59 |
+
- **No multicollinearity**: Independent variables aren't too correlated
|
| 60 |
+
""")
|
| 61 |
+
|
| 62 |
+
# Metrics
|
| 63 |
+
st.markdown("### π Evaluation Metrics")
|
| 64 |
+
st.markdown("""
|
| 65 |
+
- **Mean Absolute Error (MAE)**
|
| 66 |
+
- **Mean Squared Error (MSE)**
|
| 67 |
+
- **Root Mean Squared Error (RMSE)**
|
| 68 |
+
- **RΒ² Score (Coefficient of Determination)**
|
| 69 |
+
""")
|
| 70 |
+
|
| 71 |
+
# Visualization and Prediction Explanation
|
| 72 |
+
st.markdown("### π Visual Representation")
|
| 73 |
+
st.markdown("""
|
| 74 |
+
Linear regression fits a **line** (or hyperplane) to the data such that the **sum of squared errors** between actual and predicted values is minimized.
|
| 75 |
+
""")
|
| 76 |
+
|
| 77 |
+
# When to Use
|
| 78 |
+
st.markdown("### π― When to Use Linear Regression")
|
| 79 |
+
st.markdown("""
|
| 80 |
+
- When the relationship between input and output is **approximately linear**
|
| 81 |
+
- When you want **interpretability** (coefficients show effect size)
|
| 82 |
+
- When **speed** is important and data is not too large
|
| 83 |
+
""")
|
| 84 |
+
|
| 85 |
+
# Regularization
|
| 86 |
+
st.markdown("### π Regularization Techniques")
|
| 87 |
+
st.markdown("""
|
| 88 |
+
To prevent overfitting, especially in multiple linear regression:
|
| 89 |
+
- **Ridge Regression** (L2 penalty): Shrinks coefficients
|
| 90 |
+
- **Lasso Regression** (L1 penalty): Can set some coefficients to 0
|
| 91 |
+
- **Elastic Net**: Combines Ridge and Lasso
|
| 92 |
+
""")
|
| 93 |
+
|
| 94 |
+
# Final Note
|
| 95 |
+
st.markdown("### β
Summary")
|
| 96 |
+
st.markdown("""
|
| 97 |
+
Linear Regression is:
|
| 98 |
+
- Simple to implement and interpret
|
| 99 |
+
- Fast and effective on linearly separable data
|
| 100 |
+
- Not ideal for complex or non-linear relationships
|
| 101 |
+
π Use regularization and diagnostic plots to validate model quality.
|
| 102 |
+
""")
|