Algorithm Search⌘K

Techniqueintro

tf-idf weighting

Also called term frequency-inverse document frequency, tf.idf.

Weights a term in a document by its frequency there times the log of the inverse fraction of documents that contain it.

Tf-idf weighting gives a term tt in a document dd the weight tf(t,d)⋅log⁡(N/df(t))\mathrm{tf}(t, d) \cdot \log(N / \mathrm{df}(t)), where tf\mathrm{tf} counts the occurrences of tt in dd, NN is the number of documents and df(t)\mathrm{df}(t) is the number of documents that contain tt. A term counts more when it occurs often in the document and less when it occurs in many documents of the collection. The idf factor is due to Sparck Jones (1972), and the weights are the coordinates of the vector space model and the starting point of BM25.

In plain words

A word tells more about a document when the document uses it many times. A word tells less when almost every document uses it, like "the" or "a", because it does not separate one document from the others. Tf-idf multiplies the two effects: how often the word occurs in this document, and a factor that grows as the word gets rarer in the whole collection. The factor is a logarithm, so a word in half of the documents gets a small factor and a word in only one document gets the largest one.

Most systems also damp the count, so that ten uses of a word do not count ten times as much as one use. The weights then go into a sum over the query words, or into a vector for a cosine.

When it applies

Use tf-idf when:

  • documents and queries are bags of words, and the order of words does not matter for the task;
  • the score of a document for a query can be a sum over the query terms of a weight for each term;
  • there is no information about which documents are relevant, so the weight must come from statistics of the collection;
  • the statistics tf\mathrm{tf} and df\mathrm{df} are cheap to get, as they are from an inverted index, which stores tf\mathrm{tf} in each posting and df\mathrm{df} as the length of each list.

Notation: NN documents, tf(t,d)\mathrm{tf}(t, d) occurrences of tt in dd, df(t)\mathrm{df}(t) documents that contain tt, qtf(t,q)\mathrm{qtf}(t, q) occurrences of tt in the query.

The template

  1. TFIDF-VECTOR(d, N, df)
  2. 1for each distinct term t of d do
  3. 2a ← TF(tf(t, d)) ▷ hole: the term frequency part
  4. 3g ← IDF(N, df(t)) ▷ hole: the document frequency part
  5. 4w[t] ← a · g
  6. 5return NORM(w) ▷ hole: the normalization

The score of dd for qq is then s(q,d)=∑twq[t]⋅wd[t]s(q, d) = \sum_{t} w_q[t] \cdot w_d[t], where wqw_q comes from the same template applied to the query, possibly with other choices for the holes. The SMART notation names a choice by one letter for each hole, written for the document and then for the query, as in lnc.ltc (Manning, Raghavan and Schuetze, Section 6.4.3). With log⁡\log to one fixed base:

letter TF (L2) letter IDF (L3) letter NORM (L5)
n tf\mathrm{tf} n 11 n none
l 1+log⁡tf1 + \log \mathrm{tf} for tf>0\mathrm{tf} > 0, else 0 t log⁡(N/df)\log(N / \mathrm{df}) c divide by ∑tw[t]2\sqrt{\sum_t w[t]^2}
a 0.5+0.5 tf/max⁡utf(u,d)0.5 + 0.5\,\mathrm{tf} / \max_u \mathrm{tf}(u, d) p max⁡(0,log⁡((N−df)/df))\max(0, \log((N - \mathrm{df}) / \mathrm{df})) u pivoted unique
b 1 if tf>0\mathrm{tf} > 0, else 0 b byte size
L (1+log⁡tf)/(1+log⁡aveutf(u,d))(1 + \log \mathrm{tf}) / (1 + \log \mathrm{ave}_u \mathrm{tf}(u, d))

Walk-through on the toy collection. The toy collection of the information retrieval pages has N=8N = 8. With log⁡10\log_{10}, the idf is log⁡10(8/4)=0.301\log_{10}(8/4) = 0.301 for "sorted", log⁡10(8/3)=0.426\log_{10}(8/3) = 0.426 for "array", log⁡10(8/2)=0.6021\log_{10}(8/2) = 0.6021 for "search" and log⁡10(8/5)=0.2041\log_{10}(8/5) = 0.2041 for "a". Document d2d_2, "merge sort merges two sorted runs into one sorted array", holds "sorted" twice. Its weight for "sorted" is 2⋅0.301=0.6022 \cdot 0.301 = 0.602 in the n-t scheme and (1+log⁡102)⋅0.301=1.301⋅0.301=0.3916(1 + \log_{10} 2) \cdot 0.301 = 1.301 \cdot 0.301 = 0.3916 in the l-t scheme.

Score q1 = "sorted array search" with ntn weights for the documents and the weight 1 for each query term, so that s(q,d)=∑t∈qtf(t,d)log⁡10(N/df(t))s(q, d) = \sum_{t \in q} \mathrm{tf}(t, d) \log_{10}(N / \mathrm{df}(t)):

rank document sum score
1 d1d_1 0.301+0.426+0.60210.301 + 0.426 + 0.6021 1.329
2 d2d_2 2⋅0.301+0.4262 \cdot 0.301 + 0.426 1.028
3 d8d_8 0.301+0.4260.301 + 0.426 0.727
4 d7d_7 0.60210.6021 0.602
5 d4d_4 0.3010.301 0.301

The cosine-normalized lnc.ltc scores of the same query are worked out on the vector space model page. They give the same order on this query.

