Phaneendrabayi commited on
Commit
6b290db
Β·
verified Β·
1 Parent(s): 0060d54

Update pages/Linear_Regression_Algorithm.py

Browse files
Files changed (1) hide show
  1. pages/Linear_Regression_Algorithm.py +53 -56
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", layout="wide")
5
 
6
- st.title("πŸ“Š Regression Models - Linear Regression")
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** algorithm used to predict **continuous numeric outcomes** based on one or more input features. It assumes a **linear relationship** between the independent variable(s) (X) and the dependent variable (y).
14
 
15
- Linear Regression can be:
16
- - **Simple Linear Regression**: One feature (X)
17
- - **Multiple Linear Regression**: Multiple features (X1, X2, ..., Xn)
18
 
19
  ---
20
 
21
  ## πŸ“ Mathematical Formulation
22
- The standard form of the linear model is:
23
 
24
  \[ y = w_1x_1 + w_2x_2 + ... + w_nx_n + b = w^T x + b \]
25
 
26
  Where:
27
- - `x`: input feature vector (independent variables)
28
- - `w`: weights (coefficients learned by the model)
29
- - `b`: bias (intercept)
30
- - `y`: predicted continuous output
31
 
32
- The model parameters are estimated using **Ordinary Least Squares (OLS)** by minimizing the **Mean Squared Error (MSE)**:
33
 
34
  \[ \text{MSE} = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2 \]
35
 
36
  ---
37
 
38
- ## πŸ” Key Concepts and Assumptions
39
 
40
  ### 1. **Least Squares Estimation**
41
- The method used to minimize the squared differences between actual and predicted values.
42
 
43
  ### 2. **Residuals**
44
- The difference between actual values and predicted values \( e_i = y_i - \hat{y}_i \). Analyzing residuals helps identify model misspecification.
45
 
46
  ### 3. **R-squared (RΒ²)**
47
- Represents the proportion of variance explained by the model:
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. **Key Assumptions**
53
- - **Linearity**: The relationship between X and y is linear.
54
- - **Independence**: Observations are independent of each other.
55
- - **Homoscedasticity**: Constant variance of residuals.
56
- - **Normality**: Residuals are normally distributed.
57
- - **No Multicollinearity**: Features should not be highly correlated.
58
-
59
- Violating these assumptions can reduce the reliability of predictions.
60
 
61
  ---
62
 
63
- ## πŸ”§ Common Hyperparameters (Scikit-learn)
64
 
65
- | Parameter | Description |
66
- |------------------|-----------------------------------------------------------------------------|
67
- | `fit_intercept` | If True, model calculates the intercept. Set False when data is already centered. |
68
- | `copy_X` | If True, X is copied. Set False for memory efficiency. |
69
- | `n_jobs` | Number of cores used for computation. Useful for large datasets. |
70
 
71
- **Note**: `normalize` has been deprecated. Use preprocessing (e.g., `StandardScaler`) in a pipeline instead.
72
 
73
  ---
74
 
75
- ## βœ… Advantages
76
- - Simple and fast to implement.
77
- - Works well when assumptions are met.
78
- - Coefficients are interpretable.
79
- - Requires less computational power.
80
 
81
- ## ❌ Disadvantages
82
- - Limited to linear relationships.
83
- - Sensitive to outliers.
84
- - Poor performance with irrelevant or highly correlated features.
 
85
 
86
  ---
87
 
88
- ## πŸ§ͺ Optuna for Hyperparameter Tuning (Conceptual)
89
- Linear Regression has limited hyperparameters, but Optuna is helpful in more complex pipelines:
90
 
91
- - **Polynomial Regression**: Tune the degree of the polynomial.
92
- - **Ridge / Lasso / ElasticNet**: Tune regularization strength (`alpha`, `l1_ratio`).
93
- - **Feature Selection**: Use Optuna to select best subset of features.
94
 
95
- You define an **objective function** (e.g., minimize RMSE), and Optuna optimizes hyperparameters via **Bayesian optimization**.
96
 
97
  ---
98
 
99
- ## πŸ“Œ Use Cases
100
- - Predicting house prices based on features like area, rooms, location
101
- - Sales forecasting from historical data
102
- - Medical cost estimation from patient info
103
- - Predicting COβ‚‚ emissions from engine parameters
 
 
104
 
105
- πŸ“Ž **Tip**: Always visualize **residual plots** to verify assumptions and consider adding interaction or polynomial terms for capturing complexity.
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
+ """)