This project implements and compares linear regression using three different approaches: traditional least squares, scikit-learn's LinearRegression, and gradient descent. The goal is to showcase diverse regression methodologies and provide insights into model performance and accuracy.
Linear regression is a fundamental machine learning algorithm used for modeling the relationship between a single independent variable and a dependent variable. In this project, we explore three distinct methods to perform linear regression and compare their results. The methods employed are:
- Least Squares Method
- Scikit-learn's LinearRegression
- Gradient Descent
The least squares method is a classical approach to linear regression, calculating the line of best fit that minimizes the sum of the squared differences between the observed and predicted values.
Scikit-learn is a widely used Python library for machine learning. We utilized the LinearRegression module from scikit-learn to perform linear regression on the provided dataset, showcasing its ease of use and efficiency.
Gradient descent is an iterative optimization algorithm used for finding the minimum of a function. We implemented gradient descent to iteratively adjust model parameters to minimize the mean squared error, demonstrating a foundational optimization technique.
To run the project and observe the comparison of linear regression methods, follow these steps:
- Clone the repository.
- Ensure you have the necessary dependencies installed (NumPy, scikit-learn).
- Run the provided code and analyze the results.
We compared the obtained coefficients (slope and intercept) and costs (mean squared error) from each method. Here are the summarized results:
- Least Squares:
- Slope (m): 1.3530129888064268
- Intercept (c): 6.481604578730057
- Cost: 110.34548507080449
- Scikit-learn's LinearRegression:
- Coefficient (m): 1.35301299
- Intercept (c): 6.481604578729701
- Training Score: 0.6454504815883391
- Testing Score: 0.438073496767603
- Gradient Descent:
- Slope (m): 1.4788956622797287
- Intercept (c): 0.0302695172877759
- Cost: 112.64994426496072