Polynomial Regression

Polynomial regression is just as simple linear regression except most of the data points are located at the same side of best fit line, therefore making a quadratic kind of curve. 
In this model we would make predictions using both simple linear regression and polynomial regression and compare which best describes this dataset.
Dataset:-
It contains the salary of all the positions of an employee of a company. We need to predict the salary of an former employee who claims to be a Region Manager for half a year.
Code:-
#importing libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#importing dataset
dataset=pd.read_csv('Position_Salaries.csv')
x=dataset.iloc[:,1:2].values
y=dataset.iloc[:,2].values
#splitting datase into training and test set
"""from sklearn.cross_validation import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)"""
Fitting the result into regression model 
#fitting linear regression to dataset
from sklearn.linear_model import LinearRegression
lin_reg=LinearRegression()
lin_reg.fit(x,y)
#fitting polynomial regression to dataset
from sklearn.preprocessing import PolynomialFeatures
poly_reg=PolynomialFeatures(degree=4)
x_poly=poly_reg.fit_transform(x)
lin_reg2=LinearRegression()
lin_reg2.fit(x_poly,y)
Comparing the result of linear and polynomial model graphically
#visualing linear regression result
plt.scatter(x,y,color='red')
plt.plot(x ,lin_reg.predict(x),color='blue')
plt.title('Truth or Bluff(Linear Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
#visualing polynomial regression result
plt.scatter(x,y,color='red')
plt.plot(x ,lin_reg2.predict(poly_reg.fit_transform(x)),color='blue')
plt.title('Truth or Bluff(Polynomial Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
Comparing the predictions made by linear regression model and polynomial regression model
#predicting new result with linear regression
lin_reg.predict(6.5)
#predicting new result with polynomial regression
lin_reg2.predict(poly_reg.fit_transform(6.5))
Result:-
This is our first non linear model so we will compare it to linear just to make a smooth transition from linear to non-linear.
Prediction using Linear regression is 330378.78787879
Prediction using polynomial regression is 158862.45265153
You can see how much more accurate non linear models are.

2 thoughts on “Polynomial Regression

Leave a comment