Polynomial Regression in Python

Rahul Kadam
Analytics Vidhya
Published in
3 min readJan 22, 2021
Polynomial Regression

When your linear regression algorithm(straight line through dataset) fails to fit the data points then it might be ideal to use Polynomial Regression(PR).

Polynomial Regression:

Polynomial regression is the nonlinear relationship between an independent variable(X) and a dependent variable(y), and it is modeled as nth degree polynomial in X.

Question: If it is a nonlinear then why is it called as polynomial linear regression?

Answer: It is the form of linear regression. In every linear regression equation to find the value of dependent variable(y) we first need to find the values of coefficient(b1, b2, …,bn) and interceptor(b0). For any values of independent variable(X), values of coefficient and interceptor will remain same. In polynomial regression we do the same so that’s why it is called as polynomial linear regression.

A polynomial linear regression equation

Now, it’s time to code polynomial regression using python,

I will give a link to the code in the end!!!

1. First of all, we have to import libraries and datasets which we will be using to train our model.

Link to download the dataset: https://www.kaggle.com/jaspreet007bhatia/weather-history

Data set view
Weather History data

2. Now we need to clean our data,

  • We have to check if we have any missing values or not.
  • Now we will drop unnecessary columns, like the ‘Formatted date’ column.
  • After that, we need to encode categorical data means, we will convert our string values into 1’s and 0’s.
  • And in the last, we will separate the dataset into independent(X) and dependent(y) variables.

3. It’s time to split the dataset into training and testing sets,

We split the dataset into training and testing sets to avoid the effect of data discrepancies. We will split most of the data into a training set and a smaller portion into a testing set.

4. Training the dataset,

To train the dataset we will use PolynomialFeatures and LinearRegression classes from the scikit-learn library.

  • Using PolynomialFeatures class we will define the degree of the polynomial.
  • And using LinearRegression class we will train the dataset.

5. Predict the result,

Using predict() method we will predict the result for the testing dataset, or we can predict our own result also.

6. Coefficients and interceptor,

To finish the polynomial regression equation we need the values of coefficients and interceptor.

Code Links::

Kaggle Notebook code link: https://www.kaggle.com/rahulkadam0909/polynomial

Github code link: https://github.com/RAHUL-KAD/Machine-Learning/blob/main/Regrassion/Code/Polynomial_Regression.ipynb

Happy Coding!!!

--

--

Rahul Kadam
Analytics Vidhya

Deep Learning Enthusiast | Natual Language Processing | Content Writer | Open For Work 👔 | Connect with me on LinkedIn https://www.linkedin.com/in/rahuljkadam/