Linear Regression Algorithm
Linear Regression Algorithm
6. Evaluation Metrics
7. Regularization Techniques
Pattern Recognition
Optimization
Automation
Linear regression
Statistical method used to model the relationship between a dependent variable and
one or more independent variables.
The goal is to find the linear equation that best predicts the dependent variable from
Linear Regression
Involves one independent variable and one dependent variable. The relationship is
modeled using a straight line.
Involves two or more independent variables. The relationship is still linear, but it
extends into multiple dimensions.
Algorithm :
Fundamental to CS & mathematics and are used in various fields to process data,
automate tasks, and solve complex problems efficiently.
Characteristics equation of Geometries
Straight Line - Y = mX + C
Circle - X 2 + Y2 = R 2
Algebra & Geometry
- (X – h)2 + (Y – k)2 = R2
Parabola - Y2 = 4pX
- (Y-k)2 = 4p(X-h)
Given n data points (x1,y1), (x2,y2),..., (xn,yn). The goal is to find a line y=mx+c that best fits the data.
Calculate the Means: Compute the mean of x (x̄) and the mean of y (ȳ).
Linear Relationship
Calculation :
Linear Relationship
m = 7.5
C = 41.5
Y = 7.5 x + 41.5
Interpretation
• Slope (m = 7.5): For every additional hour studied, the test score increases by 7.5 points.
• Intercept (c = 41.5): If a student studies for 0 hours, the predicted test score is 41.5 points.
Linear Relationship
Let's predict the test score for a student who studies for 6 hours (x = 6) :
Y = 86.5
So, a student who studies for 6 hours is predicted to score 86.5 points on the test.
1. Train Test Split
Description: Split into two : one for training the model & other for testing its perf.
Purpose: Helps evaluate how well the model can predict unseen data.
Evaluating Linear Models
2. Cross-Validation
Description: Involves partitioning the dataset into several subsets (folds) &
training/testing the model multiple times, each time using a different fold as the test
set.
Purpose: Provides a more reliable estimate of model performance and reduces the
risk of overfitting.
3. Residual Analysis
Description: Analyze the residuals (the differences between predicted and actual
values) to check for patterns.
Purpose: Residuals should be randomly distributed with no discernible pattern,
indicating that the model is a good fit.
•Residual Plots: Plot residuals against predicted values to check for homoscedasticity
(constant variance).
4. Model Assumptions Check
Linear regression relies on several assumptions that should be checked:
Evaluating Linear Models
•Linearity: The relationship between predictors and the outcome should be linear.
•No Multicollinearity: Indepen. variables are not highly correlated with each other.
Purpose: Identifies which variables have the most influence on the predicted
outcome.
1. MAE (Mean Absolute Error)
Measures average squared diff b/n predicted & actual values, penalizing larger errors
more than MAE.
Provides error in the same units as the target variable, making it more interpretable.
4. R2 (R Squared)
Represents the proportion of variance in the dependent variable that can be explained
by the independent variables. Ranges from 0 to 1.
Regularization Types
Ridge and Lasso Regression are two regularization techniques used to address the
problem of multicollinearity and overfitting in linear regression models.
Regularization Techniques
They add a penalty term to the ordinary least squares (OLS) objective function,
encouraging simpler models that generalize better.
Ridge Regression (L2 Regularization)
Adds a penalty equivalent to the square of the magnitude of coefficients to the
loss function.
It shrinks the coefficients, but never exactly to zero, which means it includes all
predictors in the model.
Characteristics :
Colsed-form solution Shrinks coefficients
Characteristics :
Shrinks coefficients : Performs feature selection and shrinks some coefficients to ‘0’
Used when : We believe some predictors are irrelevant and want to simplify the model
Bias-variance trade-off : Introducing bias by significantly reducing Variance)
Optimization : Requires iterative algorithms
Lasso Regression (L1 Regularization)
Adds a penalty equivalent to the absolute value of the magnitude of coefficients
to the loss function.
Regularization Techniques
Characteristics :
Shrinks coefficients : Performs feature selection and shrinks some coefficients to ‘0’
Used when : We believe some predictors are irrelevant and want to simplify the model
Bias-variance trade-off : Introducing bias by significantly reducing Variance)
Optimization : Requires iterative algorithms
Bias Variance Trade-off
Trade off between two type of error viz., Bias Error & Variance Error which affects the
performance of the model
Bias Variance Trade-off
Trade-off
Low Bias, High Variance: Complex models (e.g., deep neural networks) that fit the training
data very well but perform poorly on unseen data due to capturing noise. Overfitting
High Bias, Low Variance: Simple models (e.g., linear regression) that may not fit the
training data closely but generalize better to unseen data. Underfitting
Variance refers to the model's sensitivity to fluctuations in the training data. High variance
can lead to:
• Overfitting: The model captures noise and random fluctuations in the training data rather
than the true underlying pattern, resulting in excellent performance on the training set but
poor generalization to new data.