Trisha Shetty (Editor)

Tf–idf

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

In information retrieval, tf–idf, short for term frequency–inverse document frequency, is a numerical statistic that is intended to reflect how important a word is to a document in a collection or corpus. It is often used as a weighting factor in information retrieval, text mining, and user modeling. The tf-idf value increases proportionally to the number of times a word appears in the document, but is offset by the frequency of the word in the corpus, which helps to adjust for the fact that some words appear more frequently in general. Nowadays, tf-idf is one of the most popular term-weighting schemes. For instance, 83% of text-based recommender systems in the domain of digital libraries use tf-idf.

Contents

Variations of the tf–idf weighting scheme are often used by search engines as a central tool in scoring and ranking a document's relevance given a user query. tf–idf can be successfully used for stop-words filtering in various subject fields including text summarization and classification.

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.

Term frequency

Suppose we have a set of English text documents and wish to determine which document is most relevant to the query "the brown cow". A simple way to start out is by eliminating documents that do not contain all three words "the", "brown", and "cow", but this still leaves many documents. To further distinguish them, we might count the number of times each term occurs in each document and sum them all together; the number of times a term occurs in a document is called its term frequency.

The first form of term weighting is due to Hans Peter Luhn (1957) and is based on the Luhn Assumption:

  • The weight of a term that occurs in a document is simply proportional to the term frequency.
  • Inverse document frequency

    Because the term "the" is so common, term frequency will tend to incorrectly emphasize documents which happen to use the word "the" more frequently, without giving enough weight to the more meaningful terms "brown" and "cow". The term "the" is not a good keyword to distinguish relevant and non-relevant documents and terms, unlike the less common words "brown" and "cow". Hence an inverse document frequency factor is incorporated which diminishes the weight of terms that occur very frequently in the document set and increases the weight of terms that occur rarely.

    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.
  • Definition

    tf–idf is the product of two statistics, term frequency and inverse document frequency. Various ways for determining the exact values of both statistics exist.

    Term frequency

    In the case of the term frequency tf(t,d), the simplest choice is to use the raw frequency of a term in a document, i.e. the number of times that term t occurs in document d. If we denote the raw frequency of t by ft,d, then the simple tf scheme is tf(t,d) = ft,d. Other possibilities include

  • Boolean "frequencies": tf(t,d) = 1 if t occurs in d and 0 otherwise;
  • logarithmically scaled frequency: tf(t,d) = 1 + log ft,d, or zero if ft,d is zero;
  • augmented frequency, to prevent a bias towards longer documents, e.g. raw frequency divided by the maximum raw frequency of any term in the document:
  • t f ( t , d ) = 0.5 + 0.5 f t , d max { f t , d : t d }

    Inverse document frequency

    The inverse document frequency is a measure of how much information the word provides, that is, whether the term is common or rare 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.

    i d f ( t , D ) = log N | { d D : t d } |

    with

  • N : total number of documents in the corpus N = | D |
  • | { d D : t d } |  : number of documents where the term t appears (i.e., t f ( t , d ) 0 ). If the term is not in the corpus, this will lead to a division-by-zero. It is therefore common to adjust the denominator to 1 + | { d D : t d } | .
  • Term frequency–Inverse document frequency

    Then tf–idf is calculated as

    t f i d f ( t , d , D ) = t f ( t , d ) i d f ( 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.

    Justification 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 ) = | { d D : t d } | N ,

    so that we can define idf as

    i d f = log P ( t | d ) = log 1 P ( t | d ) = log N | { d D : t d } |

    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.

    Example of tf–idf

    Suppose that we have term count tables of a corpus consisting of only two documents, as listed on the right.

    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.

    t f ( t h i s , d 1 ) = 1 5 = 0.2 t f ( t h i s , d 2 ) = 1 7 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".

    i d f ( t h i s , D ) = log ( 2 2 ) = 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.

    t f i d f ( t h i s , d 1 ) = 0.2 × 0 = 0 t f i d f ( t h i s , d 2 ) = 0.14 × 0 = 0

    A slightly more interesting example arises from the word "example", which occurs three times but only in the second document:

    t f ( e x a m p l e , d 1 ) = 0 5 = 0 t f ( e x a m p l e , d 2 ) = 3 7 0.429 i d f ( e x a m p l e , D ) = log ( 2 1 ) = 0.301

    Finally,

    t f i d f ( e x a m p l e , d 1 ) = t f ( e x a m p l e , d 1 ) × i d f ( e x a m p l e , D ) = 0 × 0.301 = 0 t f i d f ( e x a m p l e , d 2 ) = t f ( e x a m p l e , d 2 ) × i d f ( e x a m p l e , D ) = 0.429 × 0.301 0.13

    (using the base 10 logarithm).

    tf-idf Beyond Terms

    The idea behind TF–IDF has also been applied 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, not in all cases the concept of TF–IDF proved to be more effective 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.

    tf-idf Derivates

    There are a number of term-weighting schemes that 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 based 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.

    References

    Tf–idf Wikipedia