Machine Learning Algorithm- Polynomial Regression

Polynomial Regression


#CODE-  Polynomial Regression

import pandas as pd
import matplotlib.pyplot as plt
dataset=pd.read_csv('C:\\ClassStudies_Python\\Position_Salaries.csv')
x=dataset.iloc[:,1:2].values
y=dataset.iloc[:,2].values
from sklearn.linear_model import LinearRegression 
#lin_reg.fit(x,y)
lin_reg=LinearRegression()
lin_reg.fit(x,y)

from sklearn.preprocessing import PolynomialFeatures
poly_reg=PolynomialFeatures(degree=4)
x_poly=poly_reg.fit_transform(x)
poly_reg.fit(x_poly,y)

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

plt.scatter(x,y,color='r')
plt.plot(x,lin_reg.predict(x),color='b')
plt.title("truth or Bluff(Linear Regression)")
plt.xlabel('position')
plt.ylabel('salary')
plt.show()


plt.scatter(x,y,color='r')
plt.plot(x,lin_reg2.predict(poly_reg.fit_transform(x)),color='c')
plt.title("truth or Bluff(Polynomial Regression)")
plt.xlabel('position')
plt.ylabel('salary')
plt.show()

linpre= lin_reg.predict(6.5)
print(linpre)


polypre= lin_reg.predict(6.5)
print(polypre)

Output :

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top