Inverse Document Frequency › Worked Example: IDF from Scratch on a New Corpus

Michael BrenndoerferMarch 28, 20254 min read
Language AI HandbookAll chapters
Part 2 of 67Classical Text Representations
Chapter 12Inverse Document Frequency › Worked Example: IDF from Scratch on a New Corpus
Reading section 11 of 14 · Worked Example: IDF from Scratch on a New Corpus · 4 minSection 11 of 14 · 4 min

Book contents

Section 11 of 14 · 4 min read

Inverse Document Frequency › Worked Example: IDF from Scratch on a New CorpusOne section · Sections in this chapter11 / 14 ↓
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

Let's walk through a complete IDF computation on a fresh corpus to consolidate everything we've covered. We'll use a small corpus of movie reviews to show how IDF looks outside the machine learning domain.

In[28]:
Code
# A small movie review corpus
movie_corpus = [
    "A brilliant film with outstanding performances and compelling storytelling.",
    "Dull plot drags on forever. The acting was mediocre at best.",
    "Stunning visuals and a breathtaking score make this a must-watch masterpiece.",
    "A predictable romantic comedy with charm and warmth that grows on you.",
    "Terrifying horror that builds genuine dread through atmosphere and sound design.",
    "The cinematography is breathtaking. The director crafts tension masterfully.",
    "A romantic story with heartfelt performances and memorable dialogue.",
    "Boring and predictable. The plot never surprises. Acting was flat.",
]

movie_tokenized = [tokenize(doc) for doc in movie_corpus]
movie_N = len(movie_tokenized)

# Step 1: Compute document frequencies
movie_df = compute_document_frequency(movie_tokenized)

# Step 2: Compute standard IDF
movie_idf = compute_idf(movie_df, movie_N)

# Step 3: Compute all variants
movie_idf_variants = compute_idf_variants(movie_df, movie_N)
Out[29]:
Console
Movie Review Corpus IDF Analysis (8 documents)
======================================================================

Top 15 Most Informative Terms (highest IDF):
-------------------------------------------------------
Term                     DF        IDF Type
-------------------------------------------------------
  brilliant               1     2.0794  unique
  compelling              1     2.0794  unique
  film                    1     2.0794  unique
  outstanding             1     2.0794  unique
  storytelling            1     2.0794  unique
  at                      1     2.0794  unique
  best                    1     2.0794  unique
  drags                   1     2.0794  unique
  dull                    1     2.0794  unique
  forever                 1     2.0794  unique
  mediocre                1     2.0794  unique
  make                    1     2.0794  unique
  masterpiece             1     2.0794  unique
  must                    1     2.0794  unique
  score                   1     2.0794  unique


Terms with Zero IDF (appear in all 8 documents):
  None


Medium IDF Terms (0.3 < IDF < 1.5, appear in 2-6 docs):
---------------------------------------------
  performances           DF=2  IDF=1.3863
  acting                 DF=2  IDF=1.3863
  on                     DF=2  IDF=1.3863
  plot                   DF=2  IDF=1.3863
  was                    DF=2  IDF=1.3863
  breathtaking           DF=2  IDF=1.3863
  predictable            DF=2  IDF=1.3863
  romantic               DF=2  IDF=1.3863
Out[30]:
Visualization
Histogram showing IDF distribution across all vocabulary terms in the movie review corpus, with most terms clustered at high IDF values.
IDF value distribution for the movie review corpus. Terms at IDF=0 on the left appear in every review, while the rightmost cluster at IDF=2.08 (log(8/1)) contains terms unique to a single document and therefore provides the highest discriminative power. The middle range contains terms appearing in two or three reviews. Most vocabulary sits in the high-IDF range, confirming the natural sparsity of meaningful language.

The histogram reveals something true of virtually every natural language corpus: the vast majority of vocabulary terms appear in only a small fraction of documents, clustering at high IDF values. A small number of common words cluster at low IDF values. This bimodal shape is the statistical signature of natural language, where Zipf's law produces many rare terms and few common ones.

This distribution has a practical implication: after you apply IDF weighting to a TF matrix, most of the weight in your document vectors will be concentrated on relatively rare terms. Common words, which dominate raw word counts, are suppressed toward zero. The signal carried by specific, topic-relevant vocabulary is amplified. This is exactly the transformation that makes TF-IDF useful for retrieval.