Introduction
In Natural Language Processing (NLP) and information retrieval systems, understanding the importance of words in a document is essential. When dealing with large volumes of text—such as search engines, chatbots, recommendation systems, and document classification—simply counting words is not enough.
This is where TF-IDF (Term Frequency–Inverse Document Frequency) becomes highly useful. It is a statistical technique used to evaluate how important a word is to a document relative to a collection of documents (called a corpus).
TF-IDF helps machines identify meaningful words while reducing the impact of very common words such as “the”, “is”, and “and”. It is widely used in text processing, search ranking, keyword extraction, and machine learning models.
This article explains TF-IDF in detail, including its formula, working, examples, real-world use cases, advantages, disadvantages, and best practices.
What is TF-IDF?
TF-IDF stands for Term Frequency–Inverse Document Frequency.
It is a numerical measure that indicates how important a word is in a document compared to all documents in a dataset.
Key Idea
Words that appear frequently in a document are important (Term Frequency)
Words that appear in many documents are less important (Inverse Document Frequency)
TF-IDF combines both concepts to assign a weight to each word.
Understanding Term Frequency (TF)
Term Frequency measures how often a word appears in a document.
Formula
TF = (Number of times a term appears in a document) / (Total number of terms in the document)
Example
Document: "AI is transforming the world of AI"
Word: "AI"
Count of "AI" = 2
Total words = 7
TF = 2 / 7 = 0.28
Explanation
Term Frequency highlights words that are frequently used within a single document.
Understanding Inverse Document Frequency (IDF)
Inverse Document Frequency measures how unique or rare a word is across all documents.
Formula
IDF = log(Total number of documents / Number of documents containing the term)
Example
Total documents = 100
Documents containing "AI" = 10
IDF = log(100 / 10) = log(10)
Explanation
If a word appears in many documents, its importance decreases
Rare words get higher importance
TF-IDF Formula
TF-IDF is calculated by multiplying TF and IDF:
TF-IDF = TF × IDF
Interpretation
High TF-IDF → Word is important in that document
Low TF-IDF → Word is common or less meaningful
Step-by-Step Example of TF-IDF
Consider three documents:
Doc1: "AI is the future"
Doc2: "AI and Machine Learning"
Doc3: "Machine Learning is powerful"
Step 1: Calculate TF
"AI" appears in Doc1 and Doc2
"Machine" appears in Doc2 and Doc3
Step 2: Calculate IDF
Words appearing in all documents → low IDF
Words appearing in fewer documents → high IDF
Step 3: Calculate TF-IDF
Each word gets a score based on its importance in a document.
Join the conversation! Your thoughts help the community grow.