Polynomial Regression Calculator

Polynomial Regression Calculator y = c₀ + c₁x + c₂x² + …

Paste your data below (one point per line as x y or x,y) and pick a degree. You get the best-fit polynomial curve, R², a live plot of the fitted curve, and the coefficients — with the steps and the NumPy code.

Examples:

This free polynomial regression calculator fits a curved trend line through your data, draws the fitted curve on a live plot, and reports the polynomial equation, every coefficient, and R² — then shows the working and the NumPy code. Paste your points above, choose a degree, press Fit curve, and you see exactly how a bending curve, not a straight line, describes your data.

What polynomial regression actually does

Polynomial regression fits a curve of the form y = c₀ + c₁x + c₂x² + … + cdxd to a set of data points. It is a direct extension of linear regression: where a straight line can only capture a constant rate of change, a polynomial of degree d can bend, curve, and change direction up to d−1 times. That flexibility lets it model relationships that curve upward, level off, dip and rise again, or follow an S-shape — patterns a straight line simply cannot represent.

Despite the curve, polynomial regression is still a linear model in the statistical sense, because it is linear in its coefficients. That is the key insight that makes it solvable in one shot: even though the graph bends, we can treat each power of xx, , , and so on — as its own input feature, and then solve for the coefficients with ordinary least squares. The calculator above does exactly this, minimising the sum of squared vertical distances (the residuals, drawn as faint lines from each point to the curve) between your data and the fitted curve.

How to use the polynomial regression calculator

  1. Enter your data, one point per line, as x y or x,y. You need at least d+1 points with enough distinct x values for a degree-d fit.
  2. Choose the degree from the dropdown — 2 for a parabola, 3 for a cubic, up to 5. Changing the degree re-runs the fit instantly.
  3. Press Fit curve. The polynomial equation, each coefficient, and R² appear immediately, alongside a plot of the fitted curve through your points.
  4. Open Show the working to see the normal-equations method that produced the coefficients, and copy the ready-to-run NumPy code below.

Try the example presets — a clean cubic, an S-curve, a wave, and a random dataset — to build intuition for how degree and curve shape interact.

Choosing the right degree

The degree is the single most important choice in polynomial regression. A degree that is too low underfits: the curve is too stiff to follow the real pattern, and R² stays low. A degree that is too high overfits: the curve wiggles to pass through noise, chasing individual points instead of the underlying trend. A good rule of thumb is to pick the lowest degree that captures the genuine shape of the data. If your scatter looks like a single bend, degree 2 is usually enough; a single S-shape suggests degree 3. Raising the degree will always increase R² on the data you fit, but that does not mean the model is better — it may just be memorising noise.

Worked example

Take the five points (-2, -8), (-1, -1), (0, 0), (1, 1), (2, 8) — the default dataset. These come straight from y = x³. Fit a degree-3 polynomial and the calculator returns y = x³ with coefficients c₀ = 0, c₁ = 0, c₂ = 0, c₃ = 1 and R² = 1, a perfect fit, because the data is exactly cubic. Now switch the degree to 2: the parabola cannot capture the odd symmetry of a cubic, so R² drops sharply and the curve visibly misses the outer points. This contrast is the fastest way to feel what degree selection does.

The normal-equations and Vandermonde method

Here is how the fit is computed. For a degree-d polynomial we set up the normal equations: a (d+1)×(d+1) matrix M where M[i][j] = Σx(i+j), and a right-hand-side vector V where V[i] = Σ(xi·y), for i, j from 0 to d. Solving M·c = V gives the coefficient vector c. Equivalently, you can form the Vandermonde matrix A, whose columns are 1, x, x², …, xd for each data row, and solve the least-squares system AᵀA·c = Aᵀy — the two formulations are identical, since AᵀA = M and Aᵀy = V.

This calculator solves the system with Gaussian elimination and partial pivoting: at each step it swaps in the row with the largest pivot to keep the arithmetic numerically stable, eliminates below the pivot, and then back-substitutes to recover c₀ through cd. If the pivot magnitude collapses toward zero, it means there are not enough distinct x values to determine that many coefficients, and the tool reports an error instead of returning garbage. Fitted values are then evaluated with Horner's method, which nests the multiplications as ((cdx + cd-1)x + …)x + c₀ for speed and accuracy.

