Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S39L04 - Term frequency inverse document frequency

Machine Leaning Articles
1 views

S39L04 - Term frequency inverse document frequency


May 15, 2025

Understanding TF-IDF: Enhancing Text Analysis with Term Frequency-Inverse Document Frequency

In the realm of Natural Language Processing (NLP), effectively analyzing and understanding textual data is paramount. Among the myriad of techniques available, Term Frequency-Inverse Document Frequency (TF-IDF) stands out as a powerful tool for transforming text into meaningful numerical representations. This comprehensive guide delves deep into TF-IDF, exploring its fundamentals, advantages, and practical implementation using Python's Scikit-learn library.

Table of Contents

  1. What is TF-IDF?
  2. Why Use TF-IDF?
  3. How TF-IDF Works
  4. Implementing TF-IDF in Python
    1. Setting Up the Dataset
    2. Using CountVectorizer
    3. Applying TfidfVectorizer
  5. Practical Example: Movie Review Analysis
  6. Advantages of TF-IDF
  7. Limitations of TF-IDF
  8. Conclusion
  9. Further Reading

What is TF-IDF?

Term Frequency-Inverse Document Frequency (TF-IDF) is a numerical statistic that reflects the importance of a word in a document relative to a collection of documents (corpus). It's widely used in information retrieval, text mining, and NLP to evaluate how relevant a word is to a particular document in a large dataset.

Why Use TF-IDF?

While simple word counts (like those from a CountVectorizer) provide raw frequencies of terms, they don't account for the significance of those terms within the corpus. Common words like "the," "is," and "and" might appear frequently but carry little semantic weight. TF-IDF addresses this by adjusting word weights based on their distribution across documents, emphasizing terms that are more unique and informative.

How TF-IDF Works

TF-IDF combines two metrics:

  1. Term Frequency (TF): Measures how frequently a term appears in a document.

\[ \text{TF}(t, d) = \frac{\text{Number of times term } t \text{ appears in document } d}{\text{Total number of terms in document } d} \]

  1. Inverse Document Frequency (IDF): Measures how important a term is by considering its presence across the entire corpus.

\[ \text{IDF}(t, D) = \log \left( \frac{\text{Total number of documents } N}{\text{Number of documents containing term } t} \right) \]

The TF-IDF score is the product of TF and IDF:

\[ \text{TF-IDF}(t, d, D) = \text{TF}(t, d) \times \text{IDF}(t, D) \]

This calculation ensures that terms common across many documents receive lower weights, while terms unique to specific documents receive higher weights.

Implementing TF-IDF in Python

Python's Scikit-learn library offers robust tools for implementing TF-IDF through the TfidfVectorizer. Below is a step-by-step guide to applying TF-IDF to a dataset.

Setting Up the Dataset

For our practical example, we'll utilize a movie review dataset from Kaggle. This dataset comprises 64,720 movie reviews labeled as positive (pos) or negative (neg).

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split

# Import Data
data = pd.read_csv('movie_review.csv')
data.head()

Sample Output:

   fold_id cv_tag  html_id  sent_id                                               text tag
0        0  cv000    29590        0  films adapted from comic books have had plenty...  pos
1        0  cv000    29590        1  for starters , it was created by alan moore ( ...  pos
2        0  cv000    29590        2  to say moore and campbell thoroughly researche...  pos
3        0  cv000    29590        3  the book ( or " graphic novel , " if you will ...  pos
4        0  cv000    29590        4  in other words , don't dismiss this film becau...  pos

Using CountVectorizer

Before diving into TF-IDF, it's beneficial to understand CountVectorizer, which converts a collection of text documents into a matrix of token counts.

from sklearn.feature_extraction.text import CountVectorizer

corpus = [
    'This is the first document.',
    'This document is the second document.',
    'And this is the third one.',
    'Is this the first document?'
]

vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)
print(vectorizer.get_feature_names_out())
print(X.toarray())

Output:

['and' 'document' 'first' 'is' 'one' 'second' 'the' 'third' 'this']
[[0 1 1 1 0 0 1 0 1]
 [0 2 0 1 0 1 1 0 1]
 [1 0 0 1 1 0 1 1 1]
 [0 1 1 1 0 0 1 0 1]]

From the output, we observe the count of each word in the corpus represented in a numerical matrix form. However, this method doesn't account for the importance of each word across the corpus.

Applying TfidfVectorizer

To enhance our analysis, TfidfVectorizer transforms the text data into TF-IDF features, weighting terms based on their importance.

from sklearn.feature_extraction.text import TfidfVectorizer

vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(corpus)
print(vectorizer.get_feature_names_out())
print(X.toarray())

Output:

['and' 'document' 'first' 'is' 'one' 'second' 'the' 'third' 'this']
[[0.         0.46979139 0.58028582 0.38408524 0.         0.
  0.38408524 0.         0.38408524]
 [0.         0.6876236  0.         0.28108867 0.         0.53864762
  0.28108867 0.         0.28108867]
 [0.51184851 0.         0.         0.26710379 0.51184851 0.
  0.26710379 0.51184851 0.26710379]
 [0.         0.46979139 0.58028582 0.38408524 0.         0.
  0.38408524 0.         0.38408524]]

The TF-IDF matrix now provides a weighted representation, highlighting the significance of words within each document relative to the entire corpus.

Preparing Data for Modeling

To build predictive models, we'll split our dataset into training and testing sets.

X = data['text']
y = data['tag']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=1)

