0% found this document useful (0 votes)
54 views7 pages

UNIT-1 Polynomial Regression

Uploaded by

210701304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views7 pages

UNIT-1 Polynomial Regression

Uploaded by

210701304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIT – 1

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.
o Hence, "In Polynomial regression, the original features are converted into
Polynomial features of required degree (2,3,..,n) and then modeled using a
linear model."

Need for Polynomial Regression:


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.

Note: A Polynomial Regression algorithm is also called Polynomial Linear


Regression because it does not depend on the variables, instead, it depends on
the coefficients, which are arranged in a linear fashion.

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:
Here we will implement the Polynomial Regression using Python. We will
understand it by comparing Polynomial Regression model with the Simple Linear
Regression model. So first, let's understand the problem for which we are going to
build the model.
Steps for Polynomial Regression:
The main steps involved in Polynomial Regression are given below:

o Data Pre-processing
o Build a Linear Regression model and fit it to the dataset
o Build a Polynomial Regression model and fit it to the dataset
o Visualize the result for Linear Regression and Polynomial Regression
model.
o Predicting the output.

Data Pre-processing Step:


The data pre-processing step will remain the same as in previous regression
models, except for some changes. In the Polynomial Regression model, we will not
use feature scaling, and also we will not split our dataset into training and test set.

The code for pre-processing step is given below:

1. # importing libraries
2. import numpy as nm
3. import matplotlib.pyplot as mtp
4. import pandas as pd
5. #importing datasets
6. data_set= pd.read_csv('Position_Salaries.csv')
7. #Extracting Independent and dependent Variable
8. x= data_set.iloc[:, 1:2].values
9. y= data_set.iloc[:, 2].values

o Next, we have imported the dataset 'Position_Salaries.csv', which contains


three columns (Position, Levels, and Salary), but we will consider only two
columns (Salary and Levels).
o After that, we have extracted the dependent(Y) and independent variable(X)
from the dataset. For x-variable, we have taken parameters as [:,1:2],
because we want 1 index(levels), and included :2 to make it as a matrix.
Output:

By executing the above code, we can read our dataset as:


Building the Polynomial regression model:
Now we will build the Polynomial Regression model, but it will be a little different
from the Simple Linear model. Because here we will
use PolynomialFeatures class of preprocessing library. We are using this class to
add some extra features to our dataset.

1. #Fitting the Polynomial regression to the dataset


2. from sklearn.preprocessing import PolynomialFeatures
3. poly_regs= PolynomialFeatures(degree= 2)
4. x_poly= poly_regs.fit_transform(x)
5. lin_reg_2 =LinearRegression()
6. lin_reg_2.fit(x_poly, y)
In the above lines of code, we have used poly_regs.fit_transform(x), because first
we are converting our feature matrix into polynomial feature matrix, and then
fitting it to the Polynomial regression model. The parameter value(degree= 2)
depends on our choice. We can choose it according to our Polynomial features.

After executing the code, we will get another matrix x_poly, which can be seen
under the variable explorer option:
Next, we have used another LinearRegression object, namely lin_reg_2, to fit
our x_poly vector to the linear model.

Output:

Out[11]: LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)

Visualizing the result for Polynomial


Regression
Here we will visualize the result of Polynomial regression model, code for which is little different
from the above model.

Code for this is given below:

1. #Visulaizing the result for Polynomial Regression


2. mtp.scatter(x,y,color="blue")
3. mtp.plot(x, lin_reg_2.predict(poly_regs.fit_transform(x)), color="red")
4. mtp.title("Bluff detection model(Polynomial Regression)")
5. mtp.xlabel("Position Levels")
6. mtp.ylabel("Salary")
7. mtp.show()
Output:

As we can see in the above output image, the predictions are close to the real values. The
above plot will vary as we will change the degree.

For degree= 3:

If we change the degree=3, then we will give a more accurate plot, as shown in the below
image.

Degree= 4: Let's
again change the degree to 4, and now will get the most accurate
plot. Hence we can get more accurate results by increasing the degree of
Polynomial.
Predicting the final result with the Polynomial
Regression model:
Now, we will predict the final output using the Polynomial Regression model to
compare with Linear model. Below is the code for it:

1. poly_pred = lin_reg_2.predict(poly_regs.fit_transform([[6.5]]))
2. print(poly_pred)
Output:

[158862.45265153]

You might also like