This repository contains a simple implementation of Linear Regression using Gradient Descent in Python. The goal of this project is to provide a clear and understandable implementation of linear regression, demonstrating how the algorithm works without relying on external libraries like scikit-learn.
Linear regression is a statistical method that models the relationship between a dependent variable and one or more independent variables. This repository implements linear regression from scratch, using gradient descent to optimize the model parameters.
To get started with this project, clone the repository to your local machine using the following command:
git clone https://github.com/UznetDev/LinearRegression.gitNavigate into the project directory:
cd LinearRegressionEnsure you have Python installed on your machine. This project does not require any external dependencies, but if you plan to extend it, you might want to create a virtual environment:
python -m venv env
source env/bin/activate # On Windows, use `env\Scripts\activate`The main script model.py contains the implementation of the linear regression model. You can use this class to fit a model to your data and make predictions. Here's a basic example:
from linear_regression.model import LinearRegression
# Sample data
X = [[1], [2], [3], [4], [5]]
y = [1.2, 2.8, 3.6, 4.5, 5.1]
# Initialize the model
model = LinearRegression(step=0.01, n_iters=1000)
# Train the model
model.fit(X, y)
# Make predictions
predictions = model.predict([[6], [7]])
# Evaluate the model
mse = model.MSE(y, predictions)
print(f"Predictions: {predictions}")
print(f"MSE: {mse}")model.py: Contains theLinearRegressionclass with methods to train, predict, and evaluate a linear regression model.README.md: This file, providing an overview of the project.
-
Class Definition:
LinearRegression: A class that encapsulates the linear regression model, implementing gradient descent to optimize the model's parameters.
-
Initialization (
__init__method):- The class is initialized with a learning rate (
step) and the number of iterations (n_iters). - The model starts with initial slope and intercept values of zero, and internal variables to track the number of features (
__m_) and samples (__n_).
- The class is initialized with a learning rate (
-
Training (
fitmethod):- The
fitmethod takes in the input dataXand target valuesy. - The method uses gradient descent to adjust the slope and intercept over a specified number of iterations.
- It raises a
ValueErrorif the number of samples inXandydo not match.
- The
-
Prediction (
predictmethod):- The
predictmethod uses the trained model to predict the target values for new input dataX. - It raises a
ValueErrorif the input data has a different number of features or samples compared to the data used for training.
- The
-
Evaluation (
MSEmethod):- The
MSEmethod calculates the Mean Squared Error (MSE) between the true target valuesyand the predicted valuesy_pred.
- The
If you encounter any problems or have suggestions for improvements, please open an issue in this repository. You can do this by navigating to the "Issues" tab and clicking on the "New Issue" button.
Contributions are welcome! If you would like to contribute to this project, please follow these steps:
- Fork the repository by clicking the "Fork" button at the top of the page.
- Clone the forked repository to your local machine:
git clone https://github.com/<your-username>/LinearRegression.git
- Create a new branch for your feature or bugfix:
git checkout -b my-feature-branch
- Make your changes and commit them:
git commit -m "Add new feature" - Push your changes to your fork:
git push origin my-feature-branch
- Open a pull request from your branch to the
mainbranch of this repository.
This project is licensed under the MIT License. See the LICENSE file for more information.