Concepts / Feature Transformations: Binning, Polynomials, and Interactions

Feature Transformations: Binning, Polynomials, and Interactions

If the relationship between a feature and the target is curved, or depends on the combination of two features, the model cannot capture it unless someone builds that structure into the input.

  • Programming

Same Data, Different Results

Suppose you have already learned that binning, polynomial terms, and interactions can reshape a dataset in useful ways. The natural next question is whether you should apply these transformations to every model you train. The answer is no, and the reason reveals something important about how models differ. The same transformed dataset can produce a large improvement for one model and almost no change for another. Understanding which models are sensitive to these changes is the first step toward deciding when a transformation is worth the effort.

What do you think happens?

You transform the same dataset with binning and interaction columns, then train two models: a linear model and a tree-based model. Which model is more likely to show a large improvement?

  • The linear model

  • The tree-based model

  • Both will improve by exactly the same amount

Reveal answer
Answer:

The linear model

Binning, polynomials, and interactions have a large influence on linear models and naive Bayes models, which depend heavily on how the features are represented. Tree-based models can often discover important interactions themselves, so explicit transformations are usually not required for them.

How a Linear Model Sees a Feature

A linear model can only combine features in a straight-line fashion. If the relationship between a feature and the target is curved, or depends on the combination of two features, the model cannot capture it unless someone builds that structure into the input. Transformations are one way of doing exactly that. The model's own flexibility is limited, so the work of representing a richer relationship is handed to the features themselves.

Binning is one of the transformations that hands this work to the features. A continuous numeric feature is split into ranges, and each range becomes its own column. The original single value is replaced by several columns, and each column records whether the value falls inside its range. The diagram below shows one value being converted into bin indicator columns.

Worked example

Binning a single age value

A continuous feature called age is split into four bins: 0 to under 20, 20 to under 40, 40 to under 60, and 60 and over. Convert the age value 35 into indicator columns.

  1. 1
    Identify the bins

    There are four bins, so the single age feature becomes four new columns.

  2. 2
    Locate the value

    The value 35 is not less than 20 and is less than 40, so it falls in the 20 to under 40 bin.

  3. 3
    Set the indicator columns

    The 20 to under 40 column is set to 1, and the other three columns are set to 0.

  4. 4
    Read the result

    A linear model now assigns a separate weight to each bin. Moving from one bin to the next can change the prediction by a step, while values within a bin share the same contribution.

Result

The age value 35 becomes the indicator vector [0, 1, 0, 0].

This is why binning changes the shape of a linear model's prediction. Without bins, the model can only draw a straight line through the single numeric feature. With bins, the model's prediction can rise or fall in steps, because each bin contributes its own amount. The model is still linear in the new indicator columns, but the combined effect on the original feature is no longer a single straight line.

Polynomial Terms and Interaction Columns

Polynomial features add powers of an existing feature as new columns. A feature x can be supplemented with $x^{2}$ and $x^{3}$, which lets a linear model represent a curve, because the model still combines columns in a linear way while one of those columns bends with the data. Interaction features multiply two features together, creating a column such as $x_{1} \cdot x_{2}$. This column captures the case where the effect of one feature depends on the value of another, something a plain linear model cannot express from the raw features alone.

TransformationNew columns createdRelationship it lets a linear model represent
BinningOne 0/1 column per binStep-shaped response to one numeric feature
PolynomialPowers of an existing feature, such as x squared and x cubedCurved response to one feature
InteractionProduct of two features, such as x1 times x2Effect of one feature that depends on another

Each transformation gives a linear model a richer shape than the raw features allow.

Not every model benefits in the same way. Tree-based models stand in contrast to linear models. According to the source, they are often able to discover important interactions on their own, and most of the time they do not require the data to be transformed explicitly. A tree-based model splits the data in a sequence of decisions, and through those decisions it can pick up combinations of features without an explicit interaction column. For a learner choosing a model, this means that spending effort on manual interactions may give little return when the chosen model is tree-based.

Some model families sit between these two extremes. The source names SVMs, nearest neighbors, and neural networks. These models might sometimes benefit from binning, interactions, or polynomials, but the implications are usually much less clear than they are for linear models. You should not expect a predictable gain from transformations with these models. Instead, treat a transformation as an experiment whose result has to be measured on held-out data.

Why Extra Features Create Overfitting Risk

Once you know how many new features transformations can create, it is tempting to keep creating them until the dataset has far more columns than it started with. The source warns against this. Adding more features makes all models more complex, and that increases the chance of overfitting. Overfitting means the model learns patterns that belong to the training data specifically, so its performance on new data suffers. A model with many features has more ways to fit noise, which is why the number of features is a real design decision rather than something to maximize.

The source also points to a practical remedy. When you add new features, or whenever you work with a high-dimensional dataset, it can be a good idea to reduce the number of features so that only the most useful ones remain. Discarding the rest tends to produce simpler models, and simpler models often generalize better, meaning they perform more reliably on data they were not trained on.

