# Polynomial Regression in Python – Step 5.) Visualize the Results of Polynomial Regression

# Import the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Import the CSV Data
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values

# Split the dataset into Training set and Test set
from sklearn.cross_validation import train_test_split
xtrain, xtest, ytrain, ytest = train_test_split(X, y, test_size = 0.2)

# Training Polynomial Regression Model
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4)
X_poly = poly_reg.fit_transform(xtrain)
poly_reg.fit(X_poly, ytrain)
lin_reg = LinearRegression()
lin_reg.fit(X_poly, ytrain)

# Predict Result with Polynomial Regression

lin_reg.predict(poly_reg.fit_transform(xtest[0]))
lin_reg.predict(poly_reg.fit_transform(xtest[1]))

# Visualise the Results of Polynomial Regression
plt.scatter(xtrain, ytrain, color = ‘red’)
plt.plot(X, lin_reg_2.predict(poly_reg.fit_transform(X)), color = ‘blue’)
plt.title(‘Polynomial Regression’)
plt.xlabel(‘Position level’)
plt.ylabel(‘Salary’)
plt.show()

# Visualise the Results of Polynomial Regression with Smoother Curve
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_reg_2.predict(poly_reg.fit_transform(X_grid)), color = ‘blue’)
plt.title(‘Polynomial Regression’)
plt.xlabel(‘Position level’)
plt.ylabel(‘Salary’)
plt.show()

##### Other Sections on Polynomial Regression :

Step 5.) Visualize the Results of Polynomial Regression