Polynomial Regression
Polynomial Regression — Fit curves by adding polynomial features.
What if your data curves? Polynomial regression bends the line into a curve by feeding the model powers of the input. But bend it too far and it starts chasing noise.
- Training data
- Polynomial fit
- True function
- Extrapolation zone
Polynomial controls
The idea in plain words
A straight line can only go straight. Polynomial regression bends it into a curve by feeding the model extra features — the input squared, cubed, and so on — then fitting a linear combination of them. The degree controls how many wiggles the curve is allowed.
Raise the degree and the curve threads the training points ever more tightly, but between and especially beyond them it can rocket to absurd values. That gap between fitting the data and fitting the world is overfitting, and it motivates the bias–variance tradeoff.
Now, the math
The model is linear in the coefficients but polynomial in the input:
Stacking the powers of each x into a design matrix, the least-squares fit is:
- the degree — the highest power of x, set by the slider.
- the coefficient on the j-th power of x.
- the Vandermonde design matrix of powers of each input.
▸ Show the derivation
The columns of powers of x become nearly parallel at high degree, so X⊤X is badly conditioned — the fit is computed on a normalized input to stay numerically stable. Even so, a degree-15 polynomial through 10 points interpolates every one exactly yet oscillates wildly in between, the Runge phenomenon you can trigger with the degree slider.
Trace it by hand
Four points that lie exactly on a parabola: (0, 1), (1, 3), (2, 7), (3, 13), generated by y equals 1 plus x plus x squared. We fit degree 1 and degree 2 with the same least-squares machinery — the repo's normal-equations solver — and let the numbers expose the difference.
Step 1 — the best straight line falls short
The line's residuals are plus 1, minus 1, minus 1, plus 1 — an up-down-down-up pattern no straight line can remove.
Step 2 — add an x-squared column to the design matrix
Each row of X holds one point's powers: 1, x, x squared. The model stays linear in the coefficients — only the features changed.
Step 3 — solve the normal equations
The solver returns 1.000, 1.000, 1.000 to three decimals — it recovered the exact parabola the data came from.
Step 4 — compare the errors
The degree-2 MSE prints as 3.5 times 10 to the minus 30 — zero up to floating-point dust. One extra feature removed all the error because the truth really was quadratic.
What just happened: The same normal equations fit both models; the extra x-squared column dropped the MSE from 1 to 0. The catch: with only 4 points a degree-3 fit would also reach zero training error, so a perfect fit alone never proves you found the true curve.
Now Break It
Try this: Degree 15 wiggles through every point perfectly on train data but oscillates wildly between them.
Control: Degree slider (set to maximum)
What happens: Overfitting! The high-degree curve threads every training point but wiggles wildly — it memorized noise.
Where polynomial regression is used
Polynomial regression extends the linear model by adding powers of the inputs, letting a single feature bend to capture curved trends while still using ordinary least squares under the hood. Physicists and engineers use it to fit calibration curves for sensors whose response is not perfectly linear, and chemists model reaction yield as a function of temperature where the relationship peaks and then falls. In economics, quadratic terms capture diminishing returns, such as how output rises with labor but eventually plateaus. Growth curves in biology, dose-response modeling in pharmacology, and trajectory smoothing in sports analytics all lean on polynomial terms. Because it reuses the linear regression machinery, polynomial regression is a quick, transparent way to add flexibility before reaching for more complex nonlinear models.
The biggest misconception is that a higher-degree polynomial is always better because it fits the training data more closely. In truth, high-degree polynomial regression overfits: it chases noise, wiggles wildly between points, and behaves erratically at the edges of the data, a problem known as Runge's phenomenon. The correction is to keep the degree low, validate on held-out data, or use splines that fit local pieces instead. A second pitfall is numerical instability, because raw powers of large inputs produce highly correlated, huge-magnitude columns; centering and scaling the features, or using orthogonal polynomials, keeps the fit well conditioned and the coefficients meaningful.
Frequently asked questions
What is polynomial regression?
Is polynomial regression still a linear model?
How do I choose the polynomial degree?
Why does a high-degree polynomial overfit?
When should I use splines instead of polynomial regression?
Written & reviewed by the ML Visualization team · Last updated .