How and when to use polynomial regression in ML in python

This recipe explains how and when to use polynomial regression in ML in python

Recipe Objective

Polynomial Regression is a form of linear regression in which the relationship between the independent variable x and dependent variable y is modeled as an nth degree polynomial.

So this recipe is a short example on How and when to use polynomial regression. Let's get started.

Learn to Implement Customer Churn Prediction Using Machine Learning in Python

Step 1 - Import the library

from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.datasets import load_boston from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures

Let's pause and look at these imports. We have exported train_test_split which helps in randomly breaking the datset in two parts. Here sklearn.dataset is used to import one classification based model dataset. Also, we have exported LinearRegression and PolynomialFeatures to build the model.

Step 2 - Setup the Data

X,y=load_boston(return_X_y=True) poly = PolynomialFeatures(degree = 2) X = poly.fit_transform(X) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)

Here, we have used load_boston function to import our dataset in two list form (X and y) and therefore kept return_X_y to be True. Thereby, we have introduced polnomial features in our dataset for degree upto 2. Further with have broken down the dataset into 2 parts, train and test with ratio 3:4.

Now our dataset is ready.

Step 3 - Building the model

model = LinearRegression()

We have simply built a regressor model with LinearRegression (our data already has polnomial features and linear regreession simply means predicting coefficient) with default values.

Step 4 - Fit the model and predict for test set

model.fit(X_train, y_train) expected_y = y_test predicted_y = model.predict(X_test)

Here we have simply fit used fit function to fit our model on X_train and y_train. Now, we are predicting the values of X_test using our built model.

Step 5 - Printing the results

print(model.score(X_train,y_train)) print(model.score(X_test,y_test))

Here we have calculating accuracy score of our trained set and also, on the unknown dataset (X_test and y_test)

Step 6 - Lets look at our dataset now

Once we run the above code snippet, we will see:

0.9371457121192392
0.7328994385490928

The model has low accuracy score on unknown datset and hence might not be that efficient.

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.