Edge case

A transformation can help one model and hurt another. A set of bin columns that gives a linear model useful steps may add complexity that a tree-based model does not need. Always compare the transformed and untransformed versions on held-out data rather than assuming the new columns help.

Choosing Features by Three Strategies

The obvious follow-up question is how to tell which features are good. The source names three basic strategies, and each answers that question in a different way. All three are supervised methods. That means they use the target values, the thing the model is trying to predict, to decide which features to keep. The three strategies are univariate statistics, model-based selection, and iterative selection.

Univariate statistics judges each feature separately, using a statistic that relates that single feature to the target. It does not consider how a feature behaves alongside the others. Model-based selection instead fits a model and uses the importance that model assigns to each feature to decide which ones to keep. Iterative selection works step by step: it adds or removes one feature at a time, and each change is judged by the results it produces. Because it repeatedly changes the feature set, iterative selection generally involves fitting many more models than the other two strategies.

StrategyWhat it scoresHow many models it fits
Univariate statisticsEach feature alone, against the targetNo model is needed for the scoring step
Model-based selectionImportance assigned by a fitted modelOne fitted model is used to rank features
Iterative selectionFeature subsets, one change at a timeMany models are fitted in sequence

The three strategies trade simplicity of scoring against how much they account for feature interactions with the model.

Fitting Selection on Training Data Only

Because all three strategies use the target, there is a trap to avoid. If you select features using the whole dataset and then evaluate the model on a test set, the test set has already influenced which features were kept. The correct order is to split the data into training and test sets first, and then fit the feature selection only on the training part. The test portion then remains a fair check of how the selected features generalize.

The diagram shows where leakage would enter. If the selection step received the test rows, information about the held-out data would flow back into the choice of features. The evaluation would then be optimistic, because the features had been picked partly to suit the rows that were supposed to be unseen. Keeping the test part out of selection preserves its role as an honest check.

Mistakes Beginners Make With Transformations

  • Common mistake

    Applying binning or interactions to every model by default

    What it looks like

    Adding bins and interaction columns before training a tree-based model and expecting a large gain.

    Why it fails

    Tree-based models can often discover important interactions themselves, so explicit transformations are usually not required for them.

    Do this instead

    Start by checking whether the chosen model family is sensitive to representation, and measure any transformation on held-out data.

  • Common mistake

    Assuming more features always improve a model

    What it looks like

    Generating polynomial and interaction columns until the dataset has far more columns than it started with.

    Why it fails

    Adding features makes all models more complex, which increases the chance of overfitting.

    Do this instead

    Treat the number of features as a design decision and reduce the set to the most useful ones.

  • Common mistake

    Running feature selection on the whole dataset before splitting

    What it looks like

    Choosing the top features using every row, then splitting into train and test sets.

    Why it fails

    The test set has already influenced which features were kept, so the evaluation is no longer a fair check.

    Do this instead

    Split the data first and fit the feature selection only on the training part.

  • Common mistake

    Judging a transformation by training performance alone, which can hide overfitting

Trace the Pipeline on a Small Problem

MEDIUM

A teammate wants to add bins, polynomial terms, and interaction columns, then keep only the 10 best features by selecting on the full dataset. They will then split into train and test sets and report test accuracy. Identify the problem in this order of operations, and describe the order you would use instead.

Hints
  • Ask which rows the feature selection step is allowed to see.
  • Consider what the test set's role is in an honest evaluation.
  • Recall that all three selection strategies use the target values.
EASY

Name the transformation that lets a linear model represent a step-shaped response to one numeric feature, and name the one that lets it represent an effect that depends on another feature.

Hints
  • Think about which transformation replaces one value with several 0/1 columns.
  • Think about which transformation multiplies two features together.

Key Takeaways From the Transformation Process

  1. 1
    Binning, polynomial terms, and interactions have a large effect on linear models and naive Bayes models, because those models depend heavily on how the features are represented.
  2. 2
    Tree-based models can often discover important interactions themselves, while SVMs, nearest neighbors, and neural networks give less predictable gains from transformations.
  3. 3
    Adding features increases model complexity and the chance of overfitting, so reducing the feature set to the most useful ones can produce models that generalize better.
  4. 4
    Univariate statistics, model-based selection, and iterative selection are all supervised, so feature selection must be fit only on the training part of the data.

At a glance

  • Binning, polynomials, and interactions matter most for linear models and naive Bayes models, because these models depend on how the features are represented.
  • Tree-based models often discover interactions on their own, so explicit transformations usually add little for them.
  • More features raise model complexity and overfitting risk, so keep the most useful features and measure every transformation on held-out data.
  • Feature selection uses the target and must be fit only on the training part, so the test set remains a fair check.

Related concepts

More concepts starting with F