Skip to content
AI360XpertBeta
Core ML

Visual explainer

Polynomial Regression

Follow three samples through squared features, an exact quadratic fit, alternative interpolants and fresh-point checks, then expose a failure beyond training.

Polynomial regression adds powers of the input as features, letting a model linear in its coefficients draw a curved prediction. Its degree controls the available shapes. A perfect fit to observed samples does not establish either good interpolation between them or a correct continuation beyond them.

The least-squares line misses three quadratic samples.
The least-squares line misses three quadratic samples.

Use three synthetic training samples throughout the main example: (1,1), (2,4), (3,9). Their responses follow y = x². The best straight line with an intercept is ŷ = 4x − 10/3. It predicts 2/3, 14/3 and 26/3, leaving vertical residuals y − ŷ of 1/3, −2/3 and 1/3. Training mean squared error is:

MSE⁡=(1/3)2+(−2/3)2+(1/3)23=29.\operatorname{MSE}=\frac{(1/3)^2+(-2/3)^2+(1/3)^2}{3}=\frac29.

The scene uses 180 SVG units per input unit and 24 per response unit. The middle residual caliper is offset horizontally for visibility; its height measures the middle sample's vertical error. These three points establish that this line misses their curvature. In real data, a residual pattern is diagnostic evidence to investigate, not proof of one unique cause.

Expand The Input

A squared feature turns input 2 into 1, 2 and 4.
A squared feature turns input 2 into 1, 2 and 4.

At x = 2, the feature row [1,x,x²] is [1,2,4]. The three bars show these values on a shared height scale of 40 SVG units per feature value. The connecting branches show computation, not numerical magnitude. The constant feature lets the fit include an intercept.

The prediction is ŷ = w₀ + w₁x + w₂x². The input has undergone a nonlinear transformation, but the coefficients still enter as a weighted sum. PolynomialFeatures documentation describes this basis expansion. Multiple original inputs can also add interaction terms; this page keeps one input so every operation remains visible.

Fit The Quadratic

A quadratic fits the three training samples exactly.
A quadratic fits the three training samples exactly.

On the original plot scales, the quadratic ŷ = x² passes through every training ring. The design matrix has rows [1,1,1], [1,2,4], [1,3,9]. Its determinant is 2, so these three distinct input positions identify one polynomial of degree at most two. Solving gives (w₀,w₁,w₂) = (0,0,1), with zero training MSE.

A linear least-squares solver can therefore produce a curved function of x. Numerically, solve the least-squares problem using an appropriate factorization rather than assuming the normal matrix is always invertible. NumPy's polynomial fitting reference describes the Vandermonde least-squares system, singular-value solution and rank warnings.

More Terms Leave Freedom

An extra cubic term can change the curve without changing these training predictions.
An extra cubic term can change the curve without changing these training predictions.

The dashed quadratic and solid cubic agree at all three training circles, on the same scales. Define a whole family of interpolants:

qa(x)=x2+a(x−1)(x−2)(x−3).q_a(x)=x^2+a(x-1)(x-2)(x-3).

The added term is zero at every observed input. Choose a = 4 for the pictured example, giving q₄(x) = 4x³ − 23x² + 44x − 24. Between the training inputs, that term changes the prediction while leaving training error zero. The drawn cubic is calculated from this polynomial, including its endpoint derivatives; it is not a decorative wavy path.

Three rows cannot uniquely determine four independent cubic coefficients. This is one explicitly chosen zero-error cubic, not a claim that a particular least-squares implementation must select it. More generally, extra capacity needs enough informative observations or suitable constraints. High degree does not invariably fail, and this example is not a general demonstration of Runge's phenomenon.

Observe Between Training Inputs

Fresh midpoint observations distinguish two perfect training fits.
Fresh midpoint observations distinguish two perfect training fits.

The diamonds add fresh validation observations at x = 1.5 and 2.5, with responses 2.25 and 6.25 from the same synthetic x² rule. The circles remain the original training points. The solid curve is the chosen cubic q₄, predicting 3.75 and 4.75. Its vertical residuals have magnitude 1.5 at both new locations, shown by 36-unit stems on the unchanged response scale.

The quadratic predicts both fresh responses exactly. Training fit alone could not distinguish these curves; the new observations do. Two noiseless validation points are useful for arithmetic, but too small a sample to claim robust real-world model selection.

Compare Candidate Predictions

Validation selects among the stated candidate curves.
Validation selects among the stated candidate curves.

Compare the specific line, quadratic and chosen cubic on those same two validation observations. The line predicts 8/3 and 20/3, overpredicting both responses by 5/12. Their mean squared errors are:

