Reference articles on history, science, culture and more
Encyclopedia

Tf-idf

Estimate of the importance of a word in a document

In information retrieval, tf-idf (term frequency-inverse document frequency, TF*IDF, TFIDF, TF-IDF, or Tf-idf) is a measure of importance of a word to a document in a collection or corpus, adjusted for the fact that some words appear more frequently in general. Like the bag-of-words model, it models a document as a multiset of words, without word order. It is a refinement over the simple bag-of-words model, by allowing the weight of words to depend on the rest of the corpus.

It was often used as a weighting factor in searches of information retrieval, text mining, and user modeling. A survey conducted in 2015 showed that 83% of text-based recommender systems in digital libraries used tf-idf. Variations of the tf-idf weighting scheme were often used by search engines as a central tool in scoring and ranking a document's relevance given a user query.

One of the simplest ranking functions is computed by summing the tf-idf for each query term; many more sophisticated ranking functions are variants of this simple model.

01Motivations

Karen Spärck Jones (1972) conceived a statistical interpretation of term-specificity called Inverse Document Frequency (idf), which became a cornerstone of term weighting:

The specificity of a term can be quantified as an inverse function of the number of documents in which it occurs.

For example, the df (document frequency) and idf for some words in Shakespeare's 37 plays might be represented as follows:

Word df idf
Romeo 1 1.57
salad 2 1.27
Falstaff 4 0.966
forest 12 0.489
battle 21 0.246
wit 34 0.037
fool 36 0.012
good 37 0
sweet 37 0

In the example above, "Romeo", "Falstaff", and "salad" appears in very few plays; since only a handful of plays contain them, their appearance would significantly narrow down the possible list of plays. In contrast, "good" and "sweet" appear in every play and are completely uninformative as to which play it is.

Plot of different inverse document frequency functions: standard, smooth, probabilistic.
Plot of different inverse document frequency functions: standard, smooth, probabilistic.

02Definition

  1. The tf-idf is the product of two statistics, term frequency and inverse document frequency. There are various ways for determining the exact values of both statistics.
  2. A formula that aims to define the importance of a keyword or phrase within a document or a web page.
Variants of term frequency (tf) weight
weighting scheme tf weight
binary{0,1}
raw countf_{t,d}
term frequency f_{t,d}{\Bigg /}{\sum _{t'\in d}{f_{t',d}}}
log normalization\log(1+f_{t,d})
double normalization 0.50.5+0.5\cdot {\frac {f_{t,d}}{\max _{\{t'\in d\}}{f_{t',d}}}}
double normalization KK+(1-K){\frac {f_{t,d}}{\max _{\{t'\in d\}}{f_{t',d}}}}

Term frequency

Term frequency, tf(t,d), is the relative frequency of term t within document d,

\mathrm {tf} (t,d)={\frac {f_{t,d}}{\sum _{t'\in d}{f_{t',d}}}},

where ft,d is the raw count of a term in a document, i.e., the number of times that term t occurs in document d. Note the denominator is simply the total number of terms in document d (counting each occurrence of the same term separately). There are various other ways to define term frequency:

  • the raw count itself: tf(t,d) = ft,d
  • Boolean "frequencies": tf(t,d) = 1 if t occurs in d and 0 otherwise;
  • logarithmically scaled frequency: tf(t,d) = log (1 + ft,d);
  • augmented frequency, to prevent a bias towards longer documents, e.g. raw frequency divided by the raw frequency of the most frequently occurring term in the document:
\mathrm {tf} (t,d)=0.5+0.5\cdot {\frac {f_{t,d}}{\max\{f_{t',d}:t'\in d\}}}

Inverse document frequency

Variants of inverse document frequency (idf) weight
weighting scheme idf weight (n_{t}=|\{d\in D:t\in d\}|)
unary1
inverse document frequency\log {\frac {N}{n_{t}}}=-\log {\frac {n_{t}}{N}}
inverse document frequency smooth\log \left({\frac {N}{1+n_{t}}}\right)+1
inverse document frequency max\log \left({\frac {\max _{\{t'\in d\}}n_{t'}}{1+n_{t}}}\right)
probabilistic inverse document frequency\log {\frac {N-n_{t}}{n_{t}}}

The inverse document frequency is a measure of how much information the word provides, i.e., how common or rare it is across all documents. It is the logarithmically scaled inverse fraction of the documents that contain the word (obtained by dividing the total number of documents by the number of documents containing the term, and then taking the logarithm of that quotient):

\mathrm {idf} (t,D)=\log {\frac {N}{n_{t}}}

with

  • D: is the set of all documents in the corpus
  • N={|D|}: total number of documents in the corpus
  • n_{t}=|\{d\in D:t\in d\}| : number of documents where the term t appears (i.e., \mathrm {tf} (t,d)\neq 0). If the term is not in the corpus, this will lead to a division-by-zero. It is therefore common to adjust the numerator to 1+N and the denominator to 1+n_{t}.

Term frequency-inverse document frequency

Variants of term frequency-inverse document frequency (tf-idf) weights
weighting scheme tf-idf
count-idf f_{t,d}\cdot \log {\frac {N}{n_{t}}}
double normalization-idf \left(0.5+0.5{\frac {f_{t,q}}{\max _{t}f_{t,q}}}\right)\cdot \log {\frac {N}{n_{t}}}
log normalization-idf (1+\log f_{t,d})\cdot \log {\frac {N}{n_{t}}}

Then tf-idf is calculated as

\mathrm {tfidf} (t,d,D)=\mathrm {tf} (t,d)\cdot \mathrm {idf} (t,D)

A high weight in tf-idf is reached by a high term frequency (in the given document) and a low document frequency of the term in the whole collection of documents; the weights hence tend to filter out common terms. Since the ratio inside the idf's log function is always greater than or equal to 1, the value of idf (and tf-idf) is greater than or equal to 0. As a term appears in more documents, the ratio inside the logarithm approaches 1, bringing the idf and tf-idf closer to 0.

03Justification of idf

Idf was introduced as "term specificity" by Karen Spärck Jones in a 1972 paper. Although it has worked well as a heuristic, its theoretical foundations have been troublesome for at least three decades afterward, with many researchers trying to find information theoretic justifications for it.

Spärck Jones's own explanation did not propose much theory, aside from a connection to Zipf's law. Attempts have been made to put idf on a probabilistic footing, by estimating the probability that a given document d contains a term t as the relative document frequency,

P(t|D)={\frac {|\{d\in D:t\in d\}|}{N}},

so that we can define idf as

{\begin{aligned}\mathrm {idf} &=-\log P(t|D)\\&=\log {\frac {1}{P(t|D)}}\\&=\log {\frac {N}{|\{d\in D:t\in d\}|}}\end{aligned}}

Namely, the inverse document frequency is the logarithm of "inverse" relative document frequency.

This probabilistic interpretation in turn takes the same form as that of self-information. However, applying such information-theoretic notions to problems in information retrieval leads to problems when trying to define the appropriate event spaces for the required probability distributions: not only documents need to be taken into account, but also queries and terms.

06Example of tf-idf

Suppose that we have term count tables of a corpus consisting of only two documents:

"This is a sample A."
"This is another example, another example, example."
Document 1 (d_{1})
Term Term Count
this1
is 1
a 2
sample 1
Document 2 (d_{2})
Term Term Count
this1
is 1
another 2
example 3

The calculation of tf-idf for the term "this" is performed as follows:

In its raw frequency form, tf is just the frequency of the "this" for each document. In each document, the word "this" appears once; but as the document 2 has more words, its relative frequency is smaller.

\mathrm {tf} ({\mathsf {''this''}},d_{1})={\frac {1}{5}}=0.2
\mathrm {tf} ({\mathsf {''this''}},d_{2})={\frac {1}{7}}\approx 0.14

An idf is constant per corpus, and accounts for the ratio of documents that include the word "this". In this case, we have a corpus of two documents and all of them include the word "this".

\mathrm {idf} ({\mathsf {''this''}},D)=\log \left({\frac {2}{2}}\right)=0

So tf-idf is zero for the word "this", which implies that the word is not very informative as it appears in all documents.

\mathrm {tfidf} ({\mathsf {''this''}},d_{1},D)=0.2\times 0=0
\mathrm {tfidf} ({\mathsf {''this''}},d_{2},D)=0.14\times 0=0

The word "example" is more interesting - it occurs three times, but only in the second document:

\mathrm {tf} ({\mathsf {''example''}},d_{1})={\frac {0}{5}}=0
\mathrm {tf} ({\mathsf {''example''}},d_{2})={\frac {3}{7}}\approx 0.429
\mathrm {idf} ({\mathsf {''example''}},D)=\log \left({\frac {2}{1}}\right)=0.301

Finally,

\mathrm {tfidf} ({\mathsf {''example''}},d_{1},D)=\mathrm {tf} ({\mathsf {''example''}},d_{1})\times \mathrm {idf} ({\mathsf {''example''}},D)=0\times 0.301=0
\mathrm {tfidf} ({\mathsf {''example''}},d_{2},D)=\mathrm {tf} ({\mathsf {''example''}},d_{2})\times \mathrm {idf} ({\mathsf {''example''}},D)=0.429\times 0.301\approx 0.129

(using the base 10 logarithm).

07Beyond terms

The idea behind tf-idf also applies to entities other than terms. In 1998, the concept of idf was applied to citations. The authors argued that "if a very uncommon citation is shared by two documents, this should be weighted more highly than a citation made by a large number of documents". In addition, tf-idf was applied to "visual words" with the purpose of conducting object matching in videos, and entire sentences. However, the concept of tf-idf did not prove to be more effective in all cases than a plain tf scheme (without idf). When tf-idf was applied to citations, researchers could find no improvement over a simple citation-count weight that had no idf component.

08Derivatives

A number of term-weighting schemes have derived from tf-idf. One of them is TF-PDF (term frequency * proportional document frequency). TF-PDF was introduced in 2001 in the context of identifying emerging topics in the media. The PDF component measures the difference of how often a term occurs in different domains. Another derivate is TF-IDuF. In TF-IDuF, idf is not calculated based on the document corpus that is to be searched or recommended. Instead, idf is calculated on users' personal document collections. The authors report that TF-IDuF was equally effective as tf-idf but could also be applied in situations when, e.g., a user modeling system has no access to a global document corpus. The DELTA TF-IDF derivative uses the difference in importance of a term across two specific classes, like positive and negative sentiment. For example, it can assign a high score to a word like "excellent" in positive reviews and a low score to the same word in negative reviews. This helps identify words that strongly indicate the sentiment of a document, potentially leading to improved accuracy in text classification tasks.

Watch videos about Tf-idfExplainers and documentaries on YouTube (opens in a new tab)

Sources and credits

This article is adapted from the Wikipedia article Tf-idf, written by its contributors and licensed under CC BY-SA 4.0. Fathomly has changed the layout, removed citation markers, navigation and maintenance notices, and adjusted punctuation. This adapted version is shared under the same license. For references, see the original article.

Images, from Wikimedia Commons:

Fathomly is not affiliated with or endorsed by the Wikimedia Foundation. Spotted a problem? Tell us.

Continue exploring

Related topics

Word embedding

In natural language processing, a word embedding is a representation of a word. The embedding is used in text analysis.

Kullback-Leibler divergence

In mathematical statistics, the Kullback-Leibler divergence (also called relative entropy and I-divergence), denoted D KL ( P ∥ Q ) {\displaystyle D_{\text{KL}}(P\parallel Q)} , is a type of statistical distance: a measure of how much an approximating probability distribution Q is different from a true probability distribution P. Mathematically, it is defined as D KL ( P ∥ Q ) = ∑ x ∈ X P ( x ) log ⁡ P ( x ) Q ( x ) . {\displaystyle D_{\text{KL}}(P\parallel Q)=\sum _{x\in {\mathcal {X}}}P(x)\,\log {\frac {P(x)}{Q(x)}}{\text{.}}} A simple interpretation of the KL divergence of P from Q is the expected excess surprisal from using the approximation Q instead of P when the actual is P. While it is a measure of how different two distributions are and is thus a distance in some sense, it is not actually a metric, which is the most familiar and formal type of distance.

Latent Dirichlet allocation

In natural language processing, latent Dirichlet allocation is a generative statistical model that explains how a collection of text documents can be described by a set of unobserved "topics." For example, given a set of news articles, LDA might discover that one topic is characterized by words like "president", "government", and "election", while another is characterized by "team", "game", and "score". It is one of the most common topic models.