Concepts / Choosing Features for a Learning Method

Choosing Features for a Learning Method

Flexible nonlinear models such as random forests and SVMs may learn complex tasks without explicitly expanding the feature space, so the best choice depends on the method.

  • Programming

Same Facts, Different Usability

Imagine two people given the same spreadsheet. One sees a column of city names. The other sees that same column already translated into a set of yes-or-no indicators, one per city. Both have the same facts, yet a learning method can only work with the second version, because it needs numbers in every cell. The question this article answers is how to move from raw columns to representations a learning method can use well, and why the features you choose should match the method you apply.

What do you think happens?

Given a column of city names, which version can a learning method use directly: the names as written, or a set of yes-or-no columns, one per city?

  • The city names as written

  • The set of yes-or-no columns, one per city

Reveal answer
Answer:

The set of yes-or-no columns, one per city

A learning method needs numbers in every cell. Raw city names are labels, so they must be converted into numeric indicators before the method can use them.

Why Representation Decides Usability

Definition

Feature engineering is the process of creating new features from existing ones so that a model has better material to learn from. The features chosen, and how well they fit the machine learning method, often matter more to the final result than any other single decision in the pipeline.

The representation of data should suit the learning algorithm. A categorical variable holds labels rather than quantities, such as a color or a product type. A continuous variable holds quantities that can take many values, such as a running number. Each kind of raw column has to be turned into something the method can consume. Two common moves do this work: one-hot encoding for categories, and binning for continuous values. Two further moves, polynomial and interaction terms, create new columns from existing ones. The rest of this article traces each of these moves step by step.

Tracing a Categorical Value Into a One-Hot Vector

A categorical variable is replaced by several columns of zeros and ones, one column for each possible category. In each row, exactly one of those columns is set to 1, and it marks which category that row belongs to. The mechanism has two phases. First, you fix an ordered list of every category that appears. Second, for each value, you build a vector the same length as that list and place a 1 at the position of its category and 0 everywhere else.

red1green0blue0
How does a category like red become a vector of 0s and 1s, with one slot per category?
Worked example

Encoding a color column

A dataset has a color column with rows red, blue, green, and blue. Produce the one-hot vectors for these rows.

  1. 1
    Fix the category order

    List every category that appears once, in a fixed order: red, green, blue. This gives three slots.

  2. 2
    Build a vector for red

    Red is in position 1 of the list, so the vector is 1, 0, 0.

  3. 3
    Build a vector for blue

    Blue is in position 3 of the list, so the vector is 0, 0, 1.

  4. 4
    Apply to every row

    The four rows become 1,0,0; 0,0,1; 0,1,0; 0,0,1. Each row has exactly one 1.

Result

red becomes 1,0,0; blue becomes 0,0,1; green becomes 0,1,0. The original labels now sit in three numeric columns.

Key point

Because each category gets its own position, the encoding does not force any ordering on the categories that the original labels did not have. Red is not treated as smaller than blue simply because of where it appears in the data.

Tracing a Continuous Value Into a Bin Label

Binning creates a new feature by grouping a continuous variable into bins. Instead of treating a value as a single running number, you assign it to the range it falls in. The result is a categorical-style feature that can then be encoded like any other category, which is why binning and one-hot encoding often appear together. The trade-off is that binning gives up fine detail in exchange for a clearer structure.

child0 to 12adult13 to 64senior65 and over
Which interval does a raw number like 37 fall into, and how does that interval become a discrete label?
Worked example

Turning an age into a bin label

Using the bins child (0 to 12), adult (13 to 64), and senior (65 and over), find the bin label for the raw value 37, then encode it.

  1. 1
    Compare to the boundaries

    37 is greater than 12 and less than or equal to 64, so it does not fall in the child bin.

  2. 2
    Select the interval

    37 lies in the adult interval, 13 to 64.

  3. 3
    Replace the number with the label

    The continuous value 37 is now the categorical label adult.

  4. 4
    Encode the label

    With the bin order child, adult, senior, adult becomes the vector 0, 1, 0 using one-hot encoding.

Result

The raw value 37 becomes the bin label adult, which is represented as 0, 1, 0.

Building Polynomial and Interaction Features

Another way to create new features is by adding polynomial and interaction terms. A polynomial term takes an existing feature and raises it to a power. An interaction term multiplies two different features together. Both kinds of term are new columns built from the original ones, and both give the model a way to express relationships that a single raw column cannot express on its own.

Formula

In this expression, x1 and x2 are the raw inputs. The terms $x_{1}^{2}$ and $x_{2}^{2}$ are polynomial terms, and the term $x_{1} x_{2}$ is an interaction term. Each one becomes its own column that the model weights, just like the original inputs.

$x_{1}$raw input$x_{1}$raw input$x_{2}$raw input$x_{2}$raw input$x_{1}^{2}$polynomial$x_{2}^{2}$polynomial$x_{1} x_{2}$interaction
How do raw inputs x1 and x2 turn into new columns such as x1 squared, x2 squared, and x1 times x2, and how does the feature space grow?
Worked example

