polynomial regression

DS/ML: Polynomial Regression implementation (Python and R)

Implementing salary prediction based on job position using polynomial linear regression in Python and R.

polynomial equation

Python Code:

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

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

# Feature Scaling
"""from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)"""

# 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 = 4)
X_poly = poly_reg.fit_transform(X)
poly_reg.fit(X_poly, y)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(X_poly, y)

# Visualising the Linear Regression results
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()

# Visualising the Polynomial Regression results
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 Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

# Visualising the Polynomial Regression results (for higher resolution and 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('Truth or Bluff (Polynomial Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

# Predicting a new result with Linear Regression
lin_reg.predict(6.5)

# Predicting a new result with Polynomial Regression
lin_reg_2.predict(poly_reg.fit_transform(6.5))

R Code:

# Importing the dataset
dataset = read.csv('Position_Salaries.csv')
dataset = dataset[2:3]

# Fitting Linear Regression to the dataset
lin_reg = lm(formula = Salary ~ .,
 data = dataset)

# Fitting Polynomial Regression to the dataset
dataset$Level2 = dataset$Level^2
dataset$Level3 = dataset$Level^3
dataset$Level4 = dataset$Level^4
poly_reg = lm(formula = Salary ~ .,
 data = dataset)

# Visualising the Linear Regression results
# install.packages('ggplot2')
library(ggplot2)
ggplot() +
 geom_point(aes(x = dataset$Level, y = dataset$Salary),
 colour = 'red') +
 geom_line(aes(x = dataset$Level, y = predict(lin_reg, newdata = dataset)),
 colour = 'blue') +
 ggtitle('Truth or Bluff (Linear Regression)') +
 xlab('Level') +
 ylab('Salary')

# Visualising the Polynomial Regression results
# install.packages('ggplot2')
library(ggplot2)
ggplot() +
 geom_point(aes(x = dataset$Level, y = dataset$Salary),
 colour = 'red') +
 geom_line(aes(x = dataset$Level, y = predict(poly_reg, newdata = dataset)),
 colour = 'blue') +
 ggtitle('Truth or Bluff (Polynomial Regression)') +
 xlab('Level') +
 ylab('Salary')

# Visualising the Regression Model results (for higher resolution and smoother curve)
# install.packages('ggplot2')
library(ggplot2)
x_grid = seq(min(dataset$Level), max(dataset$Level), 0.1)
ggplot() +
 geom_point(aes(x = dataset$Level, y = dataset$Salary),
 colour = 'red') +
 geom_line(aes(x = x_grid, y = predict(poly_reg,
 newdata = data.frame(Level = x_grid,
 Level2 = x_grid^2,
 Level3 = x_grid^3,
 Level4 = x_grid^4))),
 colour = 'blue') +
 ggtitle('Truth or Bluff (Polynomial Regression)') +
 xlab('Level') +
 ylab('Salary')

# Predicting a new result with Linear Regression
predict(lin_reg, data.frame(Level = 6.5))

# Predicting a new result with Polynomial Regression
predict(poly_reg, data.frame(Level = 6.5,
 Level2 = 6.5^2,
 Level3 = 6.5^3,
 Level4 = 6.5^4))

Output:

polynomial regression

Both Python and R code are available in my Github page. You can download the sample csv file (Position_Salaries.csv) from the same place.

Credits: Udemy

3 thoughts on “DS/ML: Polynomial Regression implementation (Python and R)”

  1. Pingback: Milana Travis

Leave a Reply

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