Introduction to Categorical Data

Hello, Space Voyager! Today, we're venturing through a fascinating territory: Categorical Data Encoding! Categorical Data consist of groups or traits such as "gender", "marital status", or "hometown". We convert categories into numbers using Label and One-Hot Encoding techniques for our machine-learning mates.

Concept of Label Encoding

Label Encoding maps categories to numbers ranging from 0 through N-1, where N represents the unique category count. It's beneficial for ordered data like "Small", "Medium", and "Large".

To illustrate, here is a Python list of shirt sizes:

sizes = ["Small", "Medium", "Large"]

Python's Pandas library can be used to assign 0 to "Small", 1 to "Medium", and 2 to "Large":

import pandas as pd


df = pd.DataFrame({
    'item_id': [1302, 1440, 1220, 2038, 1102],
    'sizes': ['Small', 'Medium', 'Large', 'Small', 'Medium']
})

size_mapping = {"Small": 0, "Medium": 1, "Large": 2}
df['sizes'] = df['sizes'].map(size_mapping)  # Apply mapping to the specified column
print(df)
'''Output:
   item_id  sizes
0     1302      0
1     1440      1
2     1220      2
3     2038      0
4     1102      1
'''

In this example, we define mapping in the most natural way for it – as a dictionary. Then, we apply this mapping using dataframe's .map function.

Concept of One-Hot Encoding

One-Hot Encoding creates additional columns for each category, placing a 1 for the appropriate category and 0s elsewhere. It's preferred for nominal data, where order doesn't matter, such as "Red", "Green", "Blue".

You can perform one-hot encoding with Pandas' .get_dummies():

import pandas as pd

df = pd.DataFrame({
    'item_id': [1302, 1440, 1220, 2038, 1102],
    'colors': ['Red', 'Green', 'Blue', 'Red', 'Green']
})

df = pd.get_dummies(df, columns=['colors'])  # One-hot encode specified column
print(df)
'''Output:
   item_id  colors_Blue  colors_Green  colors_Red
0     1302        False         False        True
1     1440        False          True       False
2     1220         True         False       False
3     2038        False         False        True
4     1102        False          True       False
'''
Why One-Hot Encoding?
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal