0Pricing
Learn AI with Python · Lesson

Encoding Categorical Variables

Label encoding, one-hot encoding, ordinal encoding, pd.get_dummies() vs sklearn OrdinalEncoder.

Encoding Categorical Variables is a free Learn AI with Python lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Encode Categories?

Most machine learning models need NUMBERS, not text labels like "red" or "small". Encoding converts categorical variables into numeric form while preserving their meaning.

Ordinal vs Nominal

Ordinal categories have a natural order (small < medium < large). Nominal categories do not (red, green, blue). The right encoding depends on which kind you have.

Label Encoding for Ordinals

LabelEncoder maps each category to an integer. It is appropriate for ORDINAL data where the integer order is meaningful.

from sklearn.preprocessing import LabelEncoder
sizes = ["small", "large", "medium", "small"]
le = LabelEncoder()
print(le.fit_transform(sizes))   # integers (alphabetical by default)

The Danger on Nominals

Applying label encoding to NOMINAL data is risky: the model may read red=0, green=1, blue=2 as green being "between" red and blue, inventing a false order. Use one-hot encoding instead.

One-Hot Encoding with get_dummies

pd.get_dummies creates one binary column per category. Exactly one column is 1 per row, with no implied ordering, ideal for nominal variables.

import pandas as pd
df = pd.DataFrame({"color": ["red", "green", "blue", "red"]})
print(pd.get_dummies(df, columns=["color"]))

The Dummy Variable Trap

When k categories produce k columns, they are perfectly collinear (they always sum to 1). This dummy variable trap breaks linear models. Drop one column to fix it.

drop_first

drop_first=True removes one category, leaving k-1 columns. The dropped category becomes the implicit baseline (all-zeros), eliminating collinearity.

print(pd.get_dummies(df, columns=["color"], drop_first=True))
# one fewer column; the dropped color is the baseline

sklearn OrdinalEncoder

For pipelines, OrdinalEncoder is the sklearn equivalent of label encoding for FEATURES, and lets you specify the category order explicitly.

from sklearn.preprocessing import OrdinalEncoder
enc = OrdinalEncoder(categories=[["small", "medium", "large"]])
print(enc.fit_transform([["medium"], ["large"], ["small"]]))

sklearn OneHotEncoder

OneHotEncoder is the pipeline-friendly one-hot tool. It learns categories from training data and applies the same mapping to new data, crucial for production.

from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder(sparse_output=False)
print(ohe.fit_transform([["red"], ["green"], ["blue"]]))

Handling Unseen Categories

Test data may contain categories never seen in training. Set handle_unknown="ignore" so OneHotEncoder outputs all zeros instead of crashing.

ohe = OneHotEncoder(handle_unknown="ignore", sparse_output=False)
ohe.fit([["red"], ["green"]])
print(ohe.transform([["blue"]]))   # all zeros, no error

High-Cardinality Columns

One-hot encoding a column with thousands of unique values explodes the feature count. For high cardinality consider target encoding, hashing, or grouping rare categories into "other".

Quick Check

Test your encoding knowledge.

Recap

Encoding toolkit:

  • Ordinal data -> integer encoding (LabelEncoder / OrdinalEncoder)
  • Nominal data -> one-hot (pd.get_dummies / OneHotEncoder)
  • drop_first=True avoids the dummy variable trap
  • handle_unknown="ignore" for unseen test categories
  • Watch high-cardinality column explosion

Frequently asked questions

Is the “Encoding Categorical Variables” lesson free?

Yes — the full text of “Encoding Categorical Variables” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Encoding Categorical Variables”?

Label encoding, one-hot encoding, ordinal encoding, pd.get_dummies() vs sklearn OrdinalEncoder. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Encoding Categorical Variables” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Outlier Detection and Removal
  2. Encoding Categorical Variables
  3. Feature Scaling: Normalization and Standardization
  4. Building Preprocessing Pipelines
← Back to Learn AI with Python