Classic examples

  • Vector space model. The tf-idf weights are the coordinates of the document and query vectors, and the cosine is the score. The standard choice is lnc.ltc.
  • Binary independence model. With no relevance information, the relevance weight of a term becomes log⁡((N−df)/df)\log((N - \mathrm{df}) / \mathrm{df}), the p letter above, so idf comes out of a probabilistic derivation (Croft and Harper 1979, Robertson 2004).
  • Pivoted length normalization. It replaces the cosine normalization at L5 by a normalization tilted around the average, which is the u letter.
  • BM25. It keeps an idf at L3 and replaces the linear tf at L2 by a function that saturates and depends on the length of the document.
  • Latent semantic analysis. It takes the term-document matrix of tf-idf weights and replaces it by a low-rank approximation.

Variations

  • Sublinear tf. 1+log⁡tf1 + \log \mathrm{tf} (the l letter) stops a word that occurs 20 times from counting 20 times as much as a word that occurs once. BM25 uses the saturating form tf(k1+1)/(tf+k1)\mathrm{tf}(k_1 + 1)/(\mathrm{tf} + k_1), which has a bound.
  • Smoothed idf. Forms such as log⁡((N+1)/df)\log((N + 1)/\mathrm{df}), log⁡(1+N/df)\log(1 + N / \mathrm{df}) and ln⁡(1+(N−df+0.5)/(df+0.5))\ln(1 + (N - \mathrm{df} + 0.5)/(\mathrm{df} + 0.5)), the last one in Lucene's BM25, stay positive for a term that occurs in every document.
  • Probabilistic idf. log⁡((N−df)/df)\log((N - \mathrm{df})/\mathrm{df}), or ln⁡((N−df+0.5)/(df+0.5))\ln((N - \mathrm{df} + 0.5)/(\mathrm{df} + 0.5)) with the 0.5 corrections, is 0 for a term in half of the documents and negative above.
  • Maximum tf normalization. The a letter divides by the largest tf in the document, so that long documents do not get higher weights only because they repeat all their words.
  • Collection frequency. Some weights use the total count cf(t)\mathrm{cf}(t) in place of df(t)\mathrm{df}(t), as the basic models of divergence from randomness do.

Pitfalls

  • The log base. With the n letter for tf on both sides, a change of base multiplies every score by one constant and does not change the order. With the l letter it does change the weights, because 1+log⁡10tf1 + \log_{10} \mathrm{tf} and 1+ln⁡tf1 + \ln \mathrm{tf} are not proportional. Always say which base is used.
  • Terms in every document. With log⁡(N/df)\log(N / \mathrm{df}), a term with df=N\mathrm{df} = N gets weight 0 and disappears from the score. With the p letter, every term in at least half of the documents gets 0, because of the clamp at 0. In the toy collection these are "sorted" (df=4\mathrm{df} = 4) and "a" (df=5\mathrm{df} = 5).
  • Unknown query terms. A query term with df=0\mathrm{df} = 0 makes log⁡(N/df)\log(N / \mathrm{df}) infinite. Drop such terms or smooth the df.
  • Statistics from the wrong collection. On an index split into shards, each shard has its own NN and df\mathrm{df}, so the same document can get different scores on different shards. Use global statistics, or accept the difference.
  • Raw tf and spam. With the n letter, a page that repeats a word many times can push itself to the top of the ranking. A sublinear or saturating tf limits this.

Exercises

  1. Compute the ltc weight of "array" in d8d_8 = "the sorted array of ids is compressed with gap encoding", before normalization, with log⁡10\log_{10}. Answer: (1+log⁡101)⋅log⁡10(8/3)=0.426(1 + \log_{10} 1) \cdot \log_{10}(8/3) = 0.426.
  2. Show that with the scheme ntn.ntn a change of the log base from 10 to ee multiplies every score by (ln⁡10)2(\ln 10)^2, so the ranking does not change. Hint: each score is a sum of terms qtf⋅idf⋅tf⋅idf\mathrm{qtf} \cdot \mathrm{idf} \cdot \mathrm{tf} \cdot \mathrm{idf}.
  3. Let d′d' be the document made of two copies of d1d_1. Show that the ntc vectors of d′d' and d1d_1 are equal, but their ltc vectors are not. Hint: the n letter doubles every coordinate, and the l letter maps 1 to 1 and 2 to 1.301.
  4. Which terms of the toy collection have a p idf of 0? Answer: those with df≥4\mathrm{df} \ge 4, which are "a" and "sorted".

References

  1. Karen Sparck Jones, A statistical interpretation of term specificity and its application in retrieval, Journal of Documentation 28(1), 11 to 21, 1972, the weighting of a query term by a function of the number of documents that contain it, the origin of idf
  2. Christopher D. Manning, Prabhakar Raghavan and Hinrich Schuetze, Introduction to Information Retrieval, Cambridge University Press, 2008, Chapter 6 (Section 6.2 on term frequency and inverse document frequency, Section 6.4 on sublinear tf scaling, maximum tf normalization and the SMART notation of Section 6.4.3)
  3. Gerard Salton and Christopher Buckley, Term-weighting approaches in automatic text retrieval, Information Processing and Management 24(5), 513 to 523, 1988, the comparison of many combinations of tf, collection frequency and normalization components on several test collections
  4. Stephen Robertson, Understanding inverse document frequency, on theoretical arguments for IDF, Journal of Documentation 60(5), 503 to 520, 2004, idf as an information measure and as the relevance weight of the probabilistic model with no relevance information

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • five exercises