Linear Regression in Machine Learning

Linear Regression: 

Image Source - Google | Image by - Warrior Trading



Linear Regression is a basic algorithm, basically what is does is it looks into the scatter of data points and attempts to find a best fit line to those data points. 

There will is some kind of co-relation between x and y

Image Source - Google | Image by - Machine Learning Mindset


This is what the linear regression attempts to do.

 In some cases Linear Regression may not be best:- 



Image Source - Google | Image by - SPH - Boston University

If we have a data that is more randomised we can still draw best fit line, but may not be a good fit line. 

So, essentially we may wanna draw a line across or towards downward, but it may not give you accurate results. This is when, we don’t use Linear Regression. 

So, we use Linear Regression when we have data that directly co-related with each other. There will be some kind of strong co-relation between each other.

This line can be defined as y = mx + c
y is dependent on the equation mx + c 
we can find y may manipulating the values of x,where m is the slope of the line. 
Slope define how fast our line increases. We can have a negative slope and a positive slope.

Slope of the line can be found by taking to points on the line. Let’s consider them to be P1 and P2 

Image Source - Google | Image by - Math Warehouse


To find the slope we consider the x and y co-ordinates of those two points :

 m = (y2 - y1)/(x2 – x1) or m = (y1 – y2)/(x1 – x2) 

It doesn’t really matter what formula you use because it gonna give the same results. Basically we are not gonna do this, this is what our computer or algorithm does for us. 

 C = y intercept > this is where our line intercept with the y-axis

 What essentially computer does is, we give it all of this data by using the Linear Regression it created this line i.e best fit line. When we want to predict the value it uses it equation to predict. 

 It’s actually simple in 2d space. But we have multiple variables or attributes, when we create the best fit line, it creates the best fit line in multi dimensional space

Example:
Image Source - Google | Image by - MathWorks


Python Program to implement Linear Regression


Post a Comment