DEV Community

Cover image for Linear Regression: First Step towards Machine Learning
gaurav3030
gaurav3030

Posted on

Linear Regression: First Step towards Machine Learning

If you are wondering how to start with machine learning and write hello world program in it then Linear Regression is what you are looking for.

What is Linear Regression?

Linear Regression is a Supervised ML Algorithm that is used to predict some values from given data. Supervised Machine Learning Algorithms need some sample data from which they learn the relationship between given data and output value and then predict when new unseen data is given.
Alt Text
Suppose we have this above dataset and we want to know what is the value of Y at X = 10.
We could say that it will be close to 45 because the value of Y increases as X increases by observing the sample data.

Now how will the machine learn this? The machine will just find a line such that all data points are closer to it.
Alt Text
This Line can be represented as
Y = B0 + B1*X
where B0 and B1 are the coefficients that define this line

B1 = Σ [ (xi - xm)(yi - ym) ] / Σ [ (xi - xm)^2]

B0 = ym - B1* xm

where xm and ym are mean values of X and Y values respectively.

But what if we have Dataset Like this?
Alt Text
We can clearly see that some points are too far away from the line.

Hence, we need to find how nicely this line fits the dataset.
To find how good a line is we need to calculate the Root Mean Squared Error.

RMSE can be calculated as follows:
Alt Text

Where N is the total number of data points.

If RMSE is close to 0, we can say that our regression line is fitted nicely.

Alt Text

In the above plot length of red lines is the difference between predicted and actual values.

As RMSE increases with the difference in Predicted and actual values.

We can say that the RMSE Of fig A is less than that of RMSE of fig B
Hence, the Line in figure B is not as fit as the line in fig. A for predicting values.

Top comments (0)