polynomial regression
scikit-learn
machine learning
data fitting
Python programming

How to fit a polynomial curve to data using scikit-learn?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Polynomial regression in scikit-learn is a standard approach when your relationship is nonlinear but still smooth enough to model with polynomial terms. The core idea is simple: transform the original input features into polynomial features, then fit a linear model on that transformed space. Despite the name, LinearRegression still works because the model remains linear in parameters.

The challenge is not writing the first working script; it is choosing degree, preventing overfitting, and building a pipeline that behaves well in training and inference. This guide walks through a production-friendly setup with validation and regularization options.

Core Sections

1. Build a polynomial pipeline correctly

Always package feature expansion and regression together in a Pipeline. This prevents train/test preprocessing mismatches.

python
1import numpy as np
2from sklearn.pipeline import Pipeline
3from sklearn.preprocessing import PolynomialFeatures, StandardScaler
4from sklearn.linear_model import LinearRegression
5
6# Example data
7X = np.array([[0.0], [1.0], [2.0], [3.0], [4.0], [5.0]])
8y = np.array([1.0, 2.2, 5.1, 10.3, 17.9, 27.8])
9
10model = Pipeline([
11    ("poly", PolynomialFeatures(degree=3, include_bias=False)),
12    ("scale", StandardScaler()),
13    ("reg", LinearRegression())
14])
15
16model.fit(X, y)
17pred = model.predict(np.array([[2.5], [6.0]]))
18print(pred)

include_bias=False is typically preferred because linear models already include an intercept by default.

2. Pick degree with cross-validation, not guesswork

Degree controls flexibility. Low degree may underfit; high degree can memorize noise. Use validation metrics to choose objectively.

python
1from sklearn.model_selection import KFold, cross_val_score
2
3degrees = [1, 2, 3, 4, 5, 6]
4cv = KFold(n_splits=5, shuffle=True, random_state=42)
5
6for d in degrees:
7    candidate = Pipeline([
8        ("poly", PolynomialFeatures(degree=d, include_bias=False)),
9        ("scale", StandardScaler()),
10        ("reg", LinearRegression())
11    ])
12    scores = cross_val_score(candidate, X, y, cv=cv, scoring="neg_mean_squared_error")
13    rmse = (-scores.mean()) ** 0.5
14    print(f"degree={d}, cv_rmse={rmse:.4f}")

Select the degree that performs best on validation, not the one that looks best on the training curve.

3. Add regularization for higher-degree stability

As degree increases, coefficients can become unstable. Replace plain linear regression with Ridge or Lasso.

python
1from sklearn.linear_model import Ridge
2
3ridge_model = Pipeline([
4    ("poly", PolynomialFeatures(degree=6, include_bias=False)),
5    ("scale", StandardScaler()),
6    ("reg", Ridge(alpha=1.0))
7])
8
9ridge_model.fit(X, y)

Regularization shrinks coefficients and often improves generalization, especially with noisy data or limited samples.

For multivariate features, polynomial expansion grows quickly. Track feature count with:

python
poly = PolynomialFeatures(degree=4, include_bias=False)
X_poly = poly.fit_transform(X)
print(X_poly.shape)

If dimensionality explodes, reduce degree or apply feature selection.

Common Pitfalls

  • Fitting polynomial features on full data before splitting, which leaks test information into training.
  • Choosing degree based only on training error, resulting in severe overfitting.
  • Skipping scaling when using regularized models, which biases penalty effects across differently scaled features.
  • Using very high degrees with limited data, leading to unstable coefficients and poor extrapolation.
  • Expecting polynomial models to extrapolate safely outside observed ranges; predictions can diverge rapidly.

Summary

In scikit-learn, polynomial curve fitting is best implemented with a pipeline that combines PolynomialFeatures and a regression estimator. Use cross-validation to choose degree, and add regularization when model complexity increases. With these controls in place, polynomial regression becomes a practical, interpretable baseline for nonlinear relationships.

A strong practice is to package model selection and diagnostics together. After choosing a degree, inspect residual plots and error distribution across input ranges. Polynomial models often fit the center of observed data well but behave poorly at boundaries. Visual checks can reveal this quickly and may suggest piecewise models or spline methods when one global polynomial is too rigid.

Also treat feature engineering and model degree as coupled decisions. If inputs are already engineered nonlinear transforms (for example log or interaction terms), a lower polynomial degree may generalize better than a high-degree expansion on raw inputs. Keep an eye on coefficient magnitudes and variance across folds; unstable coefficients are a signal to simplify features or increase regularization. Reliable polynomial regression is less about maximizing degree and more about balancing flexibility with robust out-of-sample behavior.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.