Working with Categorical Data
One-hot encoding replaces a categorical variable with one new 0-1 feature per category, so exactly one feature is 1 for each data point.
A Column of Labels the Model Cannot Read
Many datasets describe people, products, or events with labels instead of numbers. A column might record a job type, a color, or a region. A learning algorithm cannot do arithmetic on a label such as 'Private Employee' in any meaningful way, so the label has to be translated into a form the model can use. This article follows one common translation, one-hot encoding, and asks a single question: how does a column of labels become a set of numeric features, and what does each row look like afterward?
What do you think happens?
If a column has four possible labels, how many new columns do you think a one-hot encoding of that column would need? Commit to a number before reading on.
Reveal answer
Four columns, one for each category.
One-hot encoding creates one new feature per category, so four categories produce four features. The rest of this article explains how those features are filled in for each row.
Why Raw Labels Break a Linear Model
A linear model combines feature values with weights. Each feature value is multiplied by its weight, and the products are added together to produce the model's output. This works well when every feature is a number. It does not work when a feature holds text. If a row's color is the text 'Blue', there is no weight that can be multiplied by the word 'Blue' to produce a meaningful contribution to the output. The model needs numbers in every position it uses.
The solution is to rewrite the label as numbers that the model can weight. The rewriting has to preserve the information in the label: a row from one category must look different from a row from another category. One-hot encoding does this by giving each category its own feature, so that each category can contribute its own weight to the result.
The One-Hot Rule: One Feature per Category
One-hot encoding replaces a categorical variable with one new 0-1 feature per category, so exactly one feature is 1 for each data point.
The method is built around a simple rule. One new feature is created for each category. Each new feature answers a yes-or-no question about a single data point: does this row belong to this category? The feature is 1 if the data point has the corresponding category, and 0 otherwise. Because every category gets its own feature, the scheme can represent any number of categories, with one feature per category.
The name 'one-hot' comes from what this rule produces. For any single data point, exactly one of the new features is 1 and the rest are 0. Only one position is 'hot' at a time, which is why the method is also described as one-out-of-N.
Once the new features exist, the original categorical column is no longer needed. When the data is used in a machine learning algorithm, the original column is dropped and only the 0-1 features are kept.
Tracing the Workclass Example
The running example uses a feature called workclass with four possible values: 'Government Employee', 'Private Employee', 'Self Employed', and 'Self Employed Incorporated'. Encoding these four values creates four new features, named after the same four categories. Each row below shows one data point, with the single 1 marking its category.
Now compare that row with a row whose workclass is 'Self Employed'. The four positions are the same, but the 1 moves to the 'Self Employed' position. Every row has the same four columns, and the pattern of 1s and 0s is what tells the model which category the row belongs to.
Encoding a Single Row
A data point has workclass 'Self Employed'. Write its one-hot features in the order Government Employee, Private Employee, Self Employed, Self Employed Incorporated.
- 1Identify the category
The row's workclass is 'Self Employed', the third of the four categories.
- 2Create one feature per category
Four categories produce four binary features, one for each position in the order given.
- 3Set the matching feature to 1
The third feature, Self Employed, is 1 because this row has that category.
- 4Set all other features to 0
Government Employee, Private Employee, and Self Employed Incorporated are 0 because the row does not have those categories.
The one-hot features are 0, 0, 1, 0. The original workclass column is then dropped and these four values are used in its place.
Writing the Encoder in Python
The idea can be written directly in Python without any special library. The function below takes a value and the list of known categories, then returns a list with a 1 in the position of the matching category and 0 everywhere else. This is a generated illustration of the same rule described above, not a library call.
[0, 1, 0, 0]
[0, 0, 0, 1]- The function compares the value with each known category in order.
- The list comprehension produces a 1 at the matching position and 0 elsewhere.
- The categories list fixes the column order, so every row uses the same layout.
The output confirms the rule. The first row has exactly one 1, in the second position, which corresponds to Private Employee. The second row has its 1 in the fourth position. The category order in the categories list must stay the same for every row; otherwise the same position would stand for different categories in different rows.
Mistakes Beginners Make with One-Hot Encoding
Common mistake Feeding the raw text label into a linear model.
What it looks likePassing a column that contains 'Blue', 'Red', and 'Green' directly into the model as if it were a number column.
Why it failsA linear model multiplies feature values by weights. Text cannot be multiplied in a meaningful way, so the model has no usable input for that column.
Do this insteadReplace the column with one 0-1 feature per category, and drop the original text column.
Common mistake Creating fewer features than there are categories.
What it looks likeEncoding four workclass values into only two or three columns.
Why it failsOne-hot encoding creates one feature per category. A missing category has no feature that can be set to 1 for its rows.
Do this insteadCount the distinct categories and create exactly one feature for each.
Common mistake Letting a row have more than one 1, or no 1 at all.
What it looks likeSetting both the Private Employee and Self Employed features to 1 for the same row.
Why it failsThe defining property of one-hot encoding is that exactly one feature is 1 for each data point.
Do this insteadCheck each row: exactly one of the new features should be 1, and the rest should be 0.
Common mistake Keeping the original categorical column alongside the new features.
What it looks likeUsing the workclass text column and the four 0-1 columns together as inputs.
Why it failsAfter encoding, the original column is no longer needed and is dropped when the data is used in a machine learning algorithm.
Do this insteadDrop the original column and use only the 0-1 features in its place.
Dummy Encoding: One Category Left Out
The one-hot encoding used in machine learning is quite similar to the dummy encoding used in statistics, but the two are not identical. The machine learning version gives each category its own binary feature. The statistics version differs in one specific way: dummy encoding uses $k-1$ features for $k$ categories, and the last category is represented as all zeros.
The reason given in the source is analytical. Using $k-1$ features simplifies the analysis, and more technically, it avoids making the data matrix rank-deficient. The source does not go further into the rank argument, so treat that as the stated purpose rather than a full derivation.
To see how the dropped category is still recognized, consider a generated example with three colors: Red, Green, and Blue, treated in that order. Dummy encoding keeps a column for Red and a column for Green. A row that is Red gets [1, 0]. A row that is Green gets [0, 1]. A row that is Blue gets [0, 0]. The all-zero pattern stands for the last category, Blue, which has no column of its own.
Comparing Feature Counts: One-Hot Versus Dummy
| Property | One-hot encoding | Dummy encoding |
|---|---|---|
| Typical use | Machine learning algorithms such as those in scikit-learn | Statistics |
| Features for k categories | k features, one per category | k-1 features |
| Representation of the last category | Its own column with a single 1 | All zeros |
| Four-category example | 4 features | 3 features |
| Exactly one 1 per row | Yes, for every row | Not for the dropped category, which is all zeros |
Comparing the two encodings for a feature with k categories
Practice: Encode a Color Column
A dataset has a color column with four categories, in this order: Red, Green, Blue, Yellow. Write the one-hot features for a row whose color is Blue, and then state how many features dummy encoding would produce for the same column.
Hints
- One-hot encoding gives one feature per category, so count the four categories first.
- For the Blue row, only the third position should be 1.
- Dummy encoding uses k-1 features for k categories, so subtract one from the number of categories.
Summary of Encoding Categorical Data
- 1A linear model cannot use a text label directly, so a categorical variable must be rewritten as numbers.
- 2One-hot encoding creates one new 0-1 feature per category, and exactly one of those features is 1 for each data point.
- 3After encoding, the original categorical column is dropped and the 0-1 features are used in its place.
- 4Dummy encoding from statistics uses k-1 features for k categories, with the last category represented as all zeros, while one-hot encoding uses k features.
At a glance
- A linear model needs numbers in every feature position, so text labels must be rewritten before modeling.
- One-hot encoding creates one new 0-1 feature per category, with exactly one 1 for each data point.
- The original categorical column is dropped once the 0-1 features are created.
- Dummy encoding uses k-1 features for k categories, representing the last category as all zeros, while one-hot encoding uses k features.