git clone https://github.com/RhesusP/ft_linear_regression.git
cd ft_linear_regression
First, you need to train the model by running the following command:
python3 trainer.py
Then, you can use the model to make predictions by running the following command:
python3 predict.py
This project is an introduction to machine learning. The goal is to predict the price of a car based on its mileage. The model is trained using a linear regression algorithm.
General steps are :
- Read the data from the
data.csv
file. - Perform a linear regression on the data.
- Save
$\theta_0$ and$\theta_1$ to a file. - Use the model to make predictions.
The dataset is a CSV file containing two columns: km
and price
.
We represent the dataset as follows :
-
$x$ is the mileage of the car. -
$y$ is the price of the car.
From the dataset, we can deduce 2 important values:
-
$m$ : the number of samples (rows) in the dataset (here,$m = 24$ ). -
$n$ : the number of features in the dataset (here,$n = 1$ = mileage).
The model is a linear regression model. It is represented by the following equation:
The goal is to find the values of
The cost function measures errors between
The sum of all errors is calculated as follows:
This is known as the Mean Squared Error (MSE).
The MSE is a sum of squared errors, so it have a convex shape with a single minimum. We are looking for this minimum with the Gradient Descent algorithm.
The Gradient Descent algorithm is as follows:
- Initialize
$a$ and$b$ to random values (0 in this project). - Calculate the derivative of this point.
- Move with a step size
$\alpha$ in the opposite direction of the derivative. - Repeat steps 2 and 3 until convergence.
The learning rate
For
For
We are going to perform a linear regression on the dataset using matrix operations. This will allow us to calculate the
values of
We still have the same dataset
The model is represented by the following equation:
Where:
We have
Where :
-
$y^{(i)}$ is all the values of$y$ from the dataset. -
$a x^{(i)} + b$ is$X \theta$ . - the
$\frac{1}{2m}$ and$^2$ are to simplify calculations.
So, the matrix form of the cost function is:
We have formulas for the two gradients:
We put these two gradients in a vector:
This vector calculate all the J derivatives for each
We have can calculate the gradient with the following formula:
We can now apply the Gradient Descent algorithm to the matrix form of the cost function and loop until convergence.
We can calculate the new