# Overview of Polynomial Regression

- Introduction
- Applications of Polynomial Regression.
- Mathematics for Polynomial Regression.
- Algorithm for Polynomial Regression.
- Implement a Polynomial Regression model with sci-kit learn.
- Summary

## 1. Overview

- Introduction
- Applications of Polynomial Regression.
- How does that algorithm work?
- How do we choose the best parameters?
- Pseudo Code of the algorithm.
- Implement our model with sci-kit learn
- Summary

Polynomial Regression is defined as: “In statistics, polynomial regression is a form of regression analysis in which the relationship between the independent variable (x) and the dependent variable(y) is modeled as an nth degree polynomial in x.”

This definition can be represented in the mathematical formula as follows:

**y (Dependent Variable) = a + b * x(Independent Variable) ^ n(degree) **

Here, the model becomes the non-linear combination of feature variables i.e., there can be exponential variables, sine, cosine, etc. Polynomial Regression is the type of Linear Regression model. There are different cases of Linear Regression which are briefly stated below:

**Linear Regression with one variable**

h(x) = b_{0} + b_{1}x

In this type of Linear regression, the predicted value(h(x)) depends on only one variable i.e., x.

**Linear Regression with multiple variable**

h(x) = b_{0} + b_{1}x_{1} + b_{2}x_{2} + b_{3}x_{3} + …… + b_{n}x_{n}

In this type of Linear regression, the predicted value(h(x)) depends on multiple variable i.e., x_{1}, x_{2}, … x_{n}.

## Polynomial (Linear) Regression

h(x) = b_{0} + b_{1}x_{1} + b_{2}x_{1}^{2} + b_{3}x_{1}^{3} + ……. + b_{n}x_{1}^{n}

In this type of Linear regression, the predicted value(h(x)) depends on the power of the single variable i.e., x_{1}, x_{1}^{2}, x_{1}^{3} and so on. Polynomial Regression is considered as a special case of Linear Regression. Although polynomial regression fits the non-linear model to data, as a statistical estimation problem it is linear, in the sense that regression function is linear in unknown parameters that are estimated from the data. The gradient models can be trained by Stochastic Gradient Descent (SGD).

## Why use polynomial regression?

Suppose we have the data set that would be plotted in the graph in the way shown in the figure below:

The above figure represents the data of those quantities which increases exponentially over some period of time. The horizontal axis could be considered as time and the vertical axis could be considered as a number of people affected by certain types of epidemics. The graph of no. of person vs time takes the above shape.

If we would like to use the Linear Regression model with one variable in the figure shown above, it would look something like this.

The Regression model is not well fitted as we can see lots of data which are below and above greater distance from the regression model. This would generate the larger squared error and thus would be considered as the bad regression model. The solution to these kinds of problems can be the polynomial regression model.

As shown in the figure, the Regression model fits the data very well. This would minimize our cost function and will give the optimum result on the Regression.

**How do you know you need Polynomial Regression?**

First of all, the polynomial term in the equation of Polynomial Regression creates a U-shaped curve(U or inverted U). If the scatter plot of data gives a curvilinear shape, we need to know that we should use Polynomial Regression. Similarly, if we try to fit a linear model to a curved data, a scatter plot of residuals(Y-axis) on the predictor(X-axis) will have patches of many positive residuals in the middle, but patches of negative residuals at either end(or vice versa). This indicates that the linear model is not a good fit, and the polynomial may give a better result.

**Pros and Cons of using Polynomial Regression**

One advantage of using Polynomial Regression is that model works on any size of the data set. However, it is fast to model and is particularly useful when the relationship to be model is not too complex and if we don’t have a lot of data. Also, the model works very well on non-linear problems. It will also minimize the cost function as it reduces the squared error.

The reason why we should not use polynomial Regression in all types of the data set is that we may end up choosing a wrong polynomial degree which will result in a bad bias/variance trade-off. It may result in over-fitting. The model is also not considered good for highly complex data.

The first figure illustrates Under-fitting. The condition which arises when we use Linear Regression with one variable in the data scattered in that form. This model generates a larger cost function.

The second figure is using polynomial Regression just to the appropriate power. The regression model fits the data well and can be used for better prediction.

The third figure is using polynomial regression to higher powers than required. This is the con of Polynomial Regression. The model fits the data well but it cannot be used for a better prediction.

**Polynomial Regression Using Sci-kit Learn**

If you know the concept of Linear Regression, polynomial Regression is similar, except you need to mention the degree and convert it to the suitable form to be used by Linear Regression later.