Polynomial Regression
Kezin B Wilson
Co-founder, Electronics Innovator , Researcher , Embedded & Robotics Engineer
What is Polynomial Regression?
Polynomial regression models the relationship between the dependent variable (y) and the independent variable (x) as an nth degree polynomial:
Where:
This allows for the modeling of curves, making it suitable for non-linear data.
Why Use Polynomial Regression?
While linear regression works well for linear relationships, real-world data often exhibits non-linear patterns. Polynomial regression can capture these patterns, making it ideal for:
When to Use Polynomial Regression
However, avoid overfitting by using excessively high-degree polynomials.
领英推荐
Steps in Polynomial Regression
1. Data Preprocessing
2. Fit a Model
3. Evaluate the Model
Hands-On Mini-Challenge: Fitting a Polynomial Curve
Let’s fit a polynomial regression model using synthetic data for better understanding.
Starter Code:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
# Generate synthetic non-linear data
np.random.seed(42)
X = np.random.rand(100, 1) * 10 # Independent variable
y = 2 + 1.5 * X + 0.5 * X**2 + np.random.randn(100, 1) * 5 # Target with quadratic relationship
# Transform to polynomial features
poly = PolynomialFeatures(degree=2) # Change degree to experiment
X_poly = poly.fit_transform(X)
# Train the model
model = LinearRegression()
model.fit(X_poly, y)
# Make predictions
y_pred = model.predict(X_poly)
# Evaluate the model
print("MSE:", mean_squared_error(y, y_pred))
print("R-squared:", r2_score(y, y_pred))
# Visualize the result
plt.scatter(X, y, color='blue', label='Data')
plt.plot(X, y_pred, color='red', label='Polynomial Fit')
plt.xlabel("X")
plt.ylabel("y")
plt.legend()
plt.title("Polynomial Regression Fit")
plt.show()
Key Considerations in Polynomial Regression
Best Practices for Polynomial Regression