Data Representation for Machine Learning
Data representation is the step of deciding how data processed by machine learning is represented.
A Customer Table the Algorithm Cannot Read Directly
Imagine you are preparing a table of information about customers so that a machine learning algorithm can learn from it. Some columns hold numbers, such as an income or an age. Others hold labels, such as a city name or a product color. A learning algorithm cannot simply read a label the way a person does. The data has to be represented in a form the algorithm can work with, and the way you represent it can change how well the final model performs.
What do you think happens?
What do you think has to happen to the city column before the algorithm can learn from it?
Reveal answer
Each city label has to be turned into numbers the algorithm can work with.
A learning algorithm cannot simply read a label the way a person does, so the label must be represented in a numerical form. Choosing that form is part of data representation.
This article follows one idea: feature engineering is the work of deciding how the inputs to a model should look. Instead of accepting every column exactly as it arrived, you create new features from existing data. Some of those features make a category usable, some reshape a number into groups, and some combine several numbers into new terms. As you read, keep one question in mind: what does the model need the data to look like in order to learn from it?
What Data Representation Asks
Data representation is the step of deciding how data processed by machine learning is represented.
The same real-world information can be written down in many different ways, and each way changes what the system can make of it. Choices about representation are made before learning begins, and those choices shape what the system can learn. The step sits at the very start of the learning process, so it is worth understanding the question before looking at any particular method.
The idea names two linked questions. The first is how the data should be represented: how it is expressed so that machine learning can work with it. The second is which data aspects to focus on: which parts of the data deserve attention. Both questions are needed. A representation that captures the wrong aspects of the data will not help the learner, and a well-chosen set of aspects still needs a usable form.
Question one asks about form, and question two asks about selection. Each aspect you keep should be checked against the goal, and each form you choose should preserve what that aspect means.
Feature Engineering as Designing the Inputs
Feature engineering means engineering new features. A new feature is often derived from one or more features that already exist in your data. The person creating the feature can use expert knowledge, meaning knowledge about the problem domain, to decide which derived features are likely to be useful. Someone who understands the problem may know that a certain combination of measurements matters more than any single measurement alone.
The techniques in this article are four common ways to create derived features: one-hot encoding for categorical variables, binning for continuous variables, polynomial terms, and interaction terms. One-hot encoding is a way of representing data. The other three are ways of expanding the set of features the model sees.
Tracing a Category Through One-Hot Encoding
A categorical variable takes one of a limited set of labels, such as red, green, or blue. One-hot encoding replaces one column of labels with several columns of numbers, one column for each possible category. For any given row, the column that matches the row's category holds a 1, and every other category column holds a 0.
The following example is an original illustration written for this article. It uses a plain Python list of colors and builds one row of 0s and 1s per entry. The categories are sorted so the column order is predictable: blue comes first, then green, then red.
columns: ['blue', 'green', 'red']
red [0, 0, 1]
green [0, 1, 0]
blue [1, 0, 0]
red [0, 0, 1]sorted(set(colors))lists each distinct label once, in alphabetical order- The list comprehension puts a 1 in the column that matches the row's label and a 0 everywhere else
Trace the first row through the loop. The label is red. Blue does not match, so its column holds 0. Green does not match, so it holds 0. Red matches, so its column holds 1. The result is [0, 0, 1]. The fourth entry is also red, so it produces the same row. The representation is a table that a learning algorithm can read, even though it no longer contains the word red.
Notice that each 1 sits in exactly one position per row. The number of columns equals the number of possible categories. Because each category now has its own column, the algorithm can treat red, green, and blue as separate inputs rather than as an ordered scale.
Binning: Turning a Range into Slots
A continuous variable, such as an age or a temperature, can take any value within a range. Binning creates a new feature by grouping continuous values into bins. Each bin covers a range of values, and a value is assigned to the bin whose range contains it. The idea is to replace a fine-grained number with the index of the group it falls into.
The example below is an original illustration. The bin edges define four ranges: below 18, from 18 up to 35, from 35 up to 65, and 65 and above. The function returns the index of the bin that each age falls into. The edges are a choice made for this example, not a rule from the source.
4 -> bin 0
17 -> bin 0
34 -> bin 1
62 -> bin 2bisect_rightcounts how many edges are less than or equal to the value, which gives the bin index- Ages 4 and 17 both fall below the first edge, so they share bin 0
Polynomial Terms: Adding a Squared Column
Polynomial terms are new columns built from an existing numeric feature by raising it to a power. The simplest example adds a column for $x^{2}$ next to the original column $x$. The source describes adding polynomials as one of the ways to create new features for linear models.
| Row | x | x squared |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 4 |
| 3 | 3 | 9 |
| 4 | 4 | 16 |
Adding an x squared column to a single numeric feature x
Why does the new column help? A model that combines the original $x$ column in a straight-line way can only draw a straight relationship between $x$ and the output. Once a column holding $x^{2}$ is available, the model can combine both columns, so it can represent a curve. The model is still linear in its inputs, but the inputs now include a curved version of the original feature. Adding the column changes what the model is able to express, not the data itself.
A polynomial term does not replace the original feature. It sits beside it, so the model can use both the raw value and its squared value.
Interaction Terms: Combining Two Features
An interaction term is a new column formed as the product of two existing features. The source describes interactions as one of the ways to build new columns for linear models, alongside binning and polynomials. The new column is useful when the effect of one feature depends on the value of another.
The example below is an original illustration using a small sales table. Each sale has a unit price and a number of units bought. Multiplying the two gives a column that tracks total spend for each sale.
| Sale | Unit price | Units bought | Price times units |
|---|---|---|---|
| A | 2 | 10 | 20 |
| B | 2 | 1 | 2 |
| C | 9 | 10 | 90 |
| D | 9 | 1 | 9 |
An interaction column built from price and units
Look at sales A and C, which both bought ten units, and sales A and B, which share a unit price of 2. Price alone does not separate the large purchases from the small ones, and units alone ignore how much each unit costs. The interaction column shows the combined effect directly: 20 for A, 2 for B, 90 for C, and 9 for D. Neither feature by itself carries that combined information, but the product does.
Choosing Techniques for Linear and Nonlinear Models
The source draws a contrast between two families of models. Linear models may benefit greatly from new features created by binning, polynomials, and interactions. More complex nonlinear models, such as random forests and SVMs, might learn complicated tasks without the feature space being expanded explicitly. The choice of technique therefore depends on the kind of model you are training.
| Technique | What it does | Linear models | Nonlinear models such as random forests and SVMs |
|---|---|---|---|
| One-hot encoding | Turns categorical labels into 0 and 1 columns | Needs numeric input, so categories must be encoded | Categories still need a numerical form before learning |
| Binning | Groups a continuous value into a range slot | May benefit greatly | May learn the pattern without this expansion |
| Polynomial terms | Adds powers of a feature as new columns | May benefit greatly | May learn the pattern without this expansion |
| Interaction terms | Multiplies two features into one new column | May benefit greatly | May learn the pattern without this expansion |
How the four techniques relate to the two model families
Applying the Two Questions to a Sales Record
Consider a small, invented case. A shop keeps a record for each sale. Every record includes a date, a product name, a price, and the name of the store branch. A team wants a machine learning system to help predict how much a customer will spend. The worked example below applies both questions to this record.
Representing a sales record for spending prediction
A sale record has a date, a product name, a price, and a store branch. Decide how each field should be represented, which aspects to keep, and why.
- 1Question one: the product name
The product name is a categorical label. A learning algorithm cannot read it directly, so one-hot encoding turns it into one 0 or 1 column per product.
- 2Question one: the store branch
The branch name is also a label with a limited set of values, so it gets the same one-hot treatment as the product name.
- 3Question one: the price
The price is already a number, so it can be kept as a numeric column without encoding.
- 4Question two: checking the date against the goal
The goal is how much a customer will spend. A raw date does not directly express spending, so the date is a candidate to drop unless the goal calls for a time pattern. Keeping or dropping it is a decision that must be justified against the goal.
- 5Question two: considering an interaction
An interaction needs two numeric features whose product carries meaning. This record has only one numeric field, the price, so no interaction term is proposed here. An interaction would need a second numeric quantity, such as units bought, to be added to the record.
- 6Justifying the choices
Each aspect kept, namely the product, the branch, and the price, is connected to the spending goal. Each form chosen preserves its meaning: the labels become indicator columns, and the price stays a number.
Keep the product and branch as one-hot columns and the price as a numeric column. Treat the date as a candidate to drop because it does not directly serve the spending goal. Do not add an interaction term until the record includes a second numeric feature.
Mistakes Beginners Make with Representation
Common mistake Leaving a label column as text and expecting the algorithm to read it the way a person does
What it looks likeA city column with the values Paris, Lagos, and Lima is passed to the model unchanged.
Why it failsA learning algorithm cannot simply read a label the way a person does, so the data has to be represented in a form it can work with.
Do this insteadApply one-hot encoding so each city becomes its own 0 or 1 column.
Common mistake Treating binning as if it keeps all the information in the original number
What it looks likeAges 4 and 17 are both placed in bin 0, and the difference between them is no longer visible to the model.
Why it failsBinning discards the exact value within each range, so the feature records only which group a value belongs to.
Do this insteadChoose the bin edges deliberately, and keep the original numeric column too if the exact value matters.
Common mistake Choosing bin edges as if a fixed rule dictated them
What it looks likeThe edges 18, 35, and 65 are used without asking what the problem needs.
Why it failsThe source gives no fixed rule for choosing edges, so the choice is a modeling decision.
Do this insteadPick edges that reflect the goal, and state why they were chosen.
Common mistake Adding every possible derived feature without checking it against the goal
What it looks likeA date, a price, and a branch are all kept and expanded, even though the date does not serve the spending goal.
Why it failsEach aspect kept should be checked against the goal. Features that do not serve the goal do not help the learner.
Do this insteadKeep only the aspects that serve the goal, and justify each keep or drop decision.
Common mistake Applying polynomial or interaction expansions to every model without considering whether the model family benefits from them
Practice: Representing a Small Dataset
A small table has three rows. Each row records a student's favorite subject (math, art, or history), the hours studied, and whether the student passed. Decide how to represent the subject column, whether binning would help the hours column, and whether an interaction between subject and hours would be meaningful. Justify each choice by naming the goal the representation serves.
Hints
- Ask first whether the subject is a label or a number, and which technique turns labels into 0 and 1 columns.
- Binning replaces a continuous value with a range slot. Consider what you would lose by doing that to hours studied.
- An interaction term multiplies two features. Subject is a label, so think about whether a product of a label and a number has a clear meaning.
Key Takeaways
- Data representation is the step of deciding how data processed by machine learning is represented, and it shapes what a learning system can learn.
- The step asks two linked questions: how the data should be represented, and which data aspects to focus on.
- One-hot encoding replaces a categorical column with one 0 or 1 column per category, binning groups continuous values into range slots, and polynomial and interaction terms add new columns built from existing features.
- Linear models may benefit greatly from binning, polynomials, and interactions, while nonlinear models such as random forests and SVMs might learn complicated tasks without the feature space being expanded explicitly.
- Every representation choice should be checked against the goal, and each form chosen should preserve what the kept aspect means.