import numpy as np
import matplotlib.pyplot as plt
import math
Polynomial Regression
Polynomial Regression is a regression technique that models the relationship between a dependent variable and one or more independent variables by fitting a polynomial equation. It expands on linear regression by introducing higher-degree polynomial terms, allowing for a more flexible curve to capture non-linear patterns in the data.
Libraries Required
Polynomial Features
def create_polynomial_features(X, degree):
= np.ones((X.shape[0], 1))
X_poly for d in range(1, degree + 1):
= np.hstack((X_poly, X.reshape(-1, 1) ** d))
X_poly return X_poly
def solve_normal_equation(X, y):
return np.linalg.inv(X.T @ X) @ X.T @ y
def predict(X, coefficients):
return X @ coefficients
Dataset ( y = 1 + x^2 )
= np.arange(0, 20, 1)
x = 1 + x**2
y
='x', c='r', label="Actual Value")
plt.scatter(x, y, marker
plt.legend()"y=1+x^2")
plt.title( plt.show()
Predictions for Different Degrees
Degree = 0
= 0 degree
= create_polynomial_features(x, degree)
X_poly
= solve_normal_equation(X_poly, y)
coefficients
= predict(X_poly, coefficients)
y_pred
# Plot the results
=(12, 6))
plt.figure(figsize='gray', label='Actual')
plt.scatter(x, y, color='blue', label='Train Prediction')
plt.plot(x, y_pred, color'X')
plt.xlabel('y')
plt.ylabel(
plt.legend() plt.show()
Degree = 1
= 1 degree
= create_polynomial_features(x, degree)
X_poly
= solve_normal_equation(X_poly, y)
coefficients
= predict(X_poly, coefficients)
y_pred
# Plot the results
=(12, 6))
plt.figure(figsize='gray', label='Actual')
plt.scatter(x, y, color='blue', label='Train Prediction')
plt.plot(x, y_pred, color'X')
plt.xlabel('y')
plt.ylabel(
plt.legend() plt.show()
Degree = 2
= 2 degree
= create_polynomial_features(x, degree)
X_poly
= solve_normal_equation(X_poly, y)
coefficients
= predict(X_poly, coefficients)
y_pred
# Plot the results
=(12, 6))
plt.figure(figsize='gray', label='Actual')
plt.scatter(x, y, color='blue', label='Train Prediction')
plt.plot(x, y_pred, color'X')
plt.xlabel('y')
plt.ylabel(
plt.legend() plt.show()
Dataset 2 ( y = Cos(x/2) )
= np.arange(0,20,1)
x = np.cos(x/2)
y
='x', c='r', label="Actual Value")
plt.scatter(x, y, marker
plt.legend()"y=Cos(x/2)")
plt.title( plt.show()
Predictions for Different Degrees
Degree = 1
= 1 degree
= create_polynomial_features(x, degree)
X_poly
= solve_normal_equation(X_poly, y)
coefficients
= predict(X_poly, coefficients)
y_pred
# Plot the results
=(12, 6))
plt.figure(figsize='gray', label='Actual')
plt.scatter(x, y, color='blue', label='Train Prediction')
plt.plot(x, y_pred, color'X')
plt.xlabel('y')
plt.ylabel(
plt.legend() plt.show()
Degree = 4
= 4 degree
= create_polynomial_features(x, degree)
X_poly
= solve_normal_equation(X_poly, y)
coefficients
= predict(X_poly, coefficients)
y_pred
# Plot the results
=(12, 6))
plt.figure(figsize='gray', label='Actual')
plt.scatter(x, y, color='blue', label='Train Prediction')
plt.plot(x, y_pred, color'X')
plt.xlabel('y')
plt.ylabel(
plt.legend() plt.show()
Degree = 9
= 9 degree
= create_polynomial_features(x, degree)
X_poly
= solve_normal_equation(X_poly, y)
coefficients
= predict(X_poly, coefficients)
y_pred
# Plot the results
=(12, 6))
plt.figure(figsize='gray', label='Actual')
plt.scatter(x, y, color='blue', label='Train Prediction')
plt.plot(x, y_pred, color'X')
plt.xlabel('y')
plt.ylabel(
plt.legend() plt.show()