0% found this document useful (0 votes)
7 views10 pages

Poly Regression

Polynomial regression models the relationship between dependent and independent variables as a polynomial equation. It transforms original features into polynomial features of a specified degree to fit nonlinear datasets with a linear regression model. This allows modeling of complicated nonlinear functions and improves accuracy over a simple linear model on nonlinear data. However, higher degree polynomials increase dimensions and risk overfitting, so feature selection is important.

Uploaded by

harshbafna.ei20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Poly Regression

Polynomial regression models the relationship between dependent and independent variables as a polynomial equation. It transforms original features into polynomial features of a specified degree to fit nonlinear datasets with a linear regression model. This allows modeling of complicated nonlinear functions and improves accuracy over a simple linear model on nonlinear data. However, higher degree polynomials increase dimensions and risk overfitting, so feature selection is important.

Uploaded by

harshbafna.ei20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Polynomial Regression

 Polynomial Regression is a regression algorithm that models


the relationship between a dependent(y) and independent
variable(x) as nth degree polynomial. The Polynomial
Regression equation is :
 The dataset used in Polynomial regression for training is of
non-linear nature.
 It makes use of a linear regression model to fit the
complicated and non-linear functions and datasets.
 In Polynomial regression, the original features are converted
into Polynomial features of required degree (2,3,..,n) and
then modelled using a linear model.
 It is a linear model with some modification in order to
increase the accuracy.
 if a linear model is built without any modification on a non-
linear dataset, then loss function will increase, the error rate
will be high, and accuracy also will be decreased.
The need of Polynomial Regression:
 Where data points are arranged in a non-linear fashion, we need
the Polynomial Regression model. The comparison diagram
illustrates linear model and polynomial model built on linear
dataset and non-linear dataset respectively.
Equation of the Polynomial Regression Model:

• Simple Linear Regression equation: y = b0+b1x


• Multiple Linear Regression equation: y= b0+b1x+ b2x2+ ....+bnxn
• Polynomial Regression equation: y= b0+b1x + b2x2+....+ bnxn
• It can be observed that all three equations are Polynomial equations but
differ by the degree of variables.
• Polynomial regression equation is Linear equation with the nth degree.
So if we add a degree to linear equations, then it will be converted into
Polynomial Linear equations.
• How to identify non-linearity trend in data?
 By visualization- scatter plot between X-features and y and also by
having the correlation coefficient< 70% by default
• Polynomial regression results a better score but with a flip side –
 Polynomial features with higher degree obviously adds more
dimensions to the dataset which leads to curse of dimensionality and
also to overfitting.
 Curse of dimensionality necessitates feature selection to make the
data manageable.
Program example..
• Importing libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
• Creating dataframe
Data= pd.read_csv(‘data.csv')
• Extracting Independent and dependent Variable
X= data_set.iloc[:, 0:4].values
y= data_set.iloc[:, 5].values
Continue..
 Train-test split
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30,
random_state=1)
 Fit the model
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
poly_regs= PolynomialFeatures(degree= 2)
x_poly= poly_regs.fit_transform(X_train)
poly_reg =LinearRegression()
poly_reg.fit(x_poly, y_train)
Continue..
 Model evaluation
poly_pred = poly_reg.predict(poly_regs.fit_transform(X_test)
print(poly_pred)

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

***Dataset has 5 features and a target column…..


Polynomial Transform..

import numpy as np
from sklearn.preprocessing import PolynomialFeatures
X = np.arange(6).reshape(3, 2)
X
• Out: array([[0, 1],
[2, 3],
[4, 5]])
poly = PolynomialFeatures(2)
poly.fit_transform(X)
• Out: array([[ 1., 0., 1., 0., 0., 1.],
[ 1., 2., 3., 4., 6., 9.],
[ 1., 4., 5., 16., 20., 25.]]) //***[1, a, b, a2(a square), ab, b2(b square)]
poly = PolynomialFeatures(interaction_only=True)
poly.fit_transform(X)
• Out: array([[ 1., 0., 1., 0.],
[ 1., 2., 3., 6.],
[ 1., 4., 5., 20.]]) //***[1, a, b, ab]

You might also like