Bag of Words
Building a Vocabulary
From corpus to word list
Minimum frequency = 1: every word in the corpus is included. The vocabulary has all unique words — common ones like 'the' (6×) and rare ones like 'big' (1×).
Step 1 of 6: Building a Vocabulary
From corpus to word list
Minimum frequency = 1: every word in the corpus is included. The vocabulary has all unique words — common ones like 'the' (6×) and rare ones like 'big' (1×).
Step 2 of 6: One-Hot Encoding
One word, one dimension
"a" is at index 0 → its one-hot vector has a 1 at position 0 and 0s everywhere else. Common words get low indices. Every word occupies exactly one dimension — no notion of similarity between words yet.
Step 3 of 6: Count Vectors
Summing one-hots into a document vector
Doc 0 has 6 tokens mapping to 5 unique words. Words like "the" appear 2× — the count vector captures frequency, not position.
Step 4 of 6: Document-Term Matrix
The full picture — all documents, all terms
The document-term matrix is the full BoW picture: every document is a row, every term is a column. With 10 terms, the matrix is 56% sparse — most cells are 0. Real corpora are 99%+ sparse.
Step 5 of 6: Binary vs. Count vs. Normalized
Three flavors of BoW
Count BoW: raw word counts. 'the' appears 2× in Doc 0, so its cell is 2. Preserves frequency, but longer documents naturally have higher counts — unfair for comparison.
Step 6 of 6: Limitations
What BoW loses
"Dog bites man" — a normal headline. The BoW vector is [1,1,1]. Now slide to reorder the words and watch: the meaning changes completely, but the BoW vector stays the same.