Expanding one row of two inputs

A row has x1 equal to 2 and x2 equal to 3. Build the polynomial and interaction columns for this row.

  1. 1
    Start from the raw inputs

    The row has two columns: x1 = 2 and x2 = 3.

  2. 2
    Square each input

    $x_{1}^{2}$ = 2 squared = 4, and $x_{2}^{2}$ = 3 squared = 9.

  3. 3
    Multiply the two inputs

    $x_{1} x_{2}$ = 2 times 3 = 6.

  4. 4
    Assemble the expanded row

    The row now has five columns: 2, 3, 4, 9, 6. The feature space grew from two columns to five.

Result

The expanded row is 2, 3, 4, 9, 6.

Why Expansion Helps Linear Models

A linear model combines its inputs with a weighted sum, so on its own it cannot bend a relationship. It can only express a relationship as a weighted combination of the columns it is given. Adding $x^{2}$ or $x_{1} x_{2}$ as columns lets the same weighted-sum machinery capture curvature or joint effects. The model is still linear in its weights, but because one of its columns is now a curved or combined quantity, the relationship it can express becomes richer.

Key point

Expansion does not change the model. It changes the columns the model sees. The same weighted sum now has more inputs to weight.

Matching Features to the Method

The need to hand-build polynomial and binned features depends on the method you pair them with. Flexible nonlinear methods such as random forests and SVMs may learn complex tasks without explicitly expanding the feature space. The match between features and method is the deciding factor, not a single rule that applies everywhere.

MethodRelationship to explicit expansionWhat the source says
Linear modelOften benefits greatly from added polynomial and interaction columnsCannot bend a relationship on its own, so new columns can supply curvature or joint effects
Random forestMay not need explicit polynomial expansionMay learn complex tasks without explicitly expanding the feature space
SVMMay not need explicit polynomial expansionMay learn complex tasks without explicitly expanding the feature space

How the need for explicit feature expansion depends on the learning method

Mistakes Beginners Make With Feature Choices

  • Common mistake

    Feeding raw category labels directly into a learning method that needs numbers

    What it looks like

    Passing the text values red, green, and blue as the input column

    Why it fails

    The method needs numbers in every cell, so the labels must first be represented numerically.

    Do this instead

    Replace the column with one-hot indicator columns, one per category.

  • Common mistake

    Assuming one-hot encoding implies an order among categories

    What it looks like

    Believing that red, green, and blue are ranked because they appear as 1, 2, and 3 in a list

    Why it fails

    Each category gets its own position in the vector, so the encoding does not impose an ordering the labels did not have.

    Do this instead

    Read the vector as indicators of membership, not as a ranking.

  • Common mistake

    Adding polynomial terms to every model by default

    What it looks like

    Expanding every feature of a random forest input with squares and products

    Why it fails

    Flexible nonlinear methods may learn complex tasks without explicit expansion, so the extra columns may add no benefit.

    Do this instead

    Decide on expansion based on the method you are applying.

  • Common mistake

    Choosing bins without considering what the boundaries mean

    What it looks like

    Splitting a continuous variable into arbitrary equal-width ranges that cut across meaningful groups

    Why it fails

    Binning trades fine detail for structure, so poorly placed bins remove information without adding a useful pattern.

    Do this instead

    Choose bin boundaries that reflect a meaningful structure in the problem.

Practice Tracing a Feature Transformation

MEDIUM

A dataset has a product column with values book, lamp, and chair, and an age column with raw values such as 9, 40, and 70. Using the bins child (0 to 12), adult (13 to 64), and senior (65 and over), write the one-hot vector for the product lamp when the category order is book, lamp, chair, and find the bin label for age 40 and its encoded form. Then state whether a random forest would need you to add polynomial terms for the same data, according to the source.

Hints
  • Place a 1 in the slot for lamp, which is second in the fixed order, and 0 everywhere else.
  • Check which interval 40 falls into, then apply the same one-hot step to the bin label.
  • The source says flexible nonlinear methods may learn complex tasks without explicitly expanding the feature space.

Summary of Feature Choices

  1. 1
    A learning method can only use data that is represented in a form it can consume, so representation affects usability.
  2. 2
    One-hot encoding replaces a categorical column with one indicator column per category, with a single 1 per row.
  3. 3
    Binning groups a continuous variable into ranges, trading fine detail for a clearer structure that can then be encoded.
  4. 4
    Polynomial and interaction terms add new columns that let linear models express curvature and joint effects.
  5. 5
    Whether to hand-build these features depends on the method; random forests and SVMs may learn complex tasks without explicit expansion.

At a glance

  • A learning method can only use data that is represented in a form it can consume, so representation affects usability.
  • One-hot encoding replaces a categorical column with one indicator column per category, with a single 1 per row.
  • Binning groups a continuous variable into ranges, trading fine detail for a clearer structure that can then be encoded.
  • Polynomial and interaction terms add new columns that let linear models express curvature and joint effects.
  • Whether to hand-build these features depends on the method; random forests and SVMs may learn complex tasks without explicit expansion.

Related concepts

More concepts starting with C