MSE⁡line=25144≈0.173611,MSE⁡quadratic=0,MSE⁡q4=94=2.25.\operatorname{MSE}_{\text{line}}=\frac{25}{144}\approx0.173611,\qquad \operatorname{MSE}_{\text{quadratic}}=0,\qquad \operatorname{MSE}_{q_4}=\frac94=2.25.

The bars share a scale of 80 SVG units per MSE unit; the mint ring marks exactly zero. These are reproducible fixture results, not general performance numbers for degrees one, two and three. The chosen cubic is underdetermined from the original training data unless its selection rule is specified. In practice, fit each candidate on training data, compare with representative validation data or cross-validation, and reserve a separate test set for final assessment.

Perturb One Observation

A small training-target change can grow outside the observed range.
A small training-target change can grow outside the observed range.

Keep x = 1,2,3, but change only the middle response from 4 to 4.2. The newly interpolated quadratic is:

p~(x)=x2−0.2(x−1)(x−3).\tilde p(x)=x^2-0.2(x-1)(x-3).

This plot shows the change in prediction, Δ(x) = p̃(x) − x², rather than either absolute response. Its vertical scale is 200 SVG units per response-change unit, with zero at the horizontal axis. At x = 2 the change is +0.2; at the training boundary x = 3 it returns to zero; at x = 4 it has become −0.6. The dashed vertical marks the largest training input, not a discontinuity in the curve.

The exact-fit procedure reproduces the changed observation, and its effect grows outside the data support. This sensitivity already occurs for this quadratic; it is distinct from floating-point numerical error and does not require a high-degree polynomial.

Change The Basis

Centering changes the polynomial basis while preserving this fitted curve.
Centering changes the polynomial basis while preserving this fitted curve.

Restore the original responses and define t = x − 2 using the training-input center. The raw squared column at x = 1,2,3 is [1,4,9]; the centered squared column at t = −1,0,1 is [1,0,1]. Both sets of bars use 18 SVG units per feature value. Labels underneath identify x or t; bar height represents its square.

The identical fitted curve can now be written as:

x2=(t+2)2=4+4t+t2.x^2=(t+2)^2=4+4t+t^2.

Coefficients change from [0,0,1] in the raw basis to [4,4,1] in the centered basis, while predictions remain unchanged. Centering and scaling can reduce large power magnitudes; they do not guarantee a well-conditioned problem or fix insufficient data. Coefficient penalties also depend on the chosen basis. Reuse training-derived transformations on fresh inputs. Higher degrees or wider ranges can favor other polynomial bases or local spline models; the interpolation example compares their behavior.

View The Squared Coordinate

The quadratic is a straight line in its squared feature coordinate.
The quadratic is a straight line in its squared feature coordinate.

Keep the original three responses and replace the horizontal coordinate x by u = x². The horizontal positions become 1,4,9 instead of 1,2,3. The response scale remains 24 SVG units per response unit; the new horizontal scale is 45 SVG units per u unit. The three points now lie on the straight line y = u.

This is the same prediction rule viewed in a feature coordinate. Polynomial regression is linear in the expanded features and coefficients even when curved in the original input. Squaring alone loses the sign of x; our inputs are positive, and the general polynomial feature row also retains x when odd effects matter. Choosing only a squared feature would restrict the model family further.

Where It Breaks

A perfect in-range fit can miss a different continuation beyond training.
A perfect in-range fit can miss a different continuation beyond training.

A different possible data-generating rule agrees with the quadratic throughout the observed interval [1,3], but continues as 3x for x > 3. Both rules meet at (3,9). This is an intentionally synthetic, continuous continuation with a changed slope, not a measured physical law. At x = 4 it produces 12 while the fitted quadratic predicts 16.

The final plot changes scales to fit the extrapolation: 120 SVG units per input unit and 12 per response unit. The amber cross is the model's 16; the white ring is the possible observed 12. Their caliper spans exactly four response units. The dashed boundary marks the end of training support.

The original samples and even perfect validation inside their interval cannot rule out this alternative continuation. A polynomial can extrapolate correctly when the relevant global relationship is justified, but a good fit by itself supplies no such justification. Match the evaluation domain to intended use and add domain constraints or further observations where the continuation matters. Simply clamping an input would change the prediction rule without establishing its correctness.

The Quick Version

  • A straight line leaves residuals on the quadratic fixture.
  • Powers of inputs create additional feature columns.
  • A linear solver can fit a curve in original coordinates.
  • Extra coefficients can leave multiple perfect interpolants.
  • Fresh observations distinguish identical training scores.
  • Validation compares specified fitted models, not degree labels alone.
  • A training perturbation can amplify beyond observed inputs.
  • Basis changes alter coefficients while preserving the fitted curve.
  • Squared-feature coordinates can turn a parabola into a line.
  • Correct extrapolation needs evidence beyond an in-range fit.