DEV Community

Cover image for Main Idea of Least Squares Method and Linear Regression
Ganesh Kumar
Ganesh Kumar

Posted on

Main Idea of Least Squares Method and Linear Regression

Hello, I'm Ganesh. I'm building git-lrc, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. Star git-lrc on GitHub to help more developers discover the project. Do give it a try and share your feedback for improving the product.

Linear Regression is a method that finds the best fitting line for a set of data points.

It is used to predict the value of a dependent variable based on the value of one or more independent variables.

let's assume we have a set of data points $(x_1, y_1), (x_2, y_2), ..., (x_n, y_n)$.

The data points can be represented as a scatter plot.

The data points are scattered in a way that it may represent a line.

where each point $(x_i, y_i)$ represents a data point.

A line can be represented as $y = mx + b$, where $m$ is the slope and $b$ is the y-intercept.

Usually, we have some random value of $m$ and $b$ and we want to find the best value of $m$ and $b$ such that the line passes through the data points.

For example in this graph there are mulitple data points, and we want to find the best fitting line for these data points.

This can be done using the least squares method.

Least Squares Method

The least squares method is a method that finds the best fitting line for a set of data points.

we calculate the sum of the squares of the differences between the actual values and the predicted values.

$(y_i - (mx_i + b))^2$

We want to minimize this value.

So, we check each state of line and calculate the sum of the squares of the differences between the actual values and the predicted values.

We choose the line that minimizes this value.

How this is done mathematically

This whole caculation can be done mathematically. I will discuss in depth in my next article.

git-lrc

Any feedback or contributors are welcome! It’s online, source-available, and ready for anyone to use.

⭐ Star git-lrc on GitHub

Top comments (0)