How and when to use polynomial regression?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How and when to use polynomial regression?

How and when to use polynomial regression?

This recipe explains how and when to use polynomial regression

0

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.

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.

Relevant Projects

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.