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 in a document the weight , where counts the occurrences of in , is the number of documents and is the number of documents that contain . 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 and are cheap to get, as they are from an inverted index, which stores in each posting and as the length of each list.
Notation: documents, occurrences of in , documents that contain , occurrences of in the query.
The template
TFIDF-VECTOR(d, N, df)- 1
for each distinct term t of d do - 2
a ← TF(tf(t, d)) ▷ hole: the term frequency part - 3
g ← IDF(N, df(t)) ▷ hole: the document frequency part - 4
w[t] ← a · g - 5
return NORM(w) ▷ hole: the normalization
The score of for is then , where 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 to one fixed base:
| letter | TF (L2) | letter | IDF (L3) | letter | NORM (L5) |
|---|---|---|---|---|---|
| n | n | n | none | ||
| l | for , else 0 | t | c | divide by | |
| a | p | u | pivoted unique | ||
| b | 1 if , else 0 | b | byte size | ||
| L |
Walk-through on the toy collection. The toy collection of the information retrieval pages has . With , the idf is for "sorted", for "array", for "search" and for "a". Document , "merge sort merges two sorted runs into one sorted array", holds "sorted" twice. Its weight for "sorted" is in the n-t scheme and 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 :
| rank | document | sum | score |
|---|---|---|---|
| 1 | 1.329 | ||
| 2 | 1.028 | ||
| 3 | 0.727 | ||
| 4 | 0.602 | ||
| 5 | 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 , 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. (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 , which has a bound.
- Smoothed idf. Forms such as , and , the last one in Lucene's BM25, stay positive for a term that occurs in every document.
- Probabilistic idf. , or 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 in place of , 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 and are not proportional. Always say which base is used.
- Terms in every document. With , a term with 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" () and "a" ().
- Unknown query terms. A query term with makes infinite. Drop such terms or smooth the df.
- Statistics from the wrong collection. On an index split into shards, each shard has its own and , 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
- Compute the ltc weight of "array" in = "the sorted array of ids is compressed with gap encoding", before normalization, with . Answer: .
- Show that with the scheme ntn.ntn a change of the log base from 10 to multiplies every score by , so the ranking does not change. Hint: each score is a sum of terms .
- Let be the document made of two copies of . Show that the ntc vectors of and 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.
- Which terms of the toy collection have a p idf of 0? Answer: those with , which are "a" and "sorted".
References
- 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
- 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)
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag TRVGYD Edit this page History Report an error Markdown