Bag of Words (BoW)
Tip the words of a document into a bucket and shake it. You have lost the order, the grammar and the sentence boundaries — and you still know a great deal about what the document is about. That is the bet behind bag of words.
What the vector looks like
Fix a vocabulary from the corpus, then represent each document as a vector with one dimension per vocabulary entry, holding the number of times that word occurs. Stack the documents and you have a document-term matrix: rows for documents, columns for words, almost entirely zeros, since one review uses a few dozen of the 50,000 available words. Scikit-learn's CountVectorizer builds precisely this.
What it buys and what it costs
The representation is cheap, exactly interpretable and a genuinely strong baseline — bag-of-words logistic regression on movie reviews or spam is hard to beat for the effort. What it discards is real:
- Order, so “not good” and “good, not bad” collapse towards each other; short n-grams recover a little of it.
- Meaning, so “film” and “movie” are as unrelated as “film” and “tractor”.
- Unseen words, which have no column at all and vanish silently.
Why it matters
Raw counts favour whatever is most common, which is why these vectors are usually reweighted by TF-IDF before use, and why dense embeddings replaced them wherever similarity of meaning matters. Bag of words remains the first thing to try on a text problem and the baseline every later representation is measured against.
Bag of words represents a document as a vector of word counts over a fixed vocabulary, discarding word order and syntax. Stacked across a corpus these vectors form the sparse document-term matrix behind classic text classification and keyword retrieval. It is cheap, interpretable and still a strong baseline, but treats synonyms as unrelated dimensions and has no slot for unseen words — the gaps TF-IDF weighting and dense embeddings address.
Imagine cutting a page into individual words, dropping them into a bag, and counting how many times each one turns up. You no longer know what order they came in — but seeing “goal”, “referee” and “penalty” twenty times still tells you the page is about football.
That is bag of words: the oldest trick for turning text into numbers a computer can compare. It works surprisingly well for sorting documents by topic or flagging spam. Its weakness is obvious once you look for it — “not good” and “good” contain almost the same words, so the bag cannot tell them apart.