Polynomial Regression ( From Scratch using Python )
- Linear Regression
- Gradient Descent
Linear Regression finds the correlation between the dependent variable ( or target variable ) and independent variables ( or features ). In short, it is a linear model to fit the data linearly. But it fails to fit and catch the pattern in non-linear data.
Let’s first apply Linear Regression on non-linear data to understand the need for Polynomial Regression. The Linear Regression model used in this article is imported from sklearn. You can refer to the separate article for the implementation of the Linear Regression model from scratch.
As shown in the output visualization, Linear Regression even failed to fit the training data well ( or failed to decode the pattern in the Y with respect to X ). Because its hypothetical function is linear in nature and Y is a non-linear function of X in the data.
For univariate linear regression : h( x ) = w * x here, x is the feature vector. and w is the weight vector.
This problem is also called as underfitting. To overcome the underfitting, we introduce new features vectors just by adding power to the original feature vector.
For univariate polynomial regression : h( x ) = w1x + w2x2 + .... + wnxn here, w is the weight vector. where x2 is the derived feature from x.
After transforming the original X into their higher degree terms, it will make our hypothetical function able to fit the non-linear data. Here is the implementation of the Polynomial Regression model from scratch and validation of the model on a dummy dataset.
We also normalized the X before feeding into the model just to avoid gradient vanishing and exploding problems.
Output visualization showed Polynomial Regression fit the non-linear data by generating a curve.
Attention reader! Don’t stop learning now. Get hold of all the important Machine Learning Concepts with the Machine Learning Foundation Course at a student-friendly price and become industry ready.