Polynomial Regression

Pavini Jain
3 min readJul 1, 2021

--

In this module, we will learn about Polynomial Regression and how it works. In the end, we will perform polynomial regression for the following dataset.

Table of Content:

  1. The intuition behind Polynomial Regression
  2. Hands-on

The intuition behind Polynomial Regression

Sometimes, the dataset may not best fit Linear Regression. That’s when Polynomial Regression comes into play. And hence the error is reduced.

The Dataset

Importing the libraries

import numpy as npimport matplotlib.pyplot as pltimport pandas as pd

Importing the dataset

dataset = pd.read_csv(‘Position_Salaries_.csv’)X = dataset.iloc[:, 1:-1].valuesy = dataset.iloc[:, -1].values

Training the Linear Regression model on the whole dataset

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

Training the Polynomial Regression model on the whole dataset with DEGREE=3

from sklearn.preprocessing import PolynomialFeaturespoly1 = PolynomialFeatures(degree = 3)X_poly1 = poly.fit_transform(X)lin_reg2 = LinearRegression()lin_reg2.fit(X_poly1,y)

Training the Polynomial Regression model on the whole dataset with DEGREE=4

from sklearn.preprocessing import PolynomialFeaturespoly = PolynomialFeatures(degree = 4)X_poly = poly.fit_transform(X)lin_reg3 = LinearRegression()lin_reg3.fit(X_poly,y)

Visualizing the Linear Regression results

plt.scatter(X,y,color = 'red')plt.plot(X,lin_reg1.predict(X),color='blue')plt.title('Truth or Bluf (Linear Regression)')plt.xlabel('Positon Level')plt.ylabel('Salary')plt.show()

Visualizing the Polynomial Regression results

plt.scatter(X,y,color='red')plt.plot(X,lin_reg3.predict(X_poly),color='blue')plt.title('Truth or Bluf (Polynomial Regression)')plt.xlabel('Positon Level')plt.ylabel('Salary')plt.show()

Visualizing the Polynomial Regression results (for higher resolution and smoother curve)

To make the curve smooth:

X_grid = np.arange(min(X), max(X), 0.1)X_grid = X_grid.reshape((len(X_grid), 1))plt.scatter(X, y, color = 'red')plt.plot(X_grid, lin_reg3.predict(poly.fit_transform(X_grid)), color = 'blue')plt.title('Truth or Bluff (Polynomial Regression)')plt.xlabel('Position level')plt.ylabel('Salary')plt.show()

Predicting a new result with Linear Regression

lin_reg1.predict([[6.5]]) #[]->list & [[]]->array

Predicting a new result with Polynomial Regression DEGREE=3

The accuracy improves:

lin_reg2.predict(poly1.fit_transform([[6.5]]))

Predicting a new result with Polynomial Regression Degree=4

This gives even better accuracy:

lin_reg3.predict(poly.fit_transform([[6.5]]))

--

--

Pavini Jain
Pavini Jain

Written by Pavini Jain

Student at Jaypee Institute of Information Technology

No responses yet