Polynomial Regression in Python: What It Is and How to Use It
x and output y is modeled as an n-degree polynomial. Using sklearn, you transform features to polynomial features and then fit a linear model to capture curves in data.How It Works
Polynomial regression works by extending simple linear regression to fit curved lines instead of just straight lines. Imagine you want to predict something that doesn’t follow a straight path, like the growth of a plant over time that speeds up or slows down. Instead of drawing a straight line, polynomial regression fits a curve by adding powers of the input features (like x², x³, etc.).
In Python, this is done by first transforming the original input data into new features that include these powers. Then, a regular linear regression model is trained on these new features. This way, the model can learn more complex patterns while still using simple math behind the scenes.
Example
This example shows how to use polynomial regression with sklearn. We create some data that follows a curve, transform it to polynomial features, and fit a linear model to it.
import numpy as np from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures from sklearn.metrics import mean_squared_error, r2_score # Create sample data x = np.array([1, 2, 3, 4, 5]).reshape(-1, 1) y = np.array([1, 4, 9, 16, 25]) # y = x^2 # Transform features to polynomial features (degree 2) poly = PolynomialFeatures(degree=2) x_poly = poly.fit_transform(x) # Fit linear regression on polynomial features model = LinearRegression() model.fit(x_poly, y) # Predict y_pred = model.predict(x_poly) # Print results print(f"Coefficients: {model.coef_}") print(f"Intercept: {model.intercept_}") print(f"Mean Squared Error: {mean_squared_error(y, y_pred):.2f}") print(f"R2 Score: {r2_score(y, y_pred):.2f}")
When to Use
Use polynomial regression when your data shows a curved relationship between the input and output, and a straight line won’t fit well. For example, it’s useful in predicting growth rates, trends in sales over time, or any situation where the effect of the input changes at different levels.
It’s important to choose the right degree for the polynomial: too low might miss the curve, too high might overfit and capture noise instead of the true pattern.
Key Points
- Polynomial regression fits curves by adding powers of input features.
- It uses linear regression on transformed polynomial features.
- Choosing the right polynomial degree is key to good predictions.
- It helps model non-linear relationships simply and effectively.