Download pages/Linear_Regression_Algorithm.py from Phaneendrabayi/Zero_To_Hero_Machine_Learning: direct link, hf CLI and curl.
- Browser
- Download file 3.63 kB
-
https://huggingface.co/spaces/Phaneendrabayi/Zero_To_Hero_Machine_Learning/resolve/main/pages/Linear_Regression_Algorithm.py
- Command line
-
hf download hf://spaces/Phaneendrabayi/Zero_To_Hero_Machine_Learning/pages/Linear_Regression_Algorithm.py
-
curl -L -o Linear_Regression_Algorithm.py https://huggingface.co/spaces/Phaneendrabayi/Zero_To_Hero_Machine_Learning/resolve/main/pages/Linear_Regression_Algorithm.py
3.63 kB
| 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. | |
| """) | |