Encoding Categorical Data
Machine learning models usually work with numerical values. But real-world datasets often contain categories such as city, color, product type, or membership level. Encoding converts these categories into numerical values that a machine learning model can use.
Encoding means converting categorical values into numbers.
For example, a dataset may contain Red, Blue, and Green. A machine learning algorithm may need these categories represented numerically before it can use them as input.
What Is Categorical Data?
Categorical data describes groups or categories instead of quantities.
These are categories. They are names, not numerical measurements.
Other examples include:
Why Do We Need Encoding?
Suppose we want to predict whether a customer will buy a product.
Age and Orders are already numbers. City contains text.
We need to transform the City information into a numerical representation that the model can use.
One Simple Encoding Method
One basic approach is to assign a number to each category.
For example:
Now the categories have numerical representations.
No. Green is not mathematically greater than Blue. The numbers are only labels.
This is why simply assigning numbers to categories can be dangerous when the categories have no natural order.
One-Hot Encoding
A common solution for categories without a natural order is one-hot encoding.
Instead of giving each category one number, we create a separate column for each category.
Suppose we have:
One-hot encoding can represent them like this:
Each row has a 1 for the category it belongs to and 0 for the other categories.
Ordered Categories Are Different
Some categorical values have a natural order.
For example:
Here, there is a meaningful order:
For ordered categories, numerical encoding can represent that order, depending on the model and the encoding method.
Basic = 1, Premium = 2, VIP = 3
Here the numbers have meaning because the categories have a natural order.
Choosing the Encoding
The main question is whether the categories have a natural order.
One-hot encoding is often a suitable choice.
An ordered numerical representation may be appropriate.
There is no single encoding method that is best for every dataset. The correct choice depends on the categories and the machine learning model.
Real-World Example
Imagine an e-commerce dataset:
Product Type is categorical data.
We could represent it using one-hot encoding:
Now the product category has been converted into numerical features that can be used by a machine learning model.
Machine Learning Models Need Usable Numerical Inputs.
Categorical data contains groups such as cities, colors, and product types. Encoding converts those categories into numerical representations that a model can work with.
Which Encoding Makes Sense?
Red, Blue, and Green have no natural order, so one-hot encoding is often appropriate. Basic, Premium, and VIP have a natural order, so an ordered numerical representation may be appropriate. Age is already numerical and does not need categorical encoding.