What is label encoding?
Label encoding converts categories into integers:
- high → 2
When it’s OK
-
For ordinal categories (where order matters):
- low < medium < high
-
For tree models sometimes (but still be careful)
When to avoid it
For nominal categories (no order):
- city, color, country
Label encoding may trick models into believing an order exists.
Example: ordinal encoding (manual)
import pandas as pd
df = pd.DataFrame({"priority": ["low", "high", "medium", "low"]})
mapping = {"low": 0, "medium": 1, "high": 2}
df["priority_code"] = df["priority"].map(mapping)
print(df)Using scikit-learn LabelEncoder
LabelEncoder is mainly designed for encoding target labels, not features.
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y = ["spam", "ham", "ham", "spam"]
print(le.fit_transform(y))
print(le.classes_)Tip
Use:
- One-hot encoding for nominal categories
- Ordinal mapping for ordered categories
Visualize it
flowchart LR A["priority column
(low, medium, high)"] --> B["mapping: low->0, medium->1, high->2"] B --> C["priority_code column
(0, 1, 2)"]
🧪 Try It Yourself
Exercise 1 – Encode with a manual mapping
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Encode with scikit-learn's LabelEncoder
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Get codes from a category dtype
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Check your understanding
When is label encoding appropriate for a feature?
Integers imply an order, which is fine when one exists. For nominal data it can trick a model into believing city 2 is greater than city 1.
Show answer
A — For ordinal categories such as low, medium and high — Integers imply an order, which is fine when one exists. For nominal data it can trick a model into believing city 2 is greater than city 1.
With mapping low=0, medium=1, high=2, what is the priority_code for the values low, high, medium and low?
map replaces each value using the dict, giving 0 for low, 2 for high, 1 for medium and 0 for low.
Show answer
A — 0, 2, 1, 0 — map replaces each value using the dict, giving 0 for low, 2 for high, 1 for medium and 0 for low.
For y = spam, ham, ham, spam, what does LabelEncoder().fit_transform(y) return?
classes_ are sorted alphabetically as ham then spam, so ham is 0 and spam is 1.
Show answer
A — [1 0 0 1] — classes_ are sorted alphabetically as ham then spam, so ham is 0 and spam is 1.
What is LabelEncoder mainly designed for?
For features, use one-hot encoding for nominal columns or an ordinal mapping for ordered ones.
Show answer
A — Encoding target labels, not features — For features, use one-hot encoding for nominal columns or an ordinal mapping for ordered ones.
Next
For categories with no natural order, prefer One-Hot Encoding so the model doesn't assume a ranking that isn't real.