Header Ads

Machine Learning - Polynomial Regression




   Polynomial Regression is as simple as other Regression.Here we will take a small dataset ,so we are going to take the full dataset as training set.

click here to get the dataset.
#polynomial regression
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

#import_datset
dataset = pd.read_csv('Position_Salaries.csv')
x = dataset.iloc[:, 1:2].values
y= dataset.iloc[:, 2].values

#fitting linear regression to the dataset
from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(x,y)

#fitting polynomial regression to the dataset
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 6)
x_poly = poly_reg.fit_transform(x)

lin_reg_2 = LinearRegression()
lin_reg_2.fit(x_poly, y)

plt.scatter(x,y , color = 'red')
plt.plot(x, lin_reg_2.predict(poly_reg.fit_transform(x)), color = 'blue')
plt.title('truth or bluff-Polynomial Linear Regression')
plt.xlabel('position')
plt.ylabel('salary')
plt.show()

#prediction with PLRM (we want to predict the salary of level 6.5 )
lin_reg_2.predict(poly_reg.fit_transform(6.5))



In the next blog we will discuss about some Advanced Regression Model.
Stay tuned :)

No comments

Powered by Blogger.