Concepts / Polynomial Features and Model Complexity

Polynomial Features and Model Complexity

Interaction and polynomial features can give a linear model like Ridge a clear performance boost, as the source reports for this comparison.

  • Programming

One Change, Two Opposite Results

Imagine you take the same dataset and make one change to its inputs: you add interaction and polynomial columns built from the original features. You then fit two models on the two versions of the data. One model is Ridge, a linear model. The other is a Random Forest. Before you read on, decide what you expect to happen to each model's test score.

What do you think happens?

After adding interaction and polynomial features, what happens to the Ridge test score and the Random Forest test score?

  • Both scores go up

  • Both scores go down

  • Ridge goes up and the Random Forest goes slightly down

  • Ridge goes down and the Random Forest goes up

Reveal answer
Answer:

Ridge goes up and the Random Forest goes slightly down.

The source reports that the added interaction and polynomial terms raise Ridge's performance, while the Random Forest's test score drops slightly. The Random Forest already beat Ridge on the original features, so the change did not make it the better model; it made its score a little worse.

If your prediction was that every model improves when you add features, you are not alone. That expectation is common, and this comparison is built to test it. The point of the exercise is not that feature engineering is harmful. It is that a feature engineering step is not a universal upgrade, and its effect depends on the model that consumes the features.

What Changes in the Feature Matrix

Each row of a feature matrix is one example, and each column is one input the model sees. Interaction features and polynomial features add new columns that are computed from the existing ones. Take a simple case with two original features, $x_1$ and $x_2$. Adding an interaction term contributes the product $x_1 \cdot x_2$. Adding polynomial terms contributes squared versions such as $x_1^{2}$ and $x_2^{2}$. The original columns stay in place; the new columns sit beside them.

x1original featurex1original featurex2original featurex2original featurex1 times x2interaction from x1 and x2x1 squaredpolynomial from x1x2 squaredpolynomial from x2keptkept
What changes when interaction and polynomial terms are added to two original features? The original columns remain, and new columns are computed from them.

Read the diagram column by column. The two original columns survive unchanged. The three new columns each come from a specific pair or single original feature: the interaction column comes from both $x_1$ and $x_2$ together, while the squared columns each come from one feature alone. The model now receives five inputs where it previously received two, and the scoring and training steps still work on this wider matrix in exactly the same way.

Why a Linear Model Gains From Products

A linear model like Ridge makes its prediction by adding up its inputs, each multiplied by a weight. On its own, it can only combine features additively. It cannot, by itself, represent the idea that the effect of $x_1$ depends on the value of $x_2$. An interaction column gives it exactly that ability: the product $x_1 \cdot x_2$ is a new input whose weight can capture a joint effect. Squared columns play a similar role by giving the linear model a curved term to work with.

The key idea is that the added columns supply a new kind of signal. The linear model was not able to express that signal from the original features, so Ridge's score can rise when the expanded columns are included.

Why a Random Forest Can Lose Ground

A Random Forest does not combine features additively. It builds its predictions from many trees, and each tree splits the data by asking questions about individual features. The source notes that the Random Forest already beat Ridge on the original features before any expansion. When the expanded set is added, the forest has more candidate splits to consider, and the same underlying signal can become harder to use well on this particular data. The result in this comparison is a slight decrease, not a collapse.

Ridgelinear modeladds inputs withweightscombines featuresadditivelyproduct columnsnew kind of signalRandom Forestmany treessplits on singlefeaturestree questionswider candidate setsame signal harder to use
Why does one identical change to the inputs improve a linear model while reducing a Random Forest's score? Each model consumes the features in a different way.

Tracing the Comparison Workflow

Before reasoning about why a model behaves the way it does, start with the measurement. The comparison has four stages: preparing the data, expanding the features, fitting the model, and scoring it on the matching test set. The data is scaled before the fit, and the expanded training and test arrays are created from the same original features. Each fit is scored on the test set that matches its own feature set.

Data prepscale original featuresFeature expansionadd interaction andpolynomial columnsFittingsame model class andsettingsScoringunexpected score appearshere
At which step of the comparison pipeline does the unexpected score first show up? The fitting step runs normally; the first visible surprise is in the scores.

Work through the flowchart step by step. Data preparation and feature expansion are where the two runs begin to differ, but neither step produces a number to be surprised by. Fitting uses the same model class, the same number of trees, and the same scoring method in both runs, so it is not the source of the change. The unexpected number first appears when each fitted model is scored on its matching test set. That is the step where a learner who expected every model to improve first sees the Random Forest result as a failure.

Reading the Two Scores

Once both fits are scored, you have two numbers for the same model type: one for the original features and one for the expanded features. The score belongs to the feature set used in that run, so label each one before you compare them. The difference between them is the effect of the added features, and only that effect, because nothing else changed between the runs. Its sign tells you the direction, and its magnitude tells you the size.

