Polynomial Regression

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Polynomial Regression:

If our data points clearly will not fit a linear regression (a straight line through all data points), it
might be ideal for polynomial regression.

Polynomial Regression is a regression algorithm that models the relationship between a


dependent(y) and independent variable(x) as nth degree polynomial. The Polynomial Regression
equation is given below:

y= b0+b1x1+ b2x12+ b2x13+...... bnx1n

o It is also called the special case of Multiple Linear Regression in ML. Because we add some
polynomial terms to the Multiple Linear regression equation to convert it into Polynomial
Regression.
o It is a linear model with some modification in order to increase the accuracy.
o The dataset used in Polynomial regression for training is of non-linear nature.
o It makes use of a linear regression model to fit the complicated and non-linear functions and
datasets.

Need for Polynomial Regression:


The need of Polynomial Regression in ML can be understood in the below points:

o If we apply a linear model on a linear dataset, then it provides us a good result as we have
seen in Simple Linear Regression, but if we apply the same model without any modification
on a non-linear dataset, then it will produce a drastic output. Due to which loss function will
increase, the error rate will be high, and accuracy will be decreased.
o So for such cases, where data points are arranged in a non-linear fashion, we need the
Polynomial Regression model. We can understand it in a better way using the below
comparison diagram of the linear dataset and non-linear dataset.
o In the above image, we have taken a dataset which is arranged non-linearly. So, if we try to
cover it with a linear model, then we can clearly see that it hardly covers any data point. On
the other hand, a curve is suitable to cover most of the data points, which is of the Polynomial
model.

Hence, if the datasets are arranged in a non-linear fashion, then we should use the Polynomial
Regression model instead of Simple Linear Regression.

Equation of the Polynomial Regression Model:


Simple Linear Regression equation:
        y = b0+b1x         .........(a)
Multiple Linear Regression equation:
        y= b0+b1x+ b2x2+ b3x3+....+ bnxn         .........(b)
Polynomial Regression equation:        
y= b0+b1x + b2x2+ b3x3+....+ bnxn         ..........(c)

When we compare the above three equations, we can clearly see that all three equations are
Polynomial equations but differ by the degree of variables. The Simple and Multiple Linear equations
are also Polynomial equations with a single degree, and the Polynomial regression equation is Linear
equation with the nth degree. So if we add a degree to our linear equations, then it will be converted
into Polynomial Linear equations.

Implementation of Polynomial Regression using Python:


Step 1: Import libraries and dataset

# Importing the libraries


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd  

# Importing the dataset


datas = pd.read_csv('data.csv')
print(datas)
Step 2: Dividing the dataset into 2 components
Divide dataset into two components that is X and y. X will contain the Column between 1 and 2. y
will contain the 2 columns.

X = datas.iloc[:, 1:2].values
y = datas.iloc[:, 2].values

Step 3: Fitting Linear Regression to the dataset

# Fitting Linear Regression to the dataset


from sklearn.linear_model import LinearRegression
lin = LinearRegression()
lin.fit(X, y)

 
Step 4: Fitting Polynomial Regression to the dataset
Fitting the Polynomial Regression model on two components X and y.

# Fitting Polynomial Regression to the dataset


from sklearn.preprocessing import PolynomialFeatures   
poly = PolynomialFeatures(degree = 4)
X_poly = poly.fit_transform(X)
poly.fit(X_poly, y)
lin2 = LinearRegression()
lin2.fit(X_poly, y)
 
Step 5: In this step we are Visualising the Linear Regression results using scatter
plot.

# Visualising the Linear Regression results

plt.scatter(X, y, color = 'blue')


plt.plot(X, lin.predict(X), color = 'red')
plt.title('Linear Regression')
plt.xlabel('Temperature')
plt.ylabel('Pressure')
plt.show()

 
Step 6: Visualising the Polynomial Regression results using scatter plot

# Visualising the Polynomial Regression results


plt.scatter(X, y, color = 'blue')
plt.plot(X, lin2.predict(poly.fit_transform(X)), color = 'red')
plt.title('Polynomial Regression')
plt.xlabel('Temperature')
plt.ylabel('Pressure')
plt.show()
 
Step 7: Predicting new result with both Linear and Polynomial Regression.

# Predicting a new result with Linear Regression


lin.predict(110.0)

# Predicting a new result with Polynomial Regression


lin2.predict(poly.fit_transform(110.0))

Disadvantage of using Polynomial Regression:


 These are too sensitive to the outliers.
 The presence of one or two outliers in the data can seriously affect the results of a nonlinear
analysis.
 In addition, there are unfortunately fewer model validation tools for the detection of outliers
in nonlinear regression than there are for linear regression.
Some Questions related to Polynomial Regression:
1. What is Polynomial Regression?
2. Why we use Polynomial Regression?
3. How the accuracy increases while using Polynomial Regression?
4. How Polynomial Regression different from Linear Regression?
5. What is the main disadvantage of using Polynomial Regression?

You might also like