Machine Learning models are geniuses at math, but they are illiterate when it comes to words. If your dataset has a column for "City" (Paris, Tokyo, New York), a linear regression model will literally crash because it can't multiply "Paris" by a coefficient.
👉 Feature Engineering is how we translate human categories into machine-readable math.
💡 The Wrong Way: Label Encoding
You might think:
I'll just give them numbers! Paris = 1, Tokyo = 2, New York = 3.
⛔️The Danger: The model will think New York (3) is "greater than" Paris (1). It might assume New York is three times more "City-ish" than Paris. This creates fake relationships in your data.
✅ The Right Way: One-Hot Encoding (Dummy Variables)
We create new columns for every category. If the row is "Paris," the Paris column gets a 1 and the others get a 0. It’s binary. It’s fair. It’s mathematical.
🐍 Let's see it in Python (Pandas)
import pandas as pd
# 1. Create a tiny dataset
data = {'User': ['Alex', 'Sam', 'Jo'],
'Plan': ['Free', 'Premium', 'Free']}
df = pd.DataFrame(data)
# 2. The Magic Function
df_encoded = pd.get_dummies(df, columns=['Plan'])
print(df_encoded)
⚠️ The "Dummy Variable Trap" (Pro Tip)
If you know someone is NOT on the Free plan, you automatically know they ARE on the Premium plan (in a 2-plan system). Having both columns creates "Multicollinearity"—basically, telling the model the same thing twice.
The Fix: Always use
drop_first=True in your code to remove that redundant column and keep your model lean.🎯 Today's Takeaway
Don't just feed raw text into a model. Transform your categories into a "One-Hot" format to ensure your machine understands the difference without inventing a hierarchy.