Types of data (numerical, categorical, ordinal, text)
Overview
Understanding data types is the foundation of feature engineering. Every ML algorithm makes assumptions about input data—feed it the wrong type and your model will either crash or silently produce garbage. This note explains what each data type represents, why the distinction matters for algorithms, and how to recognize and handle each type.
Core Data Types
1. Numerical Data
WHY the distinction matters:
- Continuous data → normalize/standardize for gradient-based models (prevents large-scale features from dominating)
- Discrete data → sometimes better treated as categorical if values are few (e.g., "number of bedrooms: 1, 2, 3" might work better one-hot encoded for tree models)
We want to transform data so it has mean = 0, standard deviation = 1.
Step 1: Remove the mean (centering) where is the mean.
WHY? Centering shifts the distribution so its center is at zero. This removes location bias.
Step 2: Scale by standard deviation where is the standard deviation.
WHY? Dividing by ensures the spread (variance) becomes 1. Now all features are on the same scale.
When to use: Linear regression, logistic regression, SVMs, neural networks (any algorithm using distance metrics or gradient descent).
Step 1: Calculate mean
WHY this step? We need the distribution's center to shift it to zero.
Step 2: Calculate standard deviation
WHY this step? Standard deviation measures spread. We use it to normalize the scale.
Step 3: Scale each value
WHY this step? Now each value represents "how many standard deviations from the mean"—a universal scale.
Result: Scaled values: [-1.09, 0.62, -0.41, 1.64, -0.75] All values now have mean ≈ 0, std ≈ 1, making them comparable to other scaled features.
2. Categorical Data
KEY INSIGHT: You cannot compute "mean of colors" or "USA - India." These are labels, not numbers.
Problem: ML algorithms need numbers. How do we convert categories to numbers without implying fake order?
Solution: Create a binary column for each category.
Step 1: List all unique categories Categories: {Cat A, Cat B, Cat C}
Step 2: For each sample, create a binary vector
- If sample is Cat A: [1, 0]
- If sample is Cat B: [0, 1, 0]
- If sample is Cat C: [0, 0, 1]
WHY this works: Each category gets its own dimension. No category is "closer" to another in numeric space. Distance between any two one-hot vectors is the same (√2 in Euclidean space).
Mathematical representation: For categories, encode category as: where the 1 is in position .
| Transaction | Payment Method |
|---|---|
| 1 | UPI |
| 2 | Credit Card |
| 3 | UPI |
| 4 | Cash |
Step 1: Identify unique categories Categories: {UPI, Credit Card, Cash} →3 categories
WHY this step? We need to know how many binary columns to create.
Step 2: Create binary columns
Create columns: is_UPI, is_CreditCard, is_Cash
Step 3: Encode each row
| Transaction | is_UPI | is_CreditCard | is_Cash |
|---|---|---|---|
| 1 | 1 | 0 | 0 |
| 2 | 0 | 1 | 0 |
| 3 | 1 | 0 | 0 |
| 4 | 0 | 0 | 1 |
WHY this encoding?
- Transaction 1 is UPI → turn on UPI column, turn off others
- No category is numerically "between" others
- Distance from UPI to Cash = distance from UPI to Credit Card = √2
Alternative for tree models: Label encoding (UPI=0, Credit Card=1, Cash=2) works fine because trees split on individual values, not distances.
Why it feels right: "I converted text to numbers, the model can handle it!"
Why it's wrong: The model now thinks Cash (2) is "twice" Credit Card (1), and Credit Card is "between" UPI and Cash. It will learn weights like for Cash, implying mathematical relationships that don't exist.
The fix: Use one-hot encoding for algorithms that assume numeric relationships (linear models, neural nets). Use label encoding ONLY for tree-based models (which split on "is equal to X") ordinal data.
Steel-man the mistake: Label encoding saves memory (1 column vs. k columns) and can work if the model learns to ignore the fake ordering. But it's risky—why introduce a bias when one-hot is safer?
3. Ordinal Data
KEY DISTINCTION from categorical: Order matters. PhD > Master's is meaningful. But the "gap" from High School to Bachelor's ≠ gap from Master's to PhD.
Constraint: We need to preserve order:
Solution: Map categories to integers respecting order: where
WHY this works: Algorithms that use comparisons (tree splits: "is education≥ 2?") preserve the ordering. Distance-based algorithms (KNN, linear models) will assume uniform spacing—acceptable if the ordinal gaps are roughly similar, problematic otherwise.
| Employee | Education |
|---|---|
| A | Bachelor's |
| B | PhD |
| C | High School |
| D | Master's |
Step 1: Define the ordering High School < Bachelor's < Master's < PhD
WHY this step? We must explicitly specify the order before encoding.
Step 2: Assign integer codes
- High School → 0
- Bachelor's → 1
- Master's → 2
- PhD → 3
Step 3: Apply encoding
| Employee | Education_Encoded |
|---|---|
| A | 1 |
| B | 3 |
| C | 0 |
| D | 2 |
WHY this encoding?
- A decision tree can split: "education_encoded ≥ 2" → captures Master's and PhD
- Order is preserved: 0 < 1 < 2 < 3 matches reality
- Linear model will assume equal spacing (problem if PhD is "much more" than Master's in impact)
When to worry about spacing: If predicting salary and PhD holders earn3× Bachelor's holders (not 3× the increment), consider:
- Creating multiple binary features:
has_bachelors,has_masters,has_phd - Using target encoding (replace with mean target value per category)
4. Text Data
KEY CHALLENGE: Text is high-dimensional, variable-length, and context-dependent. "Bank" (financial institution) vs. "river bank" requires understanding context.
Goal: Convert a document into a fixed-length vector of numbers.
Step 1: Build the vocabulary (set of all unique words across all documents)
WHY? We need a fixed set of dimensions. Vocabulary size = number of dimensions.
Step 2: For each document , count occurrences of each word where
WHY this works: Each document becomes a point in space. Similar documents (using similar words) are close in this space.
Mathematical form: For vocabulary of size , document is:
Limitation: Ignores word order. "Dog bites man" = "Man bites dog" (same BoW vector).
- "The movie was good"
- "The movie was bad"
- "Great movie"
Step 1: Build vocabulary Extract unique words: {the, movie, was, good, bad, great} Vocabulary size = 6
WHY this step? Defines our feature space dimensions.
Step 2: Count word occurrences for each document
Document 1: "The movie was good"
- the: 1, movie: 1, was: 1, good: 1, bad: 0, great: 0
- Vector: [1, 1, 1, 0, 0]
WHY this step? Converts text to numbers by counting.
Document 2: "The movie was bad"
- the: 1, movie: 1, was: 1, good: 0, bad: 1, great: 0
- Vector: [1, 1, 0, 1, 0]
Document 3: "Great movie"
- the: 0, movie: 1, was: 0, good: 0, bad: 0, great: 1
- Vector: [0, 1, 0, 0, 1]
Result Matrix:
| the | movie | was | good | bad | great | |
|---|---|---|---|---|---|---|
| D1 | 1 | 1 | 1 | 0 | ||
| D2 | 1 | 1 | 1 | 0 | 1 | 0 |
| D3 | 0 | 1 | 0 | 0 | 0 | 1 |
Interpretation:
- D1 and D2 are similar (share "the movie was") → dot product = 3
- D3 is different (different words) → dot product with D1 = 1
Next step for real use: Remove stop words ("the", "was"), apply TF-IDF to down-weight common words.
Why it feels right: "The model will just ignore unknown words."
Why it's wrong: If your test set uses "excellent" extensively to indicate positive reviews, your model sees it as all-zeros (missing signal). Performance drops because you're blind to key information.
The fix:
- During training: Keep a
<UNK>(unknown) token in vocabulary - During testing: Map unseen words to
<UNK> - Better: Use subword tokenization (BPE, WordPiece) so "excellent" breaks into "excell" + "ent," reusing learned pieces
- Best: Use pre-trained embedings (Word2Vec, BERT) that already know millions of words
Steel-man the mistake: Ignoring unknowns is simple and sometimes works if test set is similar to training. But in production, language evolves—new slang, product names, events. Handling unknowns is critical for robustness.
Advanced Considerations
80/20 Rule: 80% of performance comes from handling the 4core types correctly. Spend time here before exotic feature engineering.
Data Type Decision Tree
Is the data text in sentences?
├─ YES → Text Data (BoW, TF-IDF, embedings)
└─ NO → Is it numbers?
├─ YES → Can you compute meaningful average?
│ ├─ YES → Numerical (continuous or discrete)
│ └─ NO → Probably categorical (e.g., zip codes look numeric but aren't)
└─ NO → Is there a meaningful order?
├─ YES → Ordinal (education, ratings)
└─ NO → Categorical (colors, countries)
Connections
- Normalization and Scaling → Deep dive into min-max, z-score, robust scaling
- One-Hot Encoding vs Label Encoding → When to use which encoding strategy
- TF-IDF and Text Vectorization → Advanced text feature extraction
- Feature Engineering from Datetime → Extracting features from timestamps
- Handling High-Cardinality Categorical → Embedings, target encoding, hashing
- Curse of Dimensionality → Why one-hot encoding can explode feature space
- Feature Scaling Impact on Gradient Descent → Mathematical proof of why scaling matters
Recall Explain to a12-Year-Old
Imagine you're organizing your school supplies.
Numerical data is like counting pencils—you can say "I have 5 pencils" and do math (add3 more = 8 total). You can find the average across your class.
Categorical data is like your pencil colors—red, blue, green. You can't say "red + blue = ?" or "average color." They're just different types, no math involved.
Ordinal data is like medal rankings: bronze< silver < gold. There's an order (gold is better), but you can't say "gold is exactly twice as good as silver." The order matters, but the spacing between ranks isn't equal.
Text data is like your diary entries—full sentences and paragraphs. Computers don't understand English directly, so we have to convert it to numbers first. We might count how many times words appear: "homework" appears 3 times, "fun" appears 0 times (sadly!).
Why does this matter? If you give a calculator words, it breaks. If you treat your pencil colors like numbers ("red = 1, blue = 2"), the calculator thinks blue is "twice" red, which is nonsense. Match the data type to the tool!
#flashcards/ai-ml
What are the four fundamental data types in ML? :: Numerical (continuous/discrete), Categorical (no order), Ordinal (ordered categories), Text (natural language)
Why must we scale numerical features for gradient descent algorithms?
What is the formula for z-score normalization?
Why is one-hot encoding preferred over label encoding for categorical data in linear models?
When is it acceptable to use label encoding instead of one-hot encoding?
What is the difference between categorical and ordinal data?
How does Bag-of-Words convert text to numbers?
What is the main limitation of Bag-of-Words representation?
Why can treating zip codes as numerical data be problematic?
What is the 80/20 rule for data type handling?
How do you handle unseen words in test data forBoW models?
What is high-cardinality categorical data and why is one-hot encoding problematic for it?
When should you extract features from datetime instead of using it as numerical?
What does standardization achieve mathematically?
Why is the distance between any two one-hot encoded vectors the same?
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Data types samajhna ML ka sabse pehla important step hai. Socho tumhare pas ek dataset hai customer reviews ka—kuch columns mein numbers hain (age, price), kuch mein categories (color, city), kuch mein ratings (1 star se 5 star), aur kuch mein pure sentences (actual review text). Abagar tum directly yeh sab ek neural network ko feed kar do, wo crash ho jayega ya garbage output dega. Kyun? Kyunki har algorithm ki apni "language" hoti hai.
Numerical data wo hai jahan math meaningful hai—tum average nikal sakte ho, addition kar sakte ho. Jaise height170 cm hai toh standard scaling karo taki neural network ke liye sab features equal importance mein ayein. Categorical data mein order nahi hota—red, blue, green sab equal distance pe hone chahiye, isliye one-hot encoding use karte hain (har color ko apna column milta hai: is_red, is_blue, is_green). Galti yeh hoti hai ki log label encoding kar dete hain (red=0, blue=1) aur linear model sochta hai blue "twice red" hai, jo bilkul galat hai.
Ordinal data mein order important hai but spacing equal nahi—jaise education levels (10th < 12th < Graduation < Post-grad). Yahan 0,1,2,3 encode kar sakte ho kyunki order preserve ho raha hai. Lekin agar PhD wala person Bachelor's wale se kafi zyada kamata hai (not just "1 unit more"), toh yeh encoding problem create kar sakta hai distance-based models mein. Text data sabse tricky hai—"bahut accha movie tha" yeh sentence hai, numbers nahi. Bag-of-Words use karo: har unique word ko count karo. Lekin limitation yeh hai ki word order lost ho jata hai, "dog bites man" aur "man bites dog" same vector ban jaate hain!
Thumb rule: