Multiple Linear Regression From Scratch + Implementation in Python
Before Moving Further , if you are not familiar with Single variate Linear Regression , please do read my previous 2 posts and get familiar with it.
Today we are going to learn Multivariate Linear Regression ,
Lets take an example of Real World,
You want to predict if a Person has diabetes or not, for that what are the things you consider , may be age , gender, heredity, lifestyle , drinking habits etc, more than one variable is responsible for correct prediction , this is what we call Multiple Linear Regression where more than one variables effects the Model.
Lets directly dive into the mathematics behind it.
Below in Pic 1 , there is a Simple Uni-variate Linear Regression which we have already Solved , and now we have equation for Multiple Linear Regression where parameters range from beta(1) to beta(n) and we have to find the values for these parameters solving the equation of Multiple Linear Regression.
In pic 1 , we have now represented our Multiple Linear Regression equation in the form of Matrix so that it would be easy to solve.
Remember , how we solved our Simple Uni-variate Linear Regression ? ,
Yes by Minizmin the sum of Squares error , and yes for this equation too, we will solve by minimizing sum of Squares error.
In Pic 2 , we write equation for Residual sum of Squares ( RSS ) and substitute the values of y(hat) and y.
Now , comes the challenging and fun part, solve the equation , there are many and many methods to solve , here in this article we would be using matrix method.
We need to be familiar with some Matrix Formula before Solving the equation , the formulae are show in pic 3.
We need to solve our equation for beta(hat) so we take derivative on our RSS ( sum of squares ) equation on beta(hat) and use above mentioned matrix formula, I easily got the solution and you can too , its easy peasy
and now its time for implementation in Python
Today we are going to learn Multivariate Linear Regression ,
Lets take an example of Real World,
You want to predict if a Person has diabetes or not, for that what are the things you consider , may be age , gender, heredity, lifestyle , drinking habits etc, more than one variable is responsible for correct prediction , this is what we call Multiple Linear Regression where more than one variables effects the Model.
Lets directly dive into the mathematics behind it.
Below in Pic 1 , there is a Simple Uni-variate Linear Regression which we have already Solved , and now we have equation for Multiple Linear Regression where parameters range from beta(1) to beta(n) and we have to find the values for these parameters solving the equation of Multiple Linear Regression.
In pic 1 , we have now represented our Multiple Linear Regression equation in the form of Matrix so that it would be easy to solve.
![]() |
| pic 1 |
Remember , how we solved our Simple Uni-variate Linear Regression ? ,
Yes by Minizmin the sum of Squares error , and yes for this equation too, we will solve by minimizing sum of Squares error.
In Pic 2 , we write equation for Residual sum of Squares ( RSS ) and substitute the values of y(hat) and y.
![]() |
| Pic 2 |
Now , comes the challenging and fun part, solve the equation , there are many and many methods to solve , here in this article we would be using matrix method.
We need to be familiar with some Matrix Formula before Solving the equation , the formulae are show in pic 3.
![]() |
| pic 3 |
We need to solve our equation for beta(hat) so we take derivative on our RSS ( sum of squares ) equation on beta(hat) and use above mentioned matrix formula, I easily got the solution and you can too , its easy peasy
![]() |
| pic 4 |
and now its time for implementation in Python




Comments
Post a Comment