Polynomial Regression in Python

Polynomial Regression in Python

Read Time1 Minute, 17 Second

In this article, we will see the step-by-step process of Polynomial Regression using Python.

If you don’t like to read, here’s the video on the same.

Polynomial Regression in Python

You can read all the steps or can get the code on GitHub.

Let’s start coding step by step.

Importing the dataset and libraries.

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

# Importing the dataset
dataset = pd.read_csv('C:\\Users\\shubh\\Downloads\\position_salaries.csv')
X = dataset.iloc[:, 1:-1].values
y = dataset.iloc[:, -1].values

As the data is very small, we have trained the model on whole dataset.Training and predicting from Polynomial Regresssion.

from sklearn.linear_model import LinearRegression

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)

Predicting unseen data if you have any.


Plotting the graph.

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')

If you want to see some other actual time problems codes (DNN, SVM), feel free to check out my github.

Also Read: Multiple Linear Regression in Python