Notes

Inverse Document Frequency (IDF)

Count the words in an article and "the" wins every time, which tells you nothing about the article. Inverse document frequency is the correction for that: a weight that rises as a word gets rarer across a collection.

How the weight is computed
IDF looks past a single document at the whole corpus. With N documents in the collection and df(t) of them containing the term t, the standard form is idf(t) = log(N / df(t)), almost always with smoothing — log(N / (1 + df(t))), or the variant scikit-learn uses — so a term present in every document does not collapse to exactly zero and an unseen term does not divide by zero. The logarithm is load-bearing: without it, rarity would dominate the score instead of tempering it. Multiplied by term frequency it produces TF-IDF, the weight that fills a document-term matrix.

Where you meet it

  • Search ranking: a query for "running shoes" is informative on "shoes" and nearly worthless on "running" in a sportswear catalogue, and BM25 carries an IDF factor for the same reason.
  • Feature weighting: TfidfVectorizer in scikit-learn is the usual starting point for a text-classifier baseline.
  • Keyword extraction: the highest TF-IDF terms in a document are a cheap and surprisingly serviceable statement of what it is about.

Why it matters
IDF is corpus knowledge injected into a per-document vector: it is how a bag of counts learns that "mitochondria" is signal and "however" is noise, with nobody writing a stopword list. Compute it on the wrong collection — a legal corpus's IDF applied to social media posts — and the weights invert, promoting exactly the terms that are common in the data you actually have.

Inverse document frequency weights a term by how rare it is across a document collection, computed as the logarithm of the total number of documents divided by the number containing that term. It discounts words appearing everywhere and rewards distinctive ones. Multiplied by term frequency it produces TF-IDF, the standard weighting for sparse text vectors and a component of BM25; without it, frequent function words dominate every similarity score.

If every book in a library mentions the word "chapter", that word tells you nothing about which book you want. But if only three mention "volcanoes", that word is gold. Inverse document frequency is the simple idea that a word becomes more useful the fewer places it turns up in.

Search engines and text-sorting tools use it to decide which words in your request are worth paying attention to. It is why typing "the best espresso machine" finds pages about espresso machines rather than pages stuffed with "the". Rare words carry the meaning; common ones get quietly turned down.