TF-IDF Weighting

The standard way to weight terms in the vector space model. Term frequency and the bag-of-words view, document frequency and inverse document frequency, their product as a term weight, a four-document running example worked by hand, a worked example on Reuters statistics, and the theoretical arguments for IDF.

The TF-IDF weight of term $t$ in document $d$ is $\mathit{tf}_{t,d} \times \log_{10}(N/\mathit{df}_t)$. It grows with the term's count in the document, shrinks with the number of documents containing it, and is zero for a term in every document. On this topic's four-document running example, summing it over best car insurance lifts the auto-insurance page from third under raw counts to second. But the car review, with five occurrences of car, stays first (0.875) and the best match, a short headline, stays last (0.426): raw counts and length still need damping and normalization. IDF changes a ranking only for multi-term queries. For rare terms it nearly equals the Binary Independence Model's weight without relevance information (1.648 against 1.638 for car in Reuters RCV1).

For the query best car insurance, the three words do not matter equally. Car and insurance say what the searcher wants. Best appears in so many documents, on every subject, that it does less to tell one document from another. Within a single document, a word that appears ten times is probably more central than one that appears once. A ranking function has to turn these two observations into numbers.

The standard way is TF-IDF weighting. It gives each term in each document a weight that grows with the term's count in the document, its term frequency, and shrinks with the number of documents that contain it, through its inverse document frequency. The two components underlie most of the ranking functions in this course. Later articles in this topic build on the definitions here: cosine similarity uses the weights as the coordinates of document vectors, and variants of TF-IDF refines how each component is computed.

Here is the route, following Manning, Raghavan, and Schütze (2008, §6.2). First we set up a four-document collection that the rest of this topic shares. Then we score it by term frequency alone and see why that fails. Next we add inverse document frequency and multiply the two. Finally we give the two main arguments for why IDF works, from information theory and from probability, and solve three problems. The architecture article previewed the simplest TF-IDF score on the thesis student's Arctic sentences; this article develops it properly.

A running example

The articles in this topic share one small collection, so that each new idea can be seen changing the same numbers. It has four documents and the query best car insurance. Each document is shown by its term counts after text processing: lowercasing, stop-word removal, and folding plurals such as rates to rate.

  • $d_1$, a headline about car insurance rates: car insurance rates.
  • $d_2$, a car review that repeats car and never mentions insurance.
  • $d_3$, a page about auto insurance that says auto where others say car.
  • $d_4$, a longer page about car loans and their rates.
Term $d_1$ $d_2$ $d_3$ $d_4$ query
auto 0 1 3 0 0
best 0 2 1 2 1
car 1 5 0 3 1
insurance 1 0 2 0 1
loan 0 0 0 4 0
rate 1 0 0 3 0

A reader looking for car insurance would want $d_1$ and $d_3$, and would not want $d_2$ or $d_4$. That judgment is the yardstick for every scoring function below. The table is the collection's term-document count matrix, with one row per term and one column per document.

Term frequency

The term frequency $\mathit{tf}_{t,d}$ is the number of times term $t$ occurs in document $d$. Representing a document by the term frequencies of its terms, and nothing else, is the bag-of-words model. Word order is discarded, so Mary is quicker than John and John is quicker than Mary have identical representations.

That loss seems severe. The model accepts it: every ranking in this topic is computed from which words occur and how often, and nothing else. Phrases and word order are handled separately, with positional indexes.

A first ranking function simply adds up the term frequencies of the query terms:

$$\text{score}(q, d) = \sum_{t \in q} \mathit{tf}_{t,d}.$$

Example (the overlap score). This overlap score puts the car review $d_2$ first for best car insurance, a document the reader does not want. The score reads the query rows of the count matrix and adds them:

Document best + car + insurance Overlap score
$d_1$ 0 + 1 + 1 2
$d_2$ 2 + 5 + 0 7
$d_3$ 1 + 0 + 2 3
$d_4$ 2 + 3 + 0 5

