Linear Regression Line Through Brute Force

Part 2/5 in Linear Regression

Pratik Shukla
4 min readMay 14, 2020

Part 1 : Linear Regression From Scratch.

Part 2 : Linear Regression Line Through Brute Force.

Part 3 : Linear Regression Complete Derivation.

Part 4 : Simple Linear Regression Implementation From Scratch.

Part 5 : Simple Linear Regression Implementation Using Scikit-Learn.

Welcome!

What is least square property?

Form the distance (y — y’) between each data point (x,y) and a potential regression line y’ = mx +b. Each of these differences is known as a residual. Square these residual and sum them. The resulting sum is called residual sum of squares or SSres. The line that “best fits” has the least possible value of SSres.

In the last article we saw how to find the equation of line when two points are given. But what if we have more than 2 points? In this article we’ll see how to find the regression that “best fits” our data( i.e. minimum error). In this article we’ll see about the brute force attack to find the regression line.

We’ll find the regression line for the following data:

Dataset Provided

Now we have to find the square of each row in Y — Y’.

As we all know :

Formula

Finding the values :

Expanding the Parentheses

After adding all the values we get :

Now, remember the vertex of ax²+bx+c :

For example : The vertex of 2x²+3x+6 will be…… : (-3/4)

So, here in our equation we can easily find two equations for with variables m and b in it. Here’s how to find it.

Equation — 1
Equation — 2

Now solving the two equations we get …..

m = 7/4 = 1.75

b = -15/4 = -3.75

So, in the end our equation Y = mX + b will be ……….

Final Equation

Here’s how our regression line looks on coordinate plane :

“Best Fit” Line

To conclude, in this article we saw how we can find the line that “best fits” the given data points. But as you can see this is very time consuming approach. We generally use python libraries to perform such tasks. But we must know the logic behind each algorithm, right? So in the next article we’ll see how we can perform such calculations using a single formula and we’ll also see about how that formula is derived.

Moving forward,

In the next article we’ll see about the derivation of simple linear regression formula.

To find more such detailed explanation, visit my blog: patrickstar0110.blogspot.com

(1) Simple Linear Regression Explained With It’s Derivation:
https://youtu.be/1M2-Fq6wl4M

(2)How to Calculate The Accuracy Of A Model In Linear Regression From Scratch :
https://youtu.be/bM3KmaghclY

(3) Simple Linear Regression Using Sklearn :
https://youtu.be/_VGjHF1X9oU

You can download the code and some handwritten notes on the derivation from here : https://drive.google.com/open?id=1_stSoY4JaKjiSZqDdVyW8VupATdcVr67

If you have any additional questions, feel free to contact me : shuklapratik22@gmail.com

Thank You!

--

--