What R² tells you

The coefficient of determination R² measures how much of the variation in y the fitted curve explains. It is defined as R² = 1 − SSres/SStot, where SSres is the sum of squared residuals and SStot is the total variation around the mean of y. An R² near 1 means the curve tracks the data closely; a value near 0 means it barely beats predicting the average. Because R² never decreases when you add degree, it is a poor guide to model quality on its own — always read it alongside the plot and, ideally, a check on new data.

Higher degree is not always better
Every extra degree can only raise R² on your training points, so it is tempting to keep increasing it until the fit looks perfect. Resist that. A high-degree curve that threads every point will swing wildly between them and predict nonsense just outside your data range — classic overfitting. Prefer the simplest curve that captures the real shape, and be especially wary of degree 4 or 5 on small or noisy datasets.

Polynomial regression in Python with NumPy

In code, NumPy fits a polynomial in one line with polyfit, and poly1d turns the coefficients into a callable function:

import numpy as np
x = np.array([-2, -1, 0, 1, 2])
y = np.array([-8, -1, 0, 1, 8])
coef = np.polyfit(x, y, 3) # degree 3, highest power first
p = np.poly1d(coef)
print(p) # prints the polynomial
print(p(1.5)) # predict y at x = 1.5
# R-squared:
resid = y - p(x)
ss_res = np.sum(resid**2)
ss_tot = np.sum((y - y.mean())**2)
print(1 - ss_res / ss_tot)

Note that np.polyfit returns coefficients from the highest power down to the constant, the reverse of the c₀..cd order this calculator lists. The fitted curve and R² are otherwise identical, so you can prototype a degree here and reproduce it in code.

When to use polynomial regression

Reach for polynomial regression when a scatter plot shows a clear, smooth curve rather than a straight line — growth that accelerates, a response that peaks and falls, or a gentle S-shape. It is ideal for physical relationships (trajectories, dose-response curves), for smoothing a trend over a limited range, and for quick feature engineering. It is a poor choice when the data has sharp kinks, many turning points, or when you need to extrapolate far beyond the observed x range, where polynomials become unstable. For those cases, splines, local regression, or a nonlinear model are better tools.

Where it shows up in machine learning

Polynomial regression is the textbook illustration of two central ML ideas. First, feature expansion: by transforming a single input x into the features [x, x², x³, …], a linear model gains the power to fit nonlinear curves — the same trick that kernels use in support vector machines. Second, the bias-variance tradeoff: low-degree fits have high bias (too rigid), high-degree fits have high variance (too sensitive to noise), and the art is finding the sweet spot. Tools like PolynomialFeatures in scikit-learn make this explicit, letting you expand features and then regularise to control the variance that high degrees introduce.

Frequently asked questions

How many data points do I need?

At least d+1 points with that many distinct x values to fit a degree-d polynomial — four for a cubic, for example. More points give a more reliable fit and a more trustworthy R²; fitting a high degree to just enough points guarantees a perfect-looking but meaningless curve.

Why did I get a "not enough distinct x values" error?

A degree-d polynomial has d+1 unknown coefficients, and you need at least that many different x values to pin them down. If several points share the same x, or you asked for a degree too high for your data, the normal-equations matrix becomes singular and cannot be solved. Lower the degree or add points at new x values.

What is the difference between this and quadratic regression?

Quadratic regression is just polynomial regression fixed at degree 2, fitting a single parabola. This tool generalises it to degrees 2 through 5, so you can choose how much the curve is allowed to bend.

Does the result match NumPy?

Yes. The coefficients and R² match numpy.polyfit and numpy.poly1d for the same degree, because they solve the same least-squares normal equations. The only difference is coefficient ordering: NumPy lists highest power first, this calculator lists c₀ first.

Is the calculator free and private?

Completely free, no sign-up, and it runs entirely in your browser. Your data, the curve, and the coefficients never leave your device.

Related calculators

Compare fits with the general regression calculator, start simpler with the linear regression calculator, or fit a single parabola with the quadratic regression calculator.