Figure 1: Iterative method to fit Polynomial Regression
Linear Regression builds a linear relation between features and target variable. But if features and target variables are not linearly related, we can try to fit a polynomial relation between them.
Mathematically polynomial regression is of the form:
$$ \hat{y} = \theta_0 + \theta_1 * x + \theta_2 * x^2 + … + \theta_n * x^n $$
This equation can be viewed as a Multiple Linear Regression:
$$ \hat{y} = \theta_0 + \theta_1 * x_1 + \theta_2 * x_2 + … + \theta_n * x_n $$ Where $x_1 = x$, $x_2 = x^2$, $x_n = x^n$
Frameworks provide utility functions to generate polynomial features from input features. scikit-learn provides PolynomialFeatures(degree=…) for this purpose.
Once input is converted to polynomial features, we can use the approaches used for Linear Regression to fit the model.
In the below example we will generate non-linear data and use scikit-learn to fit the model.