MACHINE LEARNING • LESSON 6

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.

THE SIMPLEST DEFINITION

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.

01

What Is Categorical Data?

Categorical data describes groups or categories instead of quantities.

CITY Hyderabad
CITY Mumbai
CITY Chennai

These are categories. They are names, not numerical measurements.

Other examples include:

Gender → Male, Female
Color → Red, Blue, Green
Product Type → Phone, Laptop, Tablet
Membership → Basic, Premium, VIP
02

Why Do We Need Encoding?

Suppose we want to predict whether a customer will buy a product.

Age City Orders Purchased
25 Hyderabad 5 Yes
31 Mumbai 3 No

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.

ORIGINAL Hyderabad
ENCODED Numerical representation
MODEL Can use the feature
03

One Simple Encoding Method

One basic approach is to assign a number to each category.

For example:

Color Encoded Value
Red 0
Blue 1
Green 2

Now the categories have numerical representations.

But there is an important problem: does 2 mean "greater than" 1 and 1 mean "greater than" 0?

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.

04

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:

Red
Blue
Green

One-hot encoding can represent them like this:

Color Red Blue Green
Red 1 0 0
Blue 0 1 0
Green 0 0 1

Each row has a 1 for the category it belongs to and 0 for the other categories.

Red is not greater than Blue, and Blue is not greater than Green. Each category simply gets its own indicator.
05

Ordered Categories Are Different

Some categorical values have a natural order.

For example:

Basic
Premium
VIP

Here, there is a meaningful order:

Basic Premium VIP

For ordered categories, numerical encoding can represent that order, depending on the model and the encoding method.

EXAMPLE

Basic = 1, Premium = 2, VIP = 3

Here the numbers have meaning because the categories have a natural order.

06

Choosing the Encoding

The main question is whether the categories have a natural order.

NO NATURAL ORDER Red, Blue, Green

One-hot encoding is often a suitable choice.

NATURAL ORDER Basic, Premium, VIP

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.

07

Real-World Example

Imagine an e-commerce dataset:

Age Product Type Orders Purchased
25 Laptop 5 Yes
31 Phone 3 No
28 Tablet 7 Yes

Product Type is categorical data.

We could represent it using one-hot encoding:

Product Laptop Phone Tablet
Laptop 1 0 0
Phone 0 1 0
Tablet 0 0 1

Now the product category has been converted into numerical features that can be used by a machine learning model.

REMEMBER THIS

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.

QUICK CHECK

Which Encoding Makes Sense?

Color: Red, Blue, Green No natural order → one-hot encoding is often suitable
Membership: Basic, Premium, VIP Natural order → ordered numerical representation may be suitable
Age: 20, 25, 30 Already numerical → no categorical encoding needed
Answer

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.

NEXT TOPIC

Feature Scaling

After preparing categorical features, we may still have numerical features with very different ranges. Next, we will learn how feature scaling puts numerical features onto a more comparable scale.