Linear Regression From Scratch

So as upto now you can already Solve a Linear Regression , as we already discussed in earlier lessons mathematically.
Today we are going to do a Linear regression Model in Python from scratch , which means without the use of any library,

For that we need to first have our mathematical  equations for our Linear Regression Model ,

I strongly and very strongly suggest you to solve each of these maths on your own , take your time.

Here I have solved the Linear Regression Model and made it simplified so that it would be easy to code in python.

Below in Picture 1 ,  we have simply derived a equation( equation....1 )  , using partial derivative on "a"  on SSE ( that is our Cost Function )




Below in picture 2 , we again derive another equation( equation..... 2 )  performing partial derivative on "b" in our cost function and also used a little bit of trick to simplify in simpler form.


pic 2


In pic 3 and Pic 4
Now from equation......1 and equation........2 we solve for expressions of "a" and "b" , I have used simple Substitution method, you may try by matrix method too.

Pic 3

pic 4


Coming upto here , we have expression for our two independent varialbles "a" and "b" ,check the denominator of both "a" and "b" are same 
Now its time to code in python.

In pic 5 ,
We have used Numpy , a library for performing Linear Algebra function in Python.
we calcualte absolute values of "a" and "b" and plug those values of "a" and  "b"  in our regression model.
y = a * X + b

pic 5






Now its time to check  how effective is  your Linear Regression Model.
For that we use R(squared) method, this technique is used for any regression to check its effectiveness not only in Linear Model.
value of R(squared) lies from 0 to 1 and more the value , more the model goes into perfection.

R(Squared) is given by




if Rsquared =  1 ( becomes when SS residual = 0 )  , we have the perfect Model.
if Rsquared =  0 ( becomes when SS residual / SS total  = 1 )  , We just predicted the average of Y, which is not good.








Complete Code Implementation can be found here - https://github.com/prajinkhadka/linear_regression_scratch-







Comments