How to Perform Quadratic Regression in Python?
Last Updated :
20 Feb, 2022
The quadratic equation is a method of modeling a relationship between sets of independent variables is quadratic regression or we can say the technique of obtaining the equation of a parabola that best fits a collection of data is known as quadratic regression. We use the R square metric to measure the relative predictive power of a Quadratic Regression.
The equation of quadratic equation is of the form:
y=
where a, b , c are constants and a ≠0.
The term quadratic refers to an equation that has a power of 2.
Used Dataset: Click here
Quadratic Regression in Python
The code starts with importing the necessary packages, then the CSV file is read using the read_csv() and visualizes the data. visualizing the data using a seaborn scatterplot. Adding a polynomial line to the data to view the fit. np.polyfit() and np.poly1d() is used to create a quadratic fit and a quadratic equation.
Syntax: numpy.polyfit (X, Y, deg, rcond=None, full=False, w=None, cov=False)
parameters:
- X: array like object, it represents the x coordinates.
- Y:array like object, it represents the y coordinates.
- deg: integer, degree of the polynomial .
R square metric is used to measure the predictive power. R square value determines the proportion of variance for a dependent value. r2 value generally ranges from 0 to 1. an r square value near 1 is fairly good.
Python3
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import r2_score
import scipy.stats as stats
dataset = pd.read_csv( 'ball.csv' )
sns.scatterplot(data = dataset, x = 'time' ,
y = 'height' , hue = 'time' )
plt.title( 'time vs height of the ball' )
plt.xlabel( 'time' )
plt.ylabel( 'height' )
plt.show()
model = np.poly1d(np.polyfit(dataset[ 'time' ],
dataset[ 'height' ], 2 ))
polyline = np.linspace( 0 , 10 , 100 )
plt.scatter(dataset[ 'time' ], dataset[ 'height' ])
plt.plot(polyline, model(polyline))
plt.show()
print (model)
print (r2_score(dataset[ 'height' ],
model(dataset[ 'time' ])))
|
Output:
2
2.008 x - 21.69 x + 121.7
0.8133556222534604
R square value is above 0.7 so it implies quadratic fit is a fit for the data.
Share your thoughts in the comments
Please Login to comment...