What is the bag-of-words model?
The bag-of-words (BoW) model represents a document as the counts of the words it contains, ignoring their order. A fixed vocabulary assigns each word a position, and each document becomes a vector holding how many times each vocabulary word appears. Two sentences with the same words in a different order get the same vector.
Bag-of-words is the simplest way to turn text into numbers that a machine learning model can use. Spam filters, sentiment classifiers, topic models such as LDA, and early search engines were all built on it, usually paired with naive Bayes or logistic regression. It is still a quick, interpretable baseline, and TF-IDF is a reweighted version of the same vectors.
Key ideas
- Bag-of-words represents a document by its word counts and discards word order.
- A vocabulary maps each kept word to one fixed index in the vector.
- A document's count vector is the sum of the one-hot vectors of its tokens.
- Stacking the vectors gives a document-term matrix that is almost entirely zeros.
- Cells can be binary, raw counts, or counts divided by document length.
- N-grams recover some local word order at the cost of a much larger vocabulary.
Common questions
What is the difference between bag of words and TF-IDF?
Bag-of-words fills each cell with a count, so frequent function words get large values. TF-IDF starts from the same counts and multiplies each by an inverse document frequency weight, which shrinks words that appear in most documents and boosts rare, topical ones. Both produce vectors of the same shape.
Why is it called a "bag" of words?
In mathematics a bag, or multiset, is a collection that records how many times each item occurs but not their order. The idea of describing text by word distributions goes back to Zellig Harris's 1954 paper "Distributional Structure", which uses the phrase "bag of words" when discussing language.
How does bag-of-words handle words it has never seen?
A word missing from the vocabulary has no column, so it is dropped. The hashing trick avoids a fixed vocabulary by hashing each word to one of a set number of columns. That handles any word but lets unrelated words collide in the same column.
Is bag-of-words used outside text?
Yes. The bag-of-visual-words model clusters small image patches into a codebook of "visual words" and describes each image by a histogram of those clusters. It was a standard image classification method before convolutional networks, and the same counting idea appears in audio and genomics.
Related explainers
- Tokenization & BPE — How text becomes numbers — from characters to subword tokens via byte-pair encoding
- TF-IDF — Score how important a word is to a document in a collection — the backbone of search engines and text retrieval
- Naive Bayes Classifier — Probabilistic classification with Bayes' rule
- Text Classification & Sentiment — Teach a model to assign labels to text — from spam detection to sentiment analysis
- Embeddings — Turning words and categories into vectors
- Singular Value Decomposition — Factor any matrix into rotation, scaling, and rotation — the Swiss army knife of linear algebra
References
- Harris (1954), "Distributional Structure", Word
- Deerwester et al. (1990), "Indexing by Latent Semantic Analysis", Journal of the American Society for Information Science
- Jurafsky & Martin, "Speech and Language Processing" (3rd ed. draft)
Last updated