The ranking is $d_2$, $d_4$, $d_3$, $d_1$: the two documents the reader does not want come first. The overlap score has two flaws, and both show here.

  • All terms count equally. A match on a word that appears in nearly every document counts as much as a match on a word that appears in very few, although the rare word says far more about whether the document is on topic.
  • Repetition counts without limit. Five occurrences of car outweigh a document that matches the whole query once.

The first flaw is about the collection, not the document. Fixing it needs a measure of how rare each term is.

Inverse document frequency

A term is informative when it is rare: it appears in few documents, so its presence distinguishes those documents from the rest. The document frequency $\mathit{df}_t$ is the number of documents containing $t$. For a collection of $N$ documents, the inverse document frequency is

$$\mathit{idf}_t = \log_{10} \frac{N}{\mathit{df}_t}.$$

A term in every document has $\mathit{idf} = \log 1 = 0$. A term in one document in a million has $\mathit{idf} = 6$. The logarithm compresses the range, so a term a thousand times rarer gets a weight 3 higher, not a thousand times higher. The base of the logarithm does not affect rankings, since changing it multiplies every weight by the same constant.

In the running example, $N = 4$. The document frequencies are read off the count matrix by counting the nonzero entries in each row:

Term $\mathit{df}_t$ $N / \mathit{df}_t$ $\mathit{idf}_t$
auto 2 4/2 0.301
best 3 4/3 0.125
car 3 4/3 0.125
insurance 2 4/2 0.301
loan 1 4/1 0.602
rate 2 4/2 0.301

Insurance occurs in half the documents and car in three-quarters, so insurance gets the larger weight, 0.301 against 0.125. With only four documents the possible values are coarse, just 0, 0.125, 0.301 and 0.602, but the order is what matters.

Why document frequency, not total count

Document frequency is used rather than collection frequency, the total number of occurrences, because it measures how widely a term is spread across documents. Discrimination depends on that spread. As described in term statistics, try and insurance have nearly equal collection frequencies in Reuters, but insurance is concentrated in far fewer documents. It is the better discriminator, and only document frequency shows it.

The running example shows the same thing on a small scale. Auto and loan each occur 4 times in all. But loan is confined to one document and auto is spread over two, so loan gets the higher IDF.

IDF on a real collection

Manning, Raghavan, and Schütze (2008, §6.2.1) give document frequencies from the Reuters RCV1 collection, which has $N = 806{,}791$ documents:

Term $\mathit{df}_t$ $\mathit{idf}_t$
auto 6,723 2.08
car 18,165 1.65
insurance 19,241 1.62
best 25,235 1.50

