Concepts / Enhancing Linear Models with Interactions and Polynomials

Enhancing Linear Models with Interactions and Polynomials

Polynomial regression is linear regression applied to polynomial features; the fitting method is unchanged and the inputs are richer.

  • Programming

When a Straight Line Cannot Follow the Data

Suppose a straight line is a poor description of your data. The points curve, rise, and flatten, and no single straight line passes near them. A plain linear regression model can only draw a straight line through the input, so its predictions will miss the shape of the data no matter how carefully it is fitted. Polynomial regression is one way to keep the linear model machinery while letting the prediction bend.

The idea is easy to state. Polynomial regression is linear regression applied to polynomial features. The fitting method does not change. What changes is the input the model receives. This article follows one one-dimensional example from raw input to curve, and it asks at each step whether the fitted curve still matches what you expected. Finding the step where that match breaks is the main skill this article builds.

Tracing One Input Through the Pipeline

A polynomial regression pipeline has two lanes that meet at the prediction. The training lane takes the raw input, passes it through a polynomial transformer to produce a feature matrix, and then fits the linear model to that matrix together with the target. The drawing lane takes a dense grid of input values, passes it through the same transformer, and then applies the fitted weights to produce the curve.

Read the diagram from left to right in each lane. The two lanes must use the same transformer and the same column layout. If they do not, the weights learned from the training columns will be applied to the wrong columns in the drawing lane, and the curve will be wrong even though the fit itself succeeded.

python
Notes on the code
  • poly is the transformer; it builds the higher-power columns
  • fit_transform learns the column layout from the training inputs and produces X_poly
  • reg is the linear model; it only ever sees the columns in X_poly
  • line is a dense grid used only for drawing the curve
  • poly.transform reuses the layout learned during fitting, so the columns match the weights

How One Column Becomes Several

A polynomial feature is a transformed version of the original input. Instead of giving the linear model only the raw value of the input feature, the transformer builds new columns from that value, using higher powers of it up to a chosen degree. The source example uses polynomial features of degree ten, meaning the highest power in the new columns is ten.

The diagram shows one row of expanded columns for one input value. The first column holds the constant 1, the second holds the raw value, the third holds its square, and the fourth holds its cube. The linear model then fits one weight to each column. The prediction for a row is the weighted sum of these columns, so the model is still linear in its weights even though the output traces a curve in the original input.

The linear regression model itself never sees a curve. It sees a table of numbers and fits weights to that table. The bending is produced entirely by the feature columns that were prepared before fitting. This separation is the central idea of the method, and most debugging later depends on keeping the two steps distinct: the transformer decides what columns exist, and the linear model decides how much each column counts.

A linear regression model and a polynomial regression model share the same fitting method. They differ in what the model receives as input and, as a result, in the shape of the prediction they can represent. The left path in a comparison skips feature preparation, so the prediction is limited to what a straight line can express. The right path inserts polynomial features before fitting. The fitting step is identical, so polynomial regression is not a different learning algorithm. It is the same linear model given a richer set of inputs.

Worked example

Expanding one input value by hand

Suppose the raw input for one row is x = 2 and the polynomial degree is 3. What row of features does the transformer produce, and what prediction do the weights give?

  1. 1
    Build the columns

    With degree 3, the columns are 1, x, x squared, and x cubed. For x = 2 this gives the row 1, 2, 4, 8.

  2. 2
    Pair each column with a weight

    The fitted model holds weights w0, w1, w2, and w3, one for each column. The weights are learned during fitting, not chosen by hand.

  3. 3
    Compute the weighted sum

    The prediction is w0 times 1 plus w1 times 2 plus w2 times 4 plus w3 times 8. This is a linear combination of the columns, even though the input x entered the columns non-linearly.

Result

The row 1, 2, 4, 8 is the model input, and the prediction is a weighted sum of those four numbers. Changing any weight changes the shape of the curve over the whole input range.

Reaching Complexity Without Explicit Features

The source compares polynomial regression with a kernel SVM learned on the original data, with no explicit transformation of the features. A kernel SVM uses a kernel to learn a prediction that is similarly complex to the polynomial regression result. The contrast is in where the higher-order behavior is computed. The polynomial version builds its expanded columns explicitly, before fitting. The kernel SVM reaches a comparable complexity without that explicit step.

The kernel SVM is a more complex model than the linear regression used here, and the source presents it as an alternative to compare against rather than a replacement for understanding polynomial features. The source's comparison varies the gamma parameter of an SVM with an RBF kernel, which is where the kernel SVM's behavior is tuned. Understanding the explicit polynomial pipeline first makes the kernel version easier to reason about, because you can ask the same question of both: where is the bending produced, and which setting controls how much of it appears?

Watching the Curve Swing at the Edges

On one-dimensional data, polynomial features give a very smooth fit. The curve does not zigzag from point to point the way a flexible model might, and that smoothness is part of the appeal of the method. The smoothness does not hold everywhere, however. High-degree polynomials can behave extremely at boundaries and in sparse regions, where there are few training points to constrain them.

What do you think happens?

A degree-10 polynomial regression is fitted to points that lie inside the range from 0 to 1. You then predict at an input far outside that range. Will the prediction be reliable?

  • Yes, the smooth fit continues in a sensible way

  • No, high-degree polynomials can swing wildly just outside the training range

Reveal answer
Answer:

No, high-degree polynomials can swing wildly just outside the training range.

The smooth fit is a property of the region where the points are dense. Outside that region the highest powers dominate the weighted sum, so the curve can change direction or grow sharply. Boundaries and sparse regions are where a polynomial fit most often misleads.

This is why a prediction that looks correct in the middle of the plot can be wrong at its ends. When you inspect a polynomial regression result, check the boundaries and sparse regions first, not only the area where the points are densest. A curve that matches the data well in the center can still be unreliable where you have no evidence.

Mistakes Beginners Make with the Pipeline

Most polynomial regression bugs show up as a prediction that disagrees with what you expected. The useful habit is to locate the step where the disagreement begins, rather than rewriting the whole pipeline. The pipeline has three checkpoints: the features the transformer produced, the model fitted on them, and the features passed in at prediction time.

  • Common mistake

    Transforming the drawing grid with a new transformer instead of the one used for training

    What it looks like

    The training data is transformed by poly, but the grid is passed through a second, freshly built transformer.

    Why it fails

    The column layout is not guaranteed to match the one the weights were fitted on, so the weights multiply the wrong columns.

    Do this instead

    Pass both the training data and the drawing grid through the same fitted transformer, as the source's two-lane pipeline requires.

  • Common mistake

    Trusting the curve only where the points are densest

    What it looks like

    The curve looks clean across the middle of the training data, so the prediction at the far right of the plot is assumed to be fine.

    Why it fails

    High-degree polynomials can behave extremely at boundaries and in sparse regions, which the middle of the plot does not reveal.

    Do this instead

    Inspect the boundaries and sparse regions first when reviewing a polynomial regression result.

  • Common mistake

    Assuming polynomial regression is a different learning algorithm

    Why it fails

    The fitting method is unchanged. Only the inputs are richer.

    Do this instead

    Think of polynomial regression as the same linear model given polynomial feature columns.

  • Common mistake

    Rewriting the whole pipeline when a prediction looks wrong

    Why it fails

    This hides the step that actually caused the disagreement.

    Do this instead

    Check the three checkpoints in order: the transformed features, the fitted model, and the features passed in at prediction time.

Checking Your Understanding of the Pipeline

MEDIUM

A colleague builds a polynomial regression with degree 10 on a one-dimensional dataset. The middle of the curve matches the points well, but predictions near the largest input value look far off. Using the three checkpoints, describe which step you would inspect first and why.

Hints
  • Start with the features the transformer produced for the training data.
  • Consider whether the far-right region is a boundary or a sparse region relative to the training points.
  • Ask whether the grid used for prediction went through the same transformer as the training data.

Key Takeaways

  • Polynomial regression is linear regression applied to polynomial features. The fitting method is unchanged; the inputs are richer.
  • The transformer creates columns from powers of the input up to a chosen degree, and the linear model fits one weight per column, so the bending comes from the features rather than the model.
  • Training data and the drawing grid must pass through the same transformer so that the fitted weights match the columns they are applied to.
  • High-degree polynomials can look smooth in the middle of the data but behave extremely at boundaries and in sparse regions, so check those areas first.
  • A kernel SVM can learn a similarly complex prediction without an explicit feature transformation, with gamma as one tuning parameter in the source's comparison.

Related concepts

More concepts starting with E