BF50 Black Friday · 50% off all plans
from $5/mo · Auto-applied ---:--:-- Claim 50% off →
Skip to content

Search Seonio

Type a keyword, domain, or paste a URL

Start free with $1 to see full results. Powered by Seonio
Data Science

Inverse document frequency: Formula and examples

Inverse document frequency (IDF) scores how rare a word is across a set of documents: rare words score high, words found everywhere score zero.

Monthly searches · inverse document frequency
368.0k
Keyword research data
inverse document frequency
Inverse document frequency: Formula and examples
Click the image to view it full size

Inverse document frequency (IDF) is a weight that measures how much a word tells you about a document, based on how many documents in a collection contain it. A word that appears in nearly every document, such as "the", tells you almost nothing and gets a weight close to zero. A word that appears in only a few documents, such as "portafilter" in a set of coffee articles, is a strong clue to what those documents are about and gets a high weight.

Karen Spärck Jones introduced IDF in 1972, and it's still a building block of search. It's the "IDF" half of TF-IDF and part of the BM25 ranking formula.

The IDF formula

The standard textbook formula is:

idf(t) = log(N / df(t))

  • N is the total number of documents in the collection.
  • df(t), the document frequency, is the number of documents that contain the term t at least once. How often the term appears inside each document doesn't matter here.
  • log is usually base 10 in textbooks; some software uses the natural logarithm. The base changes the size of the numbers, not their order.

The logarithm keeps the scale manageable. Without it, a word in one document out of a million would weigh a million times more than a word in every document.

A worked example

Say you have 1,000 articles about coffee, and you count how many contain each word:

Word Documents containing it (df) N / df IDF (log base 10)
the 1,000 1 0
coffee 800 1.25 0.10
espresso 100 10 1
portafilter 10 100 2

"The" appears everywhere, so its IDF is zero. "Coffee" is in nearly every article, so in this collection it barely distinguishes anything, even though it's the topic. "Portafilter" is rare, so an article that uses it is probably about espresso machines in particular.

A real example from an IR textbook: across Shakespeare's 37 plays, "Romeo" appears in one play and has an IDF of 1.57; "Falstaff" appears in four (0.967); "good" and "sweet" appear in all 37 and score 0 (Jurafsky and Martin).

How IDF combines with term frequency

IDF says nothing about any single document; it's a property of a word in a collection. To score a word in one document, multiply it by the term frequency (TF), how often the word appears in that document:

tf-idf(t, d) = tf(t, d) × idf(t)

A word scores high when it's frequent in this document and rare across the collection. Those are the words that best describe what makes the document different. Textbooks usually take the log of the raw count for TF too, so a word that appears 100 times doesn't count 100 times as much as a word that appears once. See term frequency for the details.

Search engines use the same idea to rank documents for a query: a match on a rare query word counts for more than a match on a common one. That's also why stop words matter less than they used to: IDF already gives words like "the" and "of" almost no weight.

Why tools give different IDF numbers

If you compare IDF scores from two tools, they often won't match. The common reasons:

  • Different log base. Base 10 and natural log give different numbers for the same counts.
  • Smoothing. The plain formula breaks when a word appears in no documents (you'd divide by zero). Many libraries add 1 to the counts. scikit-learn's default is ln((1 + N) / (1 + df)) + 1, so a word in every document gets 1, not 0 (scikit-learn).
  • Normalization. Some tools scale each document's TF-IDF scores so long and short documents can be compared.
  • A different collection. This matters most. IDF is only meaningful relative to the documents you counted.

Compare scores only when they come from the same tool, settings and collection.

IDF in SEO content tools

Some content tools build a collection from the pages that rank for a keyword, then use TF-IDF to list words those pages use often that your page doesn't. Used carefully, that can reveal subtopics you've missed: a guide to espresso that never mentions grind size or tamping probably has a gap.

Keep three limits in mind:

  • The collection decides the result. Ten ranking pages is a small collection, and a word can look important just because two competitors happen to use it.
  • Word statistics aren't a ranking target. Google describes many ranking systems, from language understanding to links, and its guidance says there's no magical word count target and encourages writing naturally. Cramming in extra terms risks keyword stuffing, which breaks its spam policies.
  • Rare isn't the same as relevant. A typo or a brand name can have a very high IDF. Read the suggested words and keep only the ones a reader would expect.

Treat IDF-based suggestions as a checklist for coverage, not a target to hit. The page still has to answer the reader's question better than the others.

Seonio

Put it to work on your own site

Keyword research, traffic, backlinks and clustering in one place. Start with a free $1 to test, no card required.