Update pages/Linear_Regression_Algorithm.py
Browse files
pages/Linear_Regression_Algorithm.py
CHANGED
|
@@ -1,106 +1,103 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
-
st.set_page_config(page_title="Regression Models Explorer"
|
| 5 |
|
| 6 |
-
st.title("π
|
| 7 |
|
| 8 |
-
# --- Linear Regression Section ---
|
| 9 |
-
st.header("π Linear Regression - In Depth")
|
| 10 |
|
| 11 |
st.markdown("""
|
| 12 |
## π What is Linear Regression?
|
| 13 |
-
Linear Regression is a **supervised learning**
|
| 14 |
|
| 15 |
-
|
| 16 |
-
- **Simple Linear Regression**: One feature (X)
|
| 17 |
-
- **Multiple Linear Regression**: Multiple features (X1, X2, ..., Xn)
|
| 18 |
|
| 19 |
---
|
| 20 |
|
| 21 |
## π Mathematical Formulation
|
| 22 |
-
The
|
| 23 |
|
| 24 |
\[ y = w_1x_1 + w_2x_2 + ... + w_nx_n + b = w^T x + b \]
|
| 25 |
|
| 26 |
Where:
|
| 27 |
-
- `x`: input
|
| 28 |
-
- `w`: weights
|
| 29 |
-
- `b`: bias
|
| 30 |
-
- `y`: predicted
|
| 31 |
|
| 32 |
-
The model
|
| 33 |
|
| 34 |
\[ \text{MSE} = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2 \]
|
| 35 |
|
| 36 |
---
|
| 37 |
|
| 38 |
-
## π Key Concepts
|
| 39 |
|
| 40 |
### 1. **Least Squares Estimation**
|
| 41 |
-
|
| 42 |
|
| 43 |
### 2. **Residuals**
|
| 44 |
-
|
| 45 |
|
| 46 |
### 3. **R-squared (RΒ²)**
|
| 47 |
-
|
| 48 |
|
| 49 |
\[ R^2 = 1 - \frac{\text{SSR}}{\text{SST}} \]
|
| 50 |
-
Where SSR = Sum of Squared Residuals, SST = Total Sum of Squares.
|
| 51 |
|
| 52 |
-
### 4. **
|
| 53 |
-
- **Linearity**:
|
| 54 |
-
- **Independence**: Observations are independent
|
| 55 |
-
- **Homoscedasticity**: Constant variance
|
| 56 |
-
- **Normality**: Residuals are normally distributed
|
| 57 |
-
- **No
|
| 58 |
-
|
| 59 |
-
Violating these assumptions can reduce the reliability of predictions.
|
| 60 |
|
| 61 |
---
|
| 62 |
|
| 63 |
-
##
|
| 64 |
|
| 65 |
-
|
|
| 66 |
-
|------------------|-----------------------------------------------------------------------------|
|
| 67 |
-
| `fit_intercept`
|
| 68 |
-
| `copy_X`
|
| 69 |
-
| `n_jobs`
|
| 70 |
|
| 71 |
-
**Note**: `normalize`
|
| 72 |
|
| 73 |
---
|
| 74 |
|
| 75 |
-
##
|
| 76 |
-
-
|
| 77 |
-
-
|
| 78 |
-
-
|
| 79 |
-
-
|
| 80 |
|
| 81 |
-
##
|
| 82 |
-
-
|
| 83 |
-
- Sensitive to outliers
|
| 84 |
-
- Poor
|
|
|
|
| 85 |
|
| 86 |
---
|
| 87 |
|
| 88 |
-
## π§ͺ
|
| 89 |
-
Linear Regression has
|
| 90 |
|
| 91 |
-
- **Polynomial Regression**: Tune the degree of the polynomial
|
| 92 |
-
- **
|
| 93 |
-
- **Feature
|
| 94 |
|
| 95 |
-
|
| 96 |
|
| 97 |
---
|
| 98 |
|
| 99 |
-
##
|
| 100 |
-
-
|
| 101 |
-
-
|
| 102 |
-
-
|
| 103 |
-
-
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
| 106 |
-
""")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
+
st.set_page_config(page_title="Regression Models Explorer")
|
| 5 |
|
| 6 |
+
st.title("π Linear Regression")
|
| 7 |
|
|
|
|
|
|
|
| 8 |
|
| 9 |
st.markdown("""
|
| 10 |
## π What is Linear Regression?
|
| 11 |
+
Linear Regression is a **supervised learning algorithm** used for predicting **continuous numerical values**. It models the relationship between one or more input features (independent variables) and a target variable (dependent variable) by fitting a **linear equation** to the observed data.
|
| 12 |
|
| 13 |
+
Types of Linear Regression:
|
| 14 |
+
- **Simple Linear Regression**: One feature (X) and one target (y)
|
| 15 |
+
- **Multiple Linear Regression**: Multiple features (X1, X2, ..., Xn) and one target (y)
|
| 16 |
|
| 17 |
---
|
| 18 |
|
| 19 |
## π Mathematical Formulation
|
| 20 |
+
The hypothesis function in Linear Regression is given by:
|
| 21 |
|
| 22 |
\[ y = w_1x_1 + w_2x_2 + ... + w_nx_n + b = w^T x + b \]
|
| 23 |
|
| 24 |
Where:
|
| 25 |
+
- `x`: input features
|
| 26 |
+
- `w`: weights/coefficients
|
| 27 |
+
- `b`: bias or intercept
|
| 28 |
+
- `y`: predicted output
|
| 29 |
|
| 30 |
+
The model is trained using **Ordinary Least Squares (OLS)** to minimize the **Mean Squared Error (MSE)**:
|
| 31 |
|
| 32 |
\[ \text{MSE} = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2 \]
|
| 33 |
|
| 34 |
---
|
| 35 |
|
| 36 |
+
## π Key Concepts
|
| 37 |
|
| 38 |
### 1. **Least Squares Estimation**
|
| 39 |
+
Method to minimize the sum of squared differences between actual and predicted values.
|
| 40 |
|
| 41 |
### 2. **Residuals**
|
| 42 |
+
Difference between actual and predicted values: \( e_i = y_i - \hat{y}_i \)
|
| 43 |
|
| 44 |
### 3. **R-squared (RΒ²)**
|
| 45 |
+
Indicates the proportion of variance in the target variable explained by the model:
|
| 46 |
|
| 47 |
\[ R^2 = 1 - \frac{\text{SSR}}{\text{SST}} \]
|
|
|
|
| 48 |
|
| 49 |
+
### 4. **Assumptions**
|
| 50 |
+
- **Linearity**: Target has a linear relationship with inputs
|
| 51 |
+
- **Independence**: Observations are independent
|
| 52 |
+
- **Homoscedasticity**: Constant variance in errors
|
| 53 |
+
- **Normality**: Residuals are normally distributed
|
| 54 |
+
- **No multicollinearity**: Independent variables are not highly correlated
|
|
|
|
|
|
|
| 55 |
|
| 56 |
---
|
| 57 |
|
| 58 |
+
### βοΈ Hyperparameters (Scikit-learn `LinearRegression`)
|
| 59 |
|
| 60 |
+
| Hyperparameter | Description |
|
| 61 |
+
|--------------------|-----------------------------------------------------------------------------|
|
| 62 |
+
| `fit_intercept` | If True, model calculates intercept. If False, no intercept added. |
|
| 63 |
+
| `copy_X` | If True, X is copied; if False, changes X in-place for efficiency. |
|
| 64 |
+
| `n_jobs` | Number of CPU cores used. -1 uses all available processors. |
|
| 65 |
|
| 66 |
+
**Note**: `normalize` is deprecated. Use `StandardScaler` or pipelines for preprocessing.
|
| 67 |
|
| 68 |
---
|
| 69 |
|
| 70 |
+
### βοΈ Advantages
|
| 71 |
+
- Easy to implement
|
| 72 |
+
- Interpretable coefficients
|
| 73 |
+
- Requires less computational resources
|
| 74 |
+
- Fast training time
|
| 75 |
|
| 76 |
+
### π« Limitations
|
| 77 |
+
- Assumes linearity
|
| 78 |
+
- Sensitive to outliers
|
| 79 |
+
- Poor with irrelevant or highly correlated features
|
| 80 |
+
- Can underperform on complex datasets
|
| 81 |
|
| 82 |
---
|
| 83 |
|
| 84 |
+
### π§ͺ Hyperparameter Tuning with Optuna (Conceptual)
|
| 85 |
+
Although Linear Regression itself has few tunable parameters, Optuna can be valuable in these contexts:
|
| 86 |
|
| 87 |
+
- **Polynomial Regression**: Tune the degree of the polynomial
|
| 88 |
+
- **Regularized models** (e.g., Ridge, Lasso): Tune `alpha`, `l1_ratio`
|
| 89 |
+
- **Feature Engineering**: Select optimal features for better prediction
|
| 90 |
|
| 91 |
+
Optuna uses **Bayesian Optimization** to explore parameter space and optimize a target metric (e.g., RMSE).
|
| 92 |
|
| 93 |
---
|
| 94 |
|
| 95 |
+
### π‘ Real-World Applications
|
| 96 |
+
- Forecasting sales based on trends and features
|
| 97 |
+
- Predicting house prices by area, bedrooms, location
|
| 98 |
+
- Estimating insurance or healthcare costs
|
| 99 |
+
- Demand prediction in supply chain analytics
|
| 100 |
+
|
| 101 |
+
π **Best Practice**: Always visualize residuals and check assumptions. Consider using cross-validation and pipelines.
|
| 102 |
|
| 103 |
+
""")
|
|
|