QuantityWhat it refers toHow to read it
Score without interactionsTest score when the model is fit on the original scaled featuresThe baseline for this model
Score with interactionsTest score when the model is fit on the expanded featuresThe result after the added columns
DifferenceScore with interactions minus score withoutPositive means the features helped; negative means they hurt
Size of the differenceThe absolute value of the differenceA small value means a small effect that should not be generalized

Reading the two scores from a with-versus-without interaction features comparison

Worked example

Interpreting a Random Forest result

A Random Forest is scored once on the original features and once on the expanded features, using matching test sets. The expanded score is lower. What can you conclude, and what should you avoid concluding?

  1. 1
    Label each score

    Attach each test score to its feature set. The lower number belongs to the expanded features.

  2. 2
    Check what changed

    Confirm the model class, tree count, and scoring method were the same. In a controlled comparison, only the features differ.

  3. 3
    Read the direction

    The expanded score is lower, so for this model on this data the added columns did not help.

  4. 4
    Judge the size

    If the drop is small, report it as small. Do not claim that interaction features are harmful in general.

Result

The added features slightly lowered this Random Forest's test score on this data. The conclusion is limited to this model and dataset.

Log, Exp, and Sin on Single Features

Squared and cubed features are one way to give a linear model curved behavior. A second group of tools applies a single mathematical function to one feature at a time. The source names three: log, exp, and sin. Linear models and neural networks are closely tied to the scale and distribution of each feature, so when the relation between a feature and the target is nonlinear, that relation is hard for them to model, especially in regression. A transformation can reshape the feature so the pattern becomes easier to fit.

FunctionWhat it does to a featurePattern it suits
logAdjusts relative scales in the dataRelationships where relative size matters more than absolute size
expAdjusts relative scales in the other directionRelationships that grow or shrink in a multiplicative way
sin and cosMap values onto repeating cyclesPeriodic patterns, such as cycles that repeat over time

Single-feature transformations and the patterns they help linear models and neural networks capture

The source points to the memory price data from Chapter 2 as an earlier application of the log and exp idea. Tree-based models only depend on the ordering of feature values, so they are much less affected by how a feature is scaled or distributed. This is one reason the same transformation that helps a neural network can make little difference to a Random Forest.

Mistakes Beginners Make With Feature Comparisons

  • Common mistake

    Assuming every added feature improves every model

    What it looks like

    Expecting the Random Forest score to rise because Ridge's score rose

    Why it fails

    The effect of a feature set depends on the model that consumes it. A linear model combines features additively, while a Random Forest splits on individual features.

    Do this instead

    Test each model separately and read each result on its own terms.

  • Common mistake

    Changing more than one thing between the two runs

    What it looks like

    Changing the features and the number of trees, then attributing the score change to the features

    Why it fails

    If the model class, tree count, or scoring method also changes, the score change cannot be attributed to the features alone.

    Do this instead

    Keep the model settings and scoring method identical; change only the feature matrices.

  • Common mistake

    Overreading a small score difference

    What it looks like

    Concluding that interaction features are harmful after a slight drop

    Why it fails

    A small drop shows only that the extra columns did not help this model on this data.

    Do this instead

    Report both the direction and the size of the change, and limit the claim to the case you tested.

  • Common mistake

    Mixing up which score belongs to which feature set

    What it looks like

    Comparing two numbers without recording which run produced each one

    Why it fails

    The difference only means something when each score is tied to its feature set.

    Do this instead

    Label each score with its feature set before computing the difference.

Checking Your Understanding

MEDIUM

A colleague adds interaction features to a dataset, fits Ridge and a Random Forest on the original and expanded versions, and reports that the Random Forest got worse, so interaction features are bad. Using the comparison workflow, explain what they should check before accepting this conclusion, and say which step of the pipeline the unexpected number first appears in.

Hints
  • Ask whether the two runs differed in anything other than the feature matrices.
  • Ask how large the drop was, and whether it generalizes beyond this model and dataset.
  • Trace the pipeline from data preparation through scoring, and identify where the score first exists.

Key Takeaways

  • Interaction and polynomial features add new columns computed from the original features, such as the product of two features and squared terms.
  • The same added features can raise a linear model's score, as with Ridge, while slightly lowering a Random Forest's test score, because a feature step's effect depends on the model that uses the features.
  • A controlled comparison changes only the feature matrices, so the score difference can be attributed to the features; always report its direction and size.
  • The unexpected result first appears at the scoring step, when each fitted model is scored on its matching test set; the surprise reflects an expectation mismatch, not an arithmetic bug.
  • Single-feature transformations such as log and exp can help linear models and neural networks by adjusting relative scales, and sin and cos suit periodic patterns, while tree-based models are much less affected.

Related concepts

More concepts starting with P