๐ Introduction
Linear Regression is one of the simplest and most widely used supervised machine learning algorithms. It is used to predict a continuous numerical value by modeling the relationship between one or more independent variables (features) and a dependent variable (target) using a straight line.
Information
๐ฏ Objectives of Linear Regression
- Predict continuous numerical values.
- Understand relationships between variables.
- Estimate future outcomes based on historical data.
- Identify the influence of input features on the target variable.
๐ง Types of Linear Regression
| Type | Description | Example |
|---|---|---|
| Simple Linear Regression | One independent variable predicts one dependent variable. | House price based on area. |
| Multiple Linear Regression | Multiple independent variables predict one dependent variable. | House price based on area, bedrooms, and age. |
๐ Mathematical Model
Simple Linear Regression
In this equation:
- y โ Predicted dependent variable.
- x โ Independent variable.
- ฮฒโ โ Intercept (value of y when x = 0).
- ฮฒโ โ Slope or regression coefficient.
- ฮต โ Random error term.
Multiple Linear Regression
Multiple Linear Regression extends the model by using several input features to improve prediction accuracy.
โ๏ธ How Linear Regression Works
Collect and prepare the dataset.
Clean missing values and preprocess features.
Fit the best regression line to the training data.
Calculate prediction errors using a loss function.
Optimize coefficients using gradient descent or analytical methods.
Use the trained model to predict new values.
๐ Assumptions of Linear Regression
- Linearity: The relationship between features and the target is linear.
- Independence: Observations are independent of each other.
- Homoscedasticity: Error variance remains approximately constant.
- Normality: Residuals are approximately normally distributed.
- No Multicollinearity: Independent variables should not be highly correlated.
Important
๐ Cost Function
Linear Regression minimizes the Mean Squared Error (MSE), which measures the average squared difference between actual and predicted values.
Lower MSE values indicate that the regression model is making more accurate predictions.
๐ Evaluation Metrics
| Metric | Purpose |
|---|---|
| Mean Absolute Error (MAE) | Average absolute prediction error. |
| Mean Squared Error (MSE) | Average squared prediction error. |
| Root Mean Squared Error (RMSE) | Error measured in the original unit. |
| Rยฒ Score | Explains the proportion of variance captured by the model. |
Coefficient of Determination (Rยฒ)
An Rยฒ value closer to 1 indicates that the model explains a larger proportion of the variation in the target variable.
โ๏ธ Advantages and Limitations
- Simple to understand and implement.
- Fast training and prediction.
- Highly interpretable coefficients.
- Works well for approximately linear relationships.
- Requires relatively little computational power.
- Cannot model complex nonlinear relationships.
- Sensitive to outliers.
- Depends on several statistical assumptions.
- May underfit complex datasets.
- Multicollinearity can reduce coefficient reliability.
๐ Real-World Applications
| Application | Prediction Target |
|---|---|
| ๐ Real Estate | House prices |
| ๐ Sales Forecasting | Future sales revenue |
| ๐ฐ Finance | Stock trend analysis |
| ๐ก๏ธ Weather | Temperature prediction |
| โก Energy | Electricity demand forecasting |
| ๐ Automotive | Vehicle resale value |
๐ Linear Regression Workflow
๐ป Practical Example
Linear Regression Using Scikit-learn
from sklearn.linear_model import LinearRegression
import numpy as np
# Training data
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 5, 4, 5])
# Create model
model = LinearRegression()
# Train model
model.fit(X, y)
# Predict new value
prediction = model.predict([[6]])
print("Predicted Value:", prediction[0])
print("Slope:", model.coef_[0])
print("Intercept:", model.intercept_)โ ๏ธ Common Mistakes
- Applying Linear Regression to highly nonlinear relationships.
- Ignoring feature scaling when using gradient-based optimization.
- Not checking for outliers before training.
- Using highly correlated independent variables.
- Evaluating the model only on training data.