Spaces:
Sleeping
Sleeping
Update pages/Linear Regression.py
Browse files- pages/Linear Regression.py +65 -55
pages/Linear Regression.py
CHANGED
|
@@ -32,64 +32,74 @@ else:
|
|
| 32 |
tab1, tab2, tab3 = st.tabs(["๐ About Linear Regression", "โ๏ธ Train Model", "๐ Visualize"])
|
| 33 |
|
| 34 |
with tab1:
|
| 35 |
-
|
| 36 |
-
## ๐ What is Linear Regression?
|
| 37 |
-
|
| 38 |
-
**Linear Regression** is a fundamental algorithm in machine learning used to predict continuous numerical values.
|
| 39 |
-
|
| 40 |
-
---
|
| 41 |
-
### ๐ข The Linear Equation:
|
| 42 |
-
|
| 43 |
-
The general form:
|
| 44 |
-
$$
|
| 45 |
-
y = \beta_0 + \beta_1x_1 + \beta_2x_2 + ... + \beta_nx_n + \varepsilon
|
| 46 |
-
$$
|
| 47 |
-
|
| 48 |
-
- **y**: Output (target)
|
| 49 |
-
- **xโ, xโ, ..., xโ**: Input features
|
| 50 |
-
- **ฮฒโ**: Intercept
|
| 51 |
-
- **ฮฒโ, ..., ฮฒโ**: Coefficients
|
| 52 |
-
- **ฮต**: Error term
|
| 53 |
|
| 54 |
-
-
|
| 55 |
-
### ๐ง How it Works:
|
| 56 |
-
|
| 57 |
-
1. Fit a straight line that minimizes the squared error between predicted and actual values.
|
| 58 |
-
2. Uses Ordinary Least Squares (OLS) for best-fit line.
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
-
|
| 75 |
-
|
| 76 |
-
-
|
| 77 |
-
-
|
| 78 |
-
-
|
| 79 |
-
|
| 80 |
-
### โ ๏ธ Cons:
|
| 81 |
-
- Assumes linear relationship
|
| 82 |
-
- Sensitive to outliers
|
| 83 |
-
- Doesn't handle multicollinearity well
|
| 84 |
-
|
| 85 |
-
---
|
| 86 |
-
### ๐ Assumptions:
|
| 87 |
-
- Linearity
|
| 88 |
-
- Homoscedasticity
|
| 89 |
-
- Independence
|
| 90 |
-
- Normality of residuals
|
| 91 |
-
|
| 92 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
with tab2:
|
| 95 |
st.subheader("โ๏ธ Train Linear Regression Model")
|
|
|
|
| 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 |
+
This is used when we have more than one independent variable.
|
| 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 |
+
- The algorithm tries to find values of \( \beta \) (coefficients) that **minimize this cost function**.
|
| 85 |
+
""")
|
| 86 |
+
|
| 87 |
+
st.subheader("๐ Assumptions of Linear Regression")
|
| 88 |
+
st.markdown("""
|
| 89 |
+
- **Linearity**: Relationship between input and output is linear
|
| 90 |
+
- **Independence**: Observations are independent
|
| 91 |
+
- **Homoscedasticity**: Constant variance of errors
|
| 92 |
+
- **Normality of errors**
|
| 93 |
+
- **No multicollinearity** (for multiple regression)
|
| 94 |
+
""")
|
| 95 |
+
|
| 96 |
+
st.subheader("๐ก When to Use Linear Regression?")
|
| 97 |
+
st.markdown("""
|
| 98 |
+
- To predict continuous numeric values (e.g., price, salary, marks)
|
| 99 |
+
- To analyze how inputs are related to output
|
| 100 |
+
- Easy to implement and interpret
|
| 101 |
+
""")
|
| 102 |
+
|
| 103 |
|
| 104 |
with tab2:
|
| 105 |
st.subheader("โ๏ธ Train Linear Regression Model")
|