File size: 3,630 Bytes
0060d54
 
 
6b290db
0060d54
6b290db
0060d54
 
 
 
6b290db
0060d54
6b290db
 
 
0060d54
 
 
 
6b290db
0060d54
 
 
 
6b290db
 
 
 
0060d54
6b290db
4dbf5d9
0060d54
 
 
6b290db
0060d54
 
6b290db
0060d54
 
6b290db
0060d54
 
6b290db
0060d54
 
 
6b290db
 
 
 
 
 
0060d54
 
 
6b290db
0060d54
6b290db
 
 
 
 
0060d54
6b290db
0060d54
 
 
6b290db
 
 
 
 
0060d54
6b290db
 
 
 
 
0060d54
 
 
6b290db
 
0060d54
6b290db
 
 
0060d54
6b290db
0060d54
 
 
6b290db
 
 
 
 
 
 
0060d54
6b290db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import streamlit as st
import pandas as pd

st.set_page_config(page_title="Regression Models Explorer")

st.title("πŸ“Š Linear Regression")


st.markdown("""
## πŸ“˜ What is Linear Regression?
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.

Types of Linear Regression:
- **Simple Linear Regression**: One feature (X) and one target (y)
- **Multiple Linear Regression**: Multiple features (X1, X2, ..., Xn) and one target (y)

---

## πŸ“ Mathematical Formulation
The hypothesis function in Linear Regression is given by:

\[ y = w_1x_1 + w_2x_2 + ... + w_nx_n + b = w^T x + b \]

Where:
- `x`: input features
- `w`: weights/coefficients
- `b`: bias or intercept
- `y`: predicted output

The model is trained using **Ordinary Least Squares (OLS)** to minimize the **Mean Squared Error (MSE)**:
\text{MSE} = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2

---

## πŸ” Key Concepts

### 1. **Least Squares Estimation**
Method to minimize the sum of squared differences between actual and predicted values.

### 2. **Residuals**
Difference between actual and predicted values: \( e_i = y_i - \hat{y}_i \)

### 3. **R-squared (RΒ²)**
Indicates the proportion of variance in the target variable explained by the model:

\[ R^2 = 1 - \frac{\text{SSR}}{\text{SST}} \]

### 4. **Assumptions**
- **Linearity**: Target has a linear relationship with inputs
- **Independence**: Observations are independent
- **Homoscedasticity**: Constant variance in errors
- **Normality**: Residuals are normally distributed
- **No multicollinearity**: Independent variables are not highly correlated

---

### βš™οΈ Hyperparameters (Scikit-learn `LinearRegression`)

| Hyperparameter     | Description                                                                 |
|--------------------|-----------------------------------------------------------------------------|
| `fit_intercept`    | If True, model calculates intercept. If False, no intercept added.           |
| `copy_X`           | If True, X is copied; if False, changes X in-place for efficiency.           |
| `n_jobs`           | Number of CPU cores used. -1 uses all available processors.                  |

**Note**: `normalize` is deprecated. Use `StandardScaler` or pipelines for preprocessing.

---

### βš–οΈ Advantages
- Easy to implement
- Interpretable coefficients
- Requires less computational resources
- Fast training time

### 🚫 Limitations
- Assumes linearity
- Sensitive to outliers
- Poor with irrelevant or highly correlated features
- Can underperform on complex datasets

---

### πŸ§ͺ Hyperparameter Tuning with Optuna (Conceptual)
Although Linear Regression itself has few tunable parameters, Optuna can be valuable in these contexts:

- **Polynomial Regression**: Tune the degree of the polynomial
- **Regularized models** (e.g., Ridge, Lasso): Tune `alpha`, `l1_ratio`
- **Feature Engineering**: Select optimal features for better prediction

Optuna uses **Bayesian Optimization** to explore parameter space and optimize a target metric (e.g., RMSE).

---

### πŸ’‘ Real-World Applications
- Forecasting sales based on trends and features
- Predicting house prices by area, bedrooms, location
- Estimating insurance or healthcare costs
- Demand prediction in supply chain analytics

πŸ“Œ **Best Practice**: Always visualize residuals and check assumptions. Consider using cross-validation and pipelines.

""")