Study Vault Search notes, aliases and full text
Graph
ConceptDBISAI-written · not yet proofread

TF-IDF

Also tf.idf · tfidf · Inverse Document Frequency · idf

tf.idf is the score of a word w in a document D, tf(w, D) · log2(N / df_w): the term frequency weighted by the inverse document frequency, so that words occurring in many documents count little.

Formula#

Term Frequency (tf)#

tf(w,D)=number of occurrences of w in D.\mathrm{tf}(w, D) = \text{number of occurrences of } w \text{ in } D.

A word that occurs often in DD is likely important for DD. Used alone as a score, tf lets frequent function words (of, the) dominate the score sum, see Term Frequency.

Inverse Document Frequency (idf)#

idfw=log⁡2Ndfw\mathrm{idf}_w = \log_2 \frac{N}{\mathrm{df}_w}
  • NN: total number of documents in the collection.
  • dfw\mathrm{df}_w: number of documents containing ww. In an Inverted Index this is simply the length of the inverted list of ww.

idf is a proxy for the rarity of a word: the more documents contain ww, the smaller idfw\mathrm{idf}_w. A word in every document gets idf=log⁡21=0\mathrm{idf} = \log_2 1 = 0; a word in half the documents gets idf=1\mathrm{idf} = 1. The log⁡2\log_2 damps the effect: without it, small differences in df\mathrm{df} would have too much influence.

Term-document matrix and ranking#

Arranging the tf.idf scores of all terms and documents gives the term-document matrix AA of the Vector Space Model, with rows = terms, columns = documents:

Aij=tf.idf(ti,Dj).A_{ij} = \text{tf.idf}(t_i, D_j).

For a query vector qq (1 for each query term, 0 otherwise), the score of document DjD_j is the dot product

score(Dj)=qTA:,j=∑iqi Aij,\mathrm{score}(D_j) = q^T A_{:,j} = \sum_i q_i \, A_{ij},

which is the same as summing the posting scores while merging the inverted lists of the query words. Documents are ranked by descending score. AA is typically sparse.

Properties#

  • tf.idf(w,D)=0\text{tf.idf}(w, D) = 0 if w∉Dw \notin D (since tf=0\mathrm{tf} = 0).

  • tf.idf(w,D)=0\text{tf.idf}(w, D) = 0 if ww occurs in every document (idf=log⁡21=0\mathrm{idf} = \log_2 1 = 0), i.e. such a word does not influence the ranking at all.

  • The idf part is fine; the tf part has two problems (DBIS_VL02 > tf.idf):

    1. Length: longer documents tend to have larger tf, without being more “about” ww.
    2. Linearity: doubling tf makes the document only a little more “about” ww, not twice as much.

    BM25 fixes both by replacing tf\mathrm{tf} with a saturating, length-normalized tf∗\mathrm{tf}^* and keeps the same idf factor.

Example#

Sources#

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only