Coverting The Text to Vector(one hot encoding and bag of words method)

Nlp tutorial · PySpark.in

One-Hot Encoding in NLP

 Why do we need it?

 Example Corpus

D1 = "The food is good" D2 = "The food is bad" D3 = "Pizza is amazing"

Step 1: Find Vocabulary
Unique words across all documents:
["the", "food", "is", "good", "bad", "pizza", "amazing"]

Vocabulary size (V) = 7

Step 2: Create One-Hot Vectors

Each word → represented by a vector of size V (vocabulary size).
Only the index of that word = 1, all others = 0.

Example:

 Step 3: Represent Documents

[[1,0,0,0,0,0,0], # the [0,1,0,0,0,0,0], # food [0,0,1,0,0,0,0], # is [0,0,0,1,0,0,0]] # good

Shape = (4 words × 7 vocab size) → (4×7)

[[1,0,0,0,0,0,0], # the [0,1,0,0,0,0,0], # food [0,0,1,0,0,0,0], # is [0,0,0,0,1,0,0]] # bad

Shape = (4×7)

[[0,0,0,0,0,1,0], # pizza [0,0,1,0,0,0,0], # is [0,0,0,0,0,0,1]] # amazing

Shape = (3×7)

 Note:

One-hot encoding gives a unique binary vector to each word.
 The dimension of each vector = Vocabulary size.
 Each document becomes a matrix: (#words × vocab size).

Limitations (Why not used in NLP anymore?)

Advantages of One Hot Encoding

  1. Easy to implement

    • Libraries like sklearn (OneHotEncoder) and pandas.get_dummies() make it straightforward.

  2. Clear and simple representation

    • Each word/token gets a unique binary vector, which avoids ambiguity.

 Disadvantages of One Hot Encoding

  1. Sparse Matrix Problem

    • Results in high-dimensional vectors with mostly 0s.

    • Increases memory usage & computation.

    • Often leads to overfitting in ML models.

  2. Variable Length Issues

    • Sentences can have different lengths (e.g., 4×7, 3×7 matrices).

    • ML models usually need fixed-size input vectors.

    • Padding or truncation becomes necessary.

  3. No Semantic Meaning

    • Only represents presence/absence of words.

    • Doesn’t capture relationships or similarity between words.

    • Example: “food”, “pizza”, “burger” are equidistant in OHE space, though semantically related.

  4. Out-of-Vocabulary (OOV) Problem

    • New/unseen words in test data cannot be represented if they weren’t in the training vocabulary.

    • This limits generalization.

  5. High Dimensionality with Large Vocabularies

    • Real-world vocabularies can reach 50k+ unique words.

    • OHE vectors then become extremely large, inefficient, and redundant.

 

 Bag of Words (BoW) in NLP

 How it works:

  1. Collect the dataset
    Example sentences:

    • S1: He is a good boy

    • S2: She is a good girl

    • S3: Boy and girl are good

  2. Preprocessing

    • Convert all words to lowercase (avoid duplicates due to case).

    • Remove stopwords (he, she, is, a, and …).

    • Result:

      • S1 → good boy

      • S2 → good girl

      • S3 → boy girl good

  3. Build Vocabulary
    Unique words after preprocessing:
    good, boy, girl

    • good → 3 times

    • boy → 2 times

    • girl → 2 times

     Vocabulary size = 3.

  4. Create Feature Vectors (Bag of Words matrix)

    • Features = [good, boy, girl]

    • S1: good boy[1, 1, 0]

    • S2: good girl[1, 0, 1]

    • S3: boy girl good[1, 1, 1]

    Each sentence is now a fixed-length vector.

  5. Binary vs Frequency BoW

    • Binary BoW → if word present → 1, else 0.

    • Frequency BoW → word count is stored.

      • Example: “good good girl”[2, 0, 1]

 Why Bag of Words is Useful

 Key Difference from One-Hot Encoding

Screenshot 2025-09-14 151212

Bag of Words – Advantages & Disadvantages

Advantages

  1. Simple and intuitive

    • Very easy to understand and implement (e.g., CountVectorizer in sklearn).

  2. Fixed-size input for ML algorithms

    • Unlike One-Hot Encoding, BoW produces fixed-length vectors (length = vocab size).

    • This makes it compatible with most ML models.

  3. Captures frequency (optionally)

    • Unlike OHE (just 0/1), BoW can record how many times a word appears in a sentence.

Disadvantages

  1. Sparse matrix problem

    • For large vocabularies (e.g., 50k words), each document vector will be extremely large with mostly 0s.

    • Leads to memory inefficiency + can cause overfitting.

  2. Ignores word order (loss of context)

    • Sentences like:

      • “boy good girl” → [1,1,1]

      • “good girl boy” → [1,1,1]

    • Both produce the same vector, but meaning can differ → semantic info lost.

  3. Out of Vocabulary (OOV) problem

    • If a new word (e.g., school) appears in test data but wasn’t in training vocab → it gets ignored.

    • Important context can be lost.

  4. No true semantic meaning captured

    • Words are treated as independent tokens → no relationship between “good” vs “bad” or “food” vs “pizza”.

    • Example:

      • “The food is good” → [1,1,1,0]

      • “The food is not good” → [1,1,1,1]
        Cosine similarity might say they are similar, but meaning is opposite.

  5. High dimensionality

    • Larger vocabularies = larger vectors = more storage & computation cost.

Comparison with One-Hot Encoding

More Nlp tutorials

All tutorials · Try the free PySpark compiler · Practice challenges