liveBook · Manning

Inverse document frequency

As explained in Natural Language Processing in Action, Second Edition

Inverse document frequency is a term-weighting measure defined from how many documents in a corpus contain a term, assigning lower weight to terms occurring in many documents.

Inverse document frequency (IDF) measures how much information a term adds by comparing the total corpus size with the number of documents containing that term. Common terms such as “intelligence” appear across many AI books and distinguish little; rarer terms such as “neural network” can identify documents more usefully. section 3.6

Compute and interpret IDF in a small corpus:

  • Count the documents containing the term, rather than counting every occurrence. section 3.6
  • Multiply term frequency by IDF to obtain a TF–IDF weight, combining within-document prominence with corpus-wide distinctiveness. section 3.0
  • Use logarithms to reduce extreme raw differences in large collections; one rare document among a million otherwise produces an outsized score. section 3.6.1

Comparing “bias” and “and”

Two documents from a corpus about algorithmic racial discrimination contain “and” at roughly 0.0292 and 0.0303 term frequency, while “bias” occurs at 0.0167 and 0.0022. Because both terms occur in both documents, their raw IDF is 1; TF–IDF therefore preserves the difference in document-level usage without pretending that either term distinguishes the documents corpus-wide.

Equation 3.5
figure

Inspect the formal TF and IDF equations behind the practical counting examples. section 3.6.1

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains document counting, raw ratios, TF–IDF multiplication, and logarithmic scaling, stopping short of broader vector similarity details.
Examples
Demonstrates IDF with AI books, bias documents, “and,” “bias,” “black,” “cat,” and “dog.”
What it assumes
Assumes term-frequency counts and basic tokenized documents, while developing corpus-level weighting from those quantities.
What you can do after
Apply it via Python counters and token lists to calculate document frequencies, IDF values, and TF–IDF weights.
How it connects
Connects term frequency, TF–IDF, Zipf’s law, bag-of-words, n-grams, cosine similarity, search engines, and spam filters.

Also explained in

Compare how 4 books explain inverse document frequency →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.