Categorical Data 🏷️
Handling Text Categories
Machine Learning models require numbers. They cannot understand "Red", "Green", "Blue". We must convert these categories into numbers.
✨ One Hot Encoding (Dummification)
Best for Nominal data (no order, e.g., Colors, Cities).
It creates a new binary column for each category.
python
import pandas as pd
data = {'Car': ['Volvo', 'VW', 'BMW', 'VW']}
df = pd.DataFrame(data)
# Create dummy variables
ohe = pd.get_dummies(df[['Car']])
print(ohe)
# Output:
# Car_BMW Car_Volvo Car_VW
# 0 0 1 0
# 1 0 0 1
# 2 1 0 0
# 3 0 0 1🔢 Label Encoding
Best for Ordinal data (ordered, e.g., Low, Medium, High).
It assigns a unique integer to each category.
python
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
data = ['Low', 'Medium', 'High', 'Medium', 'Low']
encoded = le.fit_transform(data)
print(encoded)
# Output might be: [1 2 0 2 1] (depending on alphabetical order)⚠️ Watch Out!
Don't use Label Encoding for Nominal data (like colors). The model might think that Red (2) is "greater than" Blue (1), which is incorrect.