This project implements a simple linear regression model using gradient descent from scratch in Python. The implementation includes batch gradient descent, stochastic gradient descent, and mini-batch gradient descent.
- Predictor Variable:
./linearX.csv
- Response Variable:
./linearY.csv
- Python 3.x
- pandas
- numpy
- matplotlib
Install the required libraries using pip:
pip install pandas numpy matplotlib
- Download the dataset and place
linearX.csv
andlinearY.csv
in the project directory. - Run the
linear_regression.py
script:
python linear_regression.py
- The dataset is loaded using pandas.
- The predictor variable is normalized.
-
Batch Gradient Descent:
- Updates the parameters using the entire dataset in each iteration.
-
Stochastic Gradient Descent:
- Updates the parameters using one randomly selected data point in each iteration.
-
Mini-Batch Gradient Descent:
- Updates the parameters using a small random subset of the dataset in each iteration.
- The cost function vs. iterations is plotted for the first 50 iterations for all gradient descent methods.
- The linear regression fit is visualized for each gradient descent method.
The script prints the final parameters and cost for each gradient descent method and plots the following:
- Cost Function vs. Iterations (First 50 Iterations)
- Linear Regression Fit for Batch Gradient Descent
- Linear Regression Fit for Stochastic Gradient Descent
- Linear Regression Fit for Mini-Batch Gradient Descent
This project is licensed under the MIT License.