Polynomial Regression Example in Python

   Polynomial regression is a nonlinear relationship between independent x and dependent y variables. Fitting such type of regression is essential when we analyze fluctuated data with some bends. In this post, we'll learn how to fit a curve with polynomial regression data and plot it in Python. We use Scikit-Learn, NumPy, and matplotlib libraries in this tutorial.

We'll start by loading the required modules for this tutorial.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

We need test data, and we can generate it as shown below. You can use your own dataset too.

n = 250         # elements number
x = list(range(n))
x = [i/100 for i in x]

def GetPolyData(x):    
    return np.sin(x) + np.random.uniform(-.2, .2, n) 

y = GetPolyData(x)
train_x = np.array(x)
train_y = np.array(y)

We'll check x data visually by creating a scatter plot.

plt.scatter(train_x, train_y)
plt.show();



Next, we'll define the polynomial model with PolymonialFeatures class fit it on training data.

polyModel = PolynomialFeatures(degree = 4)
xpol = polyModel.fit_transform(train_x.reshape(-1, 1))
preg = polyModel.fit(xpol,train_y)

We need a linear model and we'll define it and fit on training data. Then we'll predict the x data with the model.

liniearModel = LinearRegression(fit_intercept = True)
liniearModel.fit(xpol, train_y[:, np.newaxis])
polyfit = liniearModel.predict(preg.fit_transform(train_x.reshape(-1, 1)))

Finally, we'll plot the fitted curve.

plt.scatter(train_x, train_y)
plt.plot(train_x, polyfit, color = 'red')
plt.show();



   In this post, we've briefly learned how to fit the polynomial regression data in Python. The full source code is listed below.


import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

# Creating a sample data
n = 250
x = list(range(n))
x = [i/100 for i in x]

def GetPolyData(x):    
    return np.sin(x) + np.random.uniform(-.2, .2, n) 

y = GetPolyData(x)
train_x = np.array(x)
train_y = np.array(y)
 
plt.scatter(train_x, train_y)
plt.show(); 
 
# building polynomial model
polyModel = PolynomialFeatures(degree = 4)
xpol = polyModel.fit_transform(train_x.reshape(-1, 1))
preg = polyModel.fit(xpol,train_y)

# Builiding linear model
liniearModel = LinearRegression(fit_intercept = True)
liniearModel.fit(xpol, train_y[:, np.newaxis])

# Fitting with linear model
polyfit = liniearModel.predict(preg.fit_transform(train_x.reshape(-1, 1)))

# Plot results
plt.scatter(train_x, train_y)
plt.plot(train_x, polyfit, color = 'red')
plt.show();
print(polyfit)


Polynomial regression curve fitting in R


No comments:

Post a Comment