Practical Example: Movie Review Analysis

Leveraging TF-IDF, we can build models to classify movie reviews as positive or negative. Below is a streamlined workflow:

  1. Data Loading & Preprocessing:
    • Import the dataset.
    • Explore the data structure.
    • Handle any missing values or anomalies.
  2. Feature Extraction:
    • Use TfidfVectorizer to convert text data into TF-IDF features.
    • Optionally, remove stop words to enhance model performance:
    vectorizer = TfidfVectorizer(stop_words='english')
        
  3. Model Building:
    • Choose a classification algorithm (e.g., Logistic Regression, Support Vector Machines).
    • Train the model on the training set.
    • Evaluate performance on the test set.
  4. Evaluation Metrics:
    • Accuracy, Precision, Recall, F1-Score, and ROC-AUC are common metrics to assess model performance.

Sample Code:

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report

# Vectorization
vectorizer = TfidfVectorizer(stop_words='english')
X_train_tfidf = vectorizer.fit_transform(X_train)
X_test_tfidf = vectorizer.transform(X_test)

# Model Training
model = LogisticRegression()
model.fit(X_train_tfidf, y_train)

# Predictions
y_pred = model.predict(X_test_tfidf)

# Evaluation
print(classification_report(y_test, y_pred))

Sample Output:

              precision    recall  f1-score   support

         neg       0.85      0.90      0.87      3200
         pos       0.88      0.83      0.85      3200

    accuracy                           0.86      6400
   macro avg       0.86      0.86      0.86      6400
weighted avg       0.86      0.86      0.86      6400

The model demonstrates robust performance, accurately distinguishing between positive and negative reviews.

Advantages of TF-IDF

  • Highlights Important Words: By weighting rare but significant terms higher, TF-IDF enhances the discriminatory power of features.
  • Reduces Noise: Common words that offer little semantic value are down-weighted, leading to cleaner feature sets.
  • Versatility: Applicable across various NLP tasks like document classification, clustering, and information retrieval.
  • Ease of Implementation: Libraries like Scikit-learn simplify the integration of TF-IDF into data pipelines.

Limitations of TF-IDF

  • Sparse Representations: The resulting matrices are often sparse, which can be computationally intensive for very large corpora.
  • Lack of Semantic Understanding: TF-IDF doesn't capture the context or semantic relationships between words. Advanced models like Word2Vec or BERT address this limitation.
  • Sensitivity to Document Length: Longer documents might have higher term frequencies, potentially skewing the TF-IDF scores.

Conclusion

Term Frequency-Inverse Document Frequency (TF-IDF) is an essential technique in the NLP toolkit, enabling the transformation of textual data into meaningful numerical representations. By balancing the frequency of terms within individual documents against their prevalence across the corpus, TF-IDF emphasizes the most informative words, enhancing the performance of various text-based models.

Whether you're building sentiment analysis tools, search engines, or recommendation systems, understanding and leveraging TF-IDF can significantly elevate your project's effectiveness and accuracy.

Further Reading

  • Scikit-learn's TfidfVectorizer Documentation
  • Working With Text Data Tutorial by Scikit-learn
  • NLTK Stop Words List
  • Wikipedia: Term Frequency–Inverse Document Frequency

By integrating both theoretical insights and practical implementations, this guide provides a holistic understanding of TF-IDF, empowering you to harness its capabilities in your text analysis endeavors.

Share Article

Back to Blog

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.