Implementation of Linear and Polynomial Regression in Python

Implementation of Linear and Polynomial Regression in Python – Machine Learning

In this tutorial, we will understand the Implementation of Polynomial Linear Regression in Python – Machine Learning.

Importing the Necessary libraries

To begin the implementation first we will import the necessary libraries like NumPy for numerical computation, MatPlotlib for visualization, and pandas for reading the dataset.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

Importing the dataset

Next, we import or read the dataset. Click here to download the Position_Salaries dataset used in this implementation. The dataset has two features Position, Level, and Salary. After reading the dataset, divide the dataset into concepts and targets. Store the concepts into X and targets into y.

dataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:, 1:-1].values
y = dataset.iloc[:, -1].values

Next, display the first five rows of the salary dataset using head() function from pandas.

dataset.head()
PositionLevelSalary
0Business Analyst145000
1Junior Consultant250000
2Senior Consultant360000
3Manager480000
4Country Manager5110000

Training the Linear Regression model on the Whole dataset

A Linear regression algorithm is used to create a model. A LinearRegression function is imported from sklearn.linear_model library.

from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X, y)

Linear Regression classifier model

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)

Training the Polynomial Regression model on the Whole dataset

A polynomial regression algorithm is used to create a model.

from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4)
X_poly = poly_reg.fit_transform(X)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(X_poly, y)

Polynomial Regression classifier model

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)

Visualising the Linear Regression results

Here scatter plot is used to visualize the results. The title of the plot is set to Truth or Bluff (Linear Regression), xlabel is set to Position Level , and ylabel is set to Salary.

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 Linear Regression results

Visualising the Polynomial Regression results

The title of the plot is set to Truth or Bluff (Polynomial Regression), xlabel is set to Position level, and ylabel is set to Salary.

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

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()
higher resolution and smoother curve

Predicting a new result with Linear Regression

lin_reg.predict([[6.5]])

Output:

array([330378.78787879])

Predicting a new result with Polynomial Regression

lin_reg_2.predict(poly_reg.fit_transform([[6.5]]))

Output:

array([158862.45265157])

Summary:

In this tutorial, we understood, the Implementation of Linear and Polynomial Regression in Python. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published.