🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #58

Categorical Data

Categorical data represents groups or labels rather than numbers — city, product category, education level. Most ML algorithms are purely numeric under the hood, so categorical features must be encoded before a model can use them.

Nominal vs Ordinal — The Distinction That Decides Which Encoding to Use

TypeMeaningExampleEncoding
NominalCategories with no inherent orderCity, color, payment methodOne-Hot Encoding
OrdinalCategories with a meaningful orderEducation level (High School < Bachelor's < Master's), rating (Low/Medium/High)Ordinal Encoding

Cardinality Matters

df["city"].nunique()        # low cardinality (e.g. 15 cities) -> one-hot encoding is fine
df["product_id"].nunique()  # high cardinality (e.g. 50,000 products) -> one-hot would explode column count

A "low-cardinality" categorical feature (few unique values) one-hot encodes cleanly. A "high-cardinality" feature (thousands of unique values — user IDs, product SKUs) one-hot encodes into an impractically wide, mostly-zero matrix; frequency encoding, target encoding, or embeddings are usually better fits there.

Practical Use Cases

  • Deciding upfront whether a column is nominal or ordinal — gets the encoding choice right the first time
  • Checking cardinality before committing to one-hot encoding on a wide categorical column

Common Mistakes

  • Label-encoding a nominal feature (like city) with arbitrary integers, which implies a false order the model may pick up on — see Label Encoding.
  • One-hot encoding a high-cardinality column without checking nunique() first, silently creating thousands of sparse columns.

Interview Relevance

Q: "Why shouldn't you always use one-hot encoding for categorical features?" It works well for low-cardinality nominal features, but creates an impractically wide, sparse matrix for high-cardinality features — and it discards ordering information that ordinal encoding would preserve for genuinely ordered categories.

Practice Question

Classify each as nominal or ordinal: (a) blood type, (b) shirt size (S/M/L/XL), (c) country of residence, (d) customer satisfaction rating (1–5).

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Categorical Data – FAQs

Quick answers about learning Categorical Data in Machine Learning.

This free note from CodingNow 2.0 explains Categorical Data in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Categorical Data, is 100% free with no signup required.
With focused practice, most students grasp Categorical Data in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now