TF-IDF
Term Frequency
Counting words in a document
"rocket" has high TF in D1, D3 but zero in the others. Terms concentrated in specific documents are more discriminating.
Step 1 of 6: Term Frequency
Counting words in a document
"rocket" has high TF in D1, D3 but zero in the others. Terms concentrated in specific documents are more discriminating.
Step 2 of 6: Inverse Document Frequency
Rare words matter more
"rocket" appears in 2 of 4 documents — IDF = log(4/2) = 0.69. The fewer documents a term appears in, the higher its IDF score.
Step 3 of 6: TF-IDF Score
Combining frequency and rarity
"rocket" has different TF-IDF scores across documents. Higher scores mean the term is both frequent in that document (high TF) and rare across the corpus (high IDF) — the hallmark of a distinctive term.
Step 4 of 6: Document Vectors
Documents as points in term-space
D1: Space has a very sparse vector — only 2 non-zero dimensions. Most terms don't appear in this document, so those dimensions are 0. Sparse vectors are typical in TF-IDF because each document uses only a small fraction of the vocabulary.
Step 5 of 6: Document Similarity
Finding related documents with cosine similarity
D1: Space is most similar to D3: Space (cosine = 0.548) because they share TF-IDF dimensions: rocket, space. Documents with no shared terms have cosine similarity = 0 — their vectors are orthogonal.
Step 6 of 6: TF-IDF in Practice
Search ranking and retrieval
Single-term query "rocket": only terms where both query and document have non-zero values contribute to the dot product. This is exactly how early search engines like AltaVista worked!