Introduction to TF-IDF (Term Frequency–Inverse Document Frequency)

Introduction

TF-IDF (Term Frequency–Inverse Document Frequency) is a statistical method widely used in information retrieval and text mining to evaluate the importance of a word in a document. It combines two key concepts: term frequency (TF) and inverse document frequency (IDF).

Term Frequency (TF)

Definition: Term frequency is the number of times a term appears in a document. To prevent document length from affecting the result, term frequency is usually normalized.

Formula: \(\text{TF}(t, d) = \frac{\text{number of times term }t\text{ appears in document }d}{\text{total number of terms in document }d}\)

For example, if apple appears 5 times in document d, and document d contains 100 terms in total: \(\text{TF}(apple, d) = \frac{5}{100} = 0.05\)

Inverse Document Frequency (IDF)

Definition: Inverse document frequency measures how generally important a term is across an entire document collection. If a term appears in many documents, its IDF value is low. If it appears in only a few documents, its IDF value is high.

Formula: \(\text{IDF}(t) = \log\left(\frac{\text{total number of documents}}{\text{number of documents containing term }t + 1}\right)\)

The +1 prevents the denominator from becoming zero.

For example, if there are 1,000 documents in total and apple appears in 100 of them: \(\text{IDF}(apple) = \log\left(\frac{1000}{100 + 1}\right) = \log(9.90099) \approx 0.996\)

TF-IDF

Definition: TF-IDF is the product of term frequency and inverse document frequency. It represents the importance of a term in a document.

Standard formula: \(\text{TF-IDF}(t, d) = \text{TF}(t, d) \times \text{IDF}(t)\)

Derivation

Combining the two parts above gives the complete TF-IDF formula:

\(\text{TF-IDF}(t, d) = \left( \frac{\text{number of times term }t\text{ appears in document }d}{\text{total number of terms in document }d} \right) \times \log\left(\frac{\text{total number of documents}}{\text{number of documents containing term }t + 1}\right)\)

Example

Assume a document collection contains 1,000 documents:

  • Document d contains 100 terms.
  • apple appears 5 times in document d.
  • apple appears in 100 documents.

Calculate the TF-IDF value of apple in document d:

  • Calculate TF: \(\text{TF}(apple, d) = \frac{5}{100} = 0.05\)
  • Calculate IDF: \(\text{IDF}(apple) = \log\left(\frac{1000}{100 + 1}\right) = \log(9.90099) \approx 0.996\)
  • Calculate TF-IDF: \(\text{TF-IDF}(apple, d) = 0.05 \times 0.996 \approx 0.0498\)

Variants and Optimizations

The standard TF-IDF formula above has several variants and practical optimizations:

  • Smoothing: To prevent IDF from becoming too large, some implementations add a small constant ( k ) to the IDF formula: \(\text{IDF}(t) = \log\left(\frac{\text{total number of documents}}{\text{number of documents containing term }t + k}\right)\) A common value for ( k ) is 1.
  • Different logarithm bases: Some implementations use the natural logarithm with base ( e ), while others use base 10 or 2. The base changes the numerical value of IDF but not its relative ordering.
  • Term-frequency normalization: In addition to a simple frequency ratio, other normalization methods can be used, such as square-root normalization: \(\text{TF}(t, d) = \sqrt{\frac{\text{number of times term }t\text{ appears in document }d}{\text{total number of terms in document }d}}\)

Applications

TF-IDF is widely used in text-analysis tasks, including:

  • Information retrieval: Improve the relevance of search results.
  • Text classification: Identify the topic or category of a document.
  • Keyword extraction: Find important terms in a document.
  • Document similarity: Compare the similarity of different documents.
What do you think?
0 Reactions
Pick a reaction