TF-IDF and Bag of Words: Text Representation & Search › Technical Deep Dive
TF-IDF and Bag of Words: Text Representation & Search › Technical Deep DiveOne section · Sections in this article3 / 8 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Bag of Words: The Foundation
Let's start with the simplest approach. Given a vocabulary with unique words, we can represent any document as a vector of length , where each element counts how many times word appears in document .
The process involves three steps:
- Tokenization: Split documents into individual words (tokens)
- Vocabulary building: Collect all unique words across all documents
- Vectorization: For each document, count occurrences of each vocabulary word
For example, if our vocabulary is and a document contains "cat runs", the vector would be : one occurrence of "cat", zero of "dog", and one of "runs".
This representation has several properties:
- Fixed dimensionality: All documents map to vectors of the same length
- Sparsity: Most documents use only a small fraction of the vocabulary, so most vector elements are zero
- Order independence: "cat runs" and "runs cat" produce identical vectors
The sparsity is important. In practice, vocabularies can contain tens of thousands of words, but individual documents might use only hundreds. This makes sparse matrix representations efficient for storage and computation.
Term Frequency (TF)
Term frequency measures how often a word appears in a document. The simplest version is the raw count:
However, longer documents naturally contain more words. To make frequencies comparable across documents of different lengths, we often normalize by document length:
This gives us a proportion: what fraction of the document consists of this word? Normalized TF values range from 0 to 1, with 1 meaning the entire document consists of that single word.
Another common normalization uses logarithmic scaling to dampen the effect of very frequent words:
This formula ensures that doubling the word count doesn't double the TF score, which helps prevent extremely common words from dominating the representation.
Inverse Document Frequency (IDF)
While term frequency tells us how important a word is within a document, inverse document frequency measures how distinctive it is across the entire collection. The key insight: words that appear in many documents are less informative than words that appear in few.
The inverse document frequency is calculated as:
where is the total number of documents, and is the number of documents containing term .
Let's break this down:
- If a word appears in all documents, the denominator equals , so
- If a word appears in only one document, the denominator is 1, so
- Words appearing in fewer documents get higher IDF scores
The logarithm serves two purposes: it compresses the scale (so IDF doesn't grow linearly with collection size), and it makes the metric more interpretable. Without the log, a word appearing in 1 out of 1000 documents would have IDF = 1000, while one appearing in 500 documents would have IDF = 2, a 500x difference. The logarithm smooths this out.
Some implementations add 1 to avoid division by zero and to ensure all terms get at least some weight:
TF-IDF: Combining Both Components
TF-IDF multiplies term frequency by inverse document frequency:
This creates a scoring system where:
- High TF-IDF: Words that are frequent in a specific document but rare across the collection
- Low TF-IDF: Words that are either rare in the document or common across many documents
The multiplication is crucial. A word with high TF but low IDF (common everywhere) gets downweighted. A word with low TF but high IDF (rare but distinctive) gets some weight. Only words with both high TF and high IDF get the highest scores.
This weighting scheme automatically identifies the most distinctive words in each document, exactly what we want for tasks like search, classification, and topic modeling.