The differences are modest because all four terms are moderately common. (The textbook's two tables disagree about insurance. This one gives it a document frequency of 19,241, and the comparison with try described in term statistics gives 3,997. The text labels both as taken from the Reuters collection and does not explain the difference, so the two tables should not be combined.) For a term such as the, present in nearly every document, the weight would be close to zero. For a rare technical term it would be several times larger.

The shape of the IDF curve

IDF against document frequency for a collection the size of Reuters RCV1. On a linear axis (left), notice that the weight falls steeply over the first few thousand documents and is nearly flat after that, so most of its range separates rare terms from one another. On a logarithmic axis (right) the same function is a straight line: every tenfold drop in document frequency adds 1 to the weight, or about 3.3 bits on the right-hand scale. The four Reuters terms of the table sit close together near the middle.
IDF against document frequency for a collection the size of Reuters RCV1. On a linear axis (left), notice that the weight falls steeply over the first few thousand documents and is nearly flat after that, so most of its range separates rare terms from one another. On a logarithmic axis (right) the same function is a straight line: every tenfold drop in document frequency adds 1 to the weight, or about 3.3 bits on the right-hand scale. The four Reuters terms of the table sit close together near the middle.

The left panel of the figure shows why the logarithm is there. Without it, $N/\mathit{df}_t$ would give a term in one document 806,791 times the weight of a term in every document, and one rare word in a query would swamp everything else. With it, the largest possible weight is $\log_{10} 806{,}791 \approx 5.91$.

The right panel plots the same function against $\log \mathit{df}_t$, where it is a straight line of slope $-1$: $\mathit{idf}_t = \log N - \log \mathit{df}_t$. A term's weight depends only on how many powers of ten separate its document frequency from the collection size.

The idea originated with Karen Spärck Jones (1972). She showed experimentally that weighting terms by a decreasing function of how many documents they occur in improved retrieval. She presented this as a statistical interpretation of term specificity: a term's specificity is judged by how many documents it is used in, not by its meaning.

IDF measures a term across the collection, and term frequency measures it within a document. The next step puts them together.

Combining them

The TF-IDF weight of term $t$ in document $d$ is the product

$$\text{tf-idf}_{t,d} = \mathit{tf}_{t,d} \times \mathit{idf}_t.$$

It is highest when a term occurs many times in a few documents. It is lower when the term occurs fewer times or in more documents, and it is zero when the term occurs in every document. Replacing raw term frequency in the overlap score gives

$$\text{score}(q, d) = \sum_{t \in q} \text{tf-idf}_{t,d}.$$

The running example weighted

Multiplying each row of the count matrix by its term's IDF gives the TF-IDF matrix. Values are rounded to three decimals and computed from unrounded IDFs:

Term $d_1$ $d_2$ $d_3$ $d_4$
auto 0 0.301 0.903 0
best 0 0.250 0.125 0.250
car 0.125 0.625 0 0.375
insurance 0.301 0 0.602 0
loan 0 0 0 2.408
rate 0.301 0 0 0.903

The score of each document adds its weights for the three query terms:

Document best + car + insurance TF-IDF score
$d_1$ 0 + 0.125 + 0.301 0.426
$d_2$ 0.250 + 0.625 + 0 0.875
$d_3$ 0.125 + 0 + 0.602 0.727
$d_4$ 0.250 + 0.375 + 0 0.625
The running example scored for best car insurance, by the overlap score (left) and the TF-IDF score (right). Solid bars are the documents a reader would want, about car or auto insurance; hatched bars are the car review and the car-loan page. Notice that IDF lifts the auto-insurance page from third to second, because insurance is the rarer query term, but the car review stays first on the strength of five occurrences of car.
The running example scored for best car insurance, by the overlap score (left) and the TF-IDF score (right). Solid bars are the documents a reader would want, about car or auto insurance; hatched bars are the car review and the car-loan page. Notice that IDF lifts the auto-insurance page from third to second, because insurance is the rarer query term, but the car review stays first on the strength of five occurrences of car.

IDF has done part of the job. Insurance, the rarer and more specific query term, now counts for more than twice as much per occurrence as best or car, and the auto-insurance page $d_3$ rises from third to second. But two problems remain:

  • $d_2$ is still first. Five occurrences of car at 0.125 each outweigh anything $d_1$ can offer.
  • $d_1$ is last. It matches two of the three query terms and is exactly what the reader wants, but it is short and every count in it is 1.

These are the two problems the rest of the topic fixes. Sublinear term frequency stops repetition from counting without limit. The length normalization of cosine similarity stops long documents from collecting large scores merely by being long. With both, $d_1$ ranks first and $d_3$ second.

Repetition on Reuters statistics

The running example's counts are small. With counts of a realistic size, the first problem is starker. Take three made-up documents and score them for the query car insurance, with the Reuters IDFs above rounded to two decimals (car 1.65, insurance 1.62):

Document car insurance TF-IDF score
A 100 0 165.00
B 1 1 3.27
C 10 2 19.74

A never mentions insurance, yet it outscores C, which uses both query terms, by more than eight times. Raw term frequency lets one heavily repeated term carry a document a long way. That weakness motivates the sublinear scaling of term frequency, which returns to these three documents. Longer documents also tend to have larger term frequencies, which motivates the length normalization of the next article.

IDF and single-term queries

Example (a one-term query). IDF does not change how the one-term query car ranks the running example. For a query of one term, every document's score is multiplied by the same IDF value, so the order is unchanged as long as that value is positive. (A term in every document has IDF 0, and then every score is 0.) The query car scores the documents 0.125, 0.625, 0 and 0.375. That is 0.125 times their counts of car, 1, 5, 0 and 3, and it ranks them exactly as the counts do.

IDF affects the order only when a query has more than one term. It decides how much each query term contributes relative to the others. In best car insurance, it ensures that a match on insurance counts for more than a match on best.

Points that are easy to confuse

  • TF belongs to a term in a document; IDF belongs to a term in a collection. A term has one IDF, shared by every document, and a TF-IDF weight for every document. Adding a document to the collection can change the IDF, and so the weights, of terms in documents that were not touched.
  • IDF is not a stop list. A term in every document gets weight zero, which acts like removing it. But a common term that misses a few documents keeps a small positive weight. In the running example best is in three of the four documents and still counts 0.125 per occurrence.
  • The query side and the document side are separate choices. The score above weights documents by TF-IDF and counts each query term once. Other schemes put IDF only on the query side, or on both. Variants of TF-IDF names these choices.
  • "TF-IDF" names a family. Libraries differ in the base of the logarithm, in smoothing (for example $\log \frac{1 + N}{1 + \mathit{df}_t} + 1$), and in whether term frequency is dampened. Two implementations can give different numbers for the same data. The ideas are the same.

Why IDF works

IDF was introduced as an empirical observation. Several later arguments have tried to explain why it works, and Robertson (2004) reviews them:

  • Information theory. If a term occurs in a fraction $\mathit{df}_t / N$ of documents, then $\log(N/\mathit{df}_t)$ is the information, in the sense of Shannon, gained on learning that a document contains it. Rare terms are surprising and therefore informative. Robertson finds justifications along these lines problematic, and regards the probabilistic argument as the sounder one.
  • Probability of relevance. The Binary Independence Model derives a term weight from the probabilities that a term appears in relevant and nonrelevant documents. When nothing is known about which documents are relevant, the weight reduces to approximately $\log(N/\mathit{df}_t)$. IDF is what the probabilistic model prescribes in the absence of relevance information.
  • Discrimination. A term that occurs in nearly every document divides the collection into one large group and a few exceptions, which does little to separate relevant from nonrelevant documents. A term in few documents divides it sharply.

The information-theoretic reading, worked

Pick a document at random from Reuters. The probability that it contains car is $18{,}165 / 806{,}791 \approx 0.0225$. Learning that it does is a moderately surprising event. Shannon's measure of the surprise, $-\log_2 0.0225$, is 5.47 bits. That is the IDF of car measured in bits instead of powers of ten: $1.648 \times \log_2 10 \approx 5.47$. The right-hand axis of the IDF figure gives every term's weight in these units.

The reading also explains why IDFs are added. Suppose occurrences of different terms were independent. Then the probability that a random document contains both car and insurance would be the product $0.0225 \times 0.0238 \approx 0.000537$. The information in that joint event would be the sum $1.648 + 1.623 = 3.270$ in base 10, since the logarithm turns the product into a sum. Summing IDFs over the matched query terms therefore ranks a document by how improbable its pattern of matches would be by chance.

The reading has two weak points. The independence assumption is plainly false for car and insurance, and the argument says nothing about relevance. The probabilistic reading below starts from relevance instead.

The probabilistic reading, in one line

The Binary Independence Model, without relevance information, gives each matching term the weight $\log \frac{N - \mathit{df}_t}{\mathit{df}_t}$, when the probability that a relevant document contains the term is taken as 1/2 and the probability for a nonrelevant one as $\mathit{df}_t/N$ (the derivation is in its article). The two weights differ by $\log \frac{N}{\mathit{df}_t} - \log \frac{N - \mathit{df}_t}{\mathit{df}_t} = \log \frac{N}{N - \mathit{df}_t} = -\log\!\left(1 - \frac{\mathit{df}_t}{N}\right)$, which is close to zero when $\mathit{df}_t$ is a small fraction of $N$. For car in Reuters the fraction is 0.0225, and the two weights are 1.638 and 1.648.

They part company for common terms. A term in half the documents gets IDF $\log 2 \approx 0.301$, but probabilistic weight $\log 1 = 0$. A term in more than half gets a negative probabilistic weight, which practical systems clip at zero. This clipped form is the "probabilistic IDF" option of the SMART notation in variants of TF-IDF.

These arguments, the probabilistic one above all, suggest why TF-IDF works: it approximates quantities that more principled models derive. That is one reason it has lasted. Its components survive, in refined forms, in BM25 and in many features used by learned rankers, where the LETOR benchmark lists term frequency, IDF and their product, each summed over the query terms, as separate features. A third line of argument does not derive the weight at all. It states conditions that any reasonable weight should meet and tests formulas against them, and it is the subject of constraints on term weighting.

Worked problems

The first problem is Exercise 6.10 of the course text (Manning, Raghavan, and Schütze, 2008), restated with its data. The other two are posed on this topic's running example. All numbers are computed by tools/experiments/vsm/running_example.py.

Problem 1 (Exercise 6.10 of the course text). Three documents have the term frequencies below. Using the Reuters IDF values of the table above rounded to two decimals (car 1.65, auto 2.08, insurance 1.62, best 1.5), compute the TF-IDF weight of each term in each document.

Term Doc1 Doc2 Doc3
car 27 4 24
auto 3 33 0
insurance 0 33 29
best 14 0 17

Solution. Each weight is the count times the term's IDF, $\text{tf-idf}_{t,d} = \mathit{tf}_{t,d} \times \mathit{idf}_t$:

Term IDF Doc1 Doc2 Doc3
car 1.65 27 × 1.65 = 44.55 4 × 1.65 = 6.60 24 × 1.65 = 39.60
auto 2.08 3 × 2.08 = 6.24 33 × 2.08 = 68.64 0
insurance 1.62 0 33 × 1.62 = 53.46 29 × 1.62 = 46.98
best 1.5 14 × 1.5 = 21.00 0 17 × 1.5 = 25.50

The counts of auto and insurance in Doc2 are equal, 33 each, but auto is the rarer term in Reuters, so its weight is larger, 68.64 against 53.46. For the query car insurance, the score of this article adds the two query rows: 44.55 for Doc1, $6.60 + 53.46 = 60.06$ for Doc2 and $39.60 + 46.98 = 86.58$ for Doc3. Doc3, the only document that uses both terms heavily, ranks first. Cosine similarity returns to these three vectors and normalizes them.

Problem 2 (another query). Rank the four documents of the running example for the query auto insurance, by the overlap score and by the TF-IDF score.

Solution. Both scores rank $d_3$ first, $d_1$ and $d_2$ tied behind it, and $d_4$ last with a score of zero. The counts of auto are 0, 1, 3, 0 and of insurance 1, 0, 2, 0, and both terms have IDF 0.301:

Document auto + insurance Overlap score TF-IDF score
$d_1$ 0 + 1 1 0 + 0.301 = 0.301
$d_2$ 1 + 0 1 0.301 + 0 = 0.301
$d_3$ 3 + 2 5 0.903 + 0.602 = 1.505
$d_4$ 0 + 0 0 0

Because the two query terms have the same IDF, every TF-IDF score is 0.301 times the overlap score, and the two rankings must agree. That is the one-term remark above in a second form: IDF changes an order only through differences between the query terms' IDF values. The auto-insurance page is first, as a reader would want. The headline $d_1$, which is about car insurance, ties with the car review, because each contains exactly one of the two words once.

Problem 3 (a document is added). A fifth document, $d_5$, with car twice and insurance and best once each, joins the running example. Recompute the IDF of every term and the TF-IDF scores for best car insurance.

Solution. Every IDF changes, and the scores of the four old documents change with them although their text did not. Now $N = 5$, and $d_5$ raises the document frequencies of best, car and insurance by one:

Term $\mathit{df}_t$ before $\mathit{idf}_t$ before $\mathit{df}_t$ after $\mathit{idf}_t$ after
auto 2 $\log_{10}(4/2) = 0.301$ 2 $\log_{10}(5/2) = 0.398$
best 3 0.125 4 $\log_{10}(5/4) = 0.097$
car 3 0.125 4 0.097
insurance 2 0.301 3 $\log_{10}(5/3) = 0.222$
loan 1 0.602 1 $\log_{10}(5/1) = 0.699$
rate 2 0.301 2 0.398

The terms $d_5$ does not contain became rarer relative to the collection, so their IDF rose. The terms it contains became more common, and theirs fell. The scores, from unrounded IDFs:

Document best + car + insurance Score before Score after
$d_1$ 0 + 1 × 0.097 + 1 × 0.222 0.426 0.319
$d_2$ 2 × 0.097 + 5 × 0.097 + 0 0.875 0.678
$d_3$ 1 × 0.097 + 0 + 2 × 0.222 0.727 0.541
$d_4$ 2 × 0.097 + 3 × 0.097 + 0 0.625 0.485
$d_5$ 1 × 0.097 + 2 × 0.097 + 1 × 0.222 0.513

The ranking is $d_2$, $d_3$, $d_5$, $d_4$, $d_1$. The four old documents keep their order here, but an added document can reverse the order of two documents it does not touch. The ratio of the IDF of insurance to that of car moved from $0.301/0.125 = 2.41$ to $0.222/0.097 = 2.29$. A document with car seven times and one with insurance three times would score $7 \times 0.125 = 0.875$ and $3 \times 0.301 = 0.903$ before the addition, and $7 \times 0.097 = 0.678$ and $3 \times 0.222 = 0.666$ after it, so the pair changes places. This is why a system that stores TF-IDF weights must either recompute them as the collection changes or keep IDF out of the stored document weights, as the lnc.ltc scheme of variants of TF-IDF does.

What we have learned

A TF-IDF weight multiplies how often a term occurs in a document by how rare the term is in the collection. On the running example, IDF lifted the auto-insurance page from third to second, because insurance is the rarer query term. IDF can be read as the information in a match, and it approximates the probabilistic weight without relevance information, the sounder of the two justifications.

Raw counts still let the long car review win, and they leave the short headline $d_1$, the best match, in last place. The weights do not yet account for how long a document is. The next article, Cosine Similarity, treats documents as vectors of these weights and compares their directions instead of their sizes.

Every number in the running example and the Reuters examples is computed by tools/experiments/vsm/running_example.py in the site repository, and the figures by tools/figures/tf-idf.py.

References

  • Manning, C. D., Raghavan, P., and Schütze, H. (2008). Introduction to Information Retrieval, sections 6.2 and 6.4. Cambridge University Press. record · 102 pages
  • Robertson, S. (2004). Understanding inverse document frequency: On theoretical arguments for IDF. Journal of Documentation, 60(5), 503–520. record
  • Spärck Jones, K. (1972). A statistical interpretation of term specificity and its application in retrieval. Journal of Documentation, 28(1), 11–21. record · 2 pages

Reviewed: verified, 16 Nov 2026 · citations checked, 16 Nov 2026 · editorial review, 23 Nov 2026 · 8 revisions