- Introduced by
- Salton & Buckley
- Year
- 1988
- Type
- Text vectorization / term-weighting scheme
What does it do?
TF-IDF (term frequency–inverse document frequency) is a term-weighting scheme that scores each word in a document by how often it appears there and how rare it is across the whole collection.
It turns raw text into weighted document vectors, giving high weight to terms that are frequent in one document but uncommon elsewhere. For the underlying comparison of term-weighting approaches in automatic text retrieval, see Salton and Buckley (1988).
The intuition behind the method
Not every word that appears often is informative. Words like "the" occur in nearly every document and say little about what a particular document is about. TF-IDF rests on the idea that a term matters most when it is frequent inside one document yet rare across the rest of the collection. Multiplying a local count (how often the term occurs in this document) by a global rarity measure (how few documents contain it) highlights the words that characterise each document and reduces the weight of words that are common everywhere.
History
Salton and Buckley (1988), 'Term-weighting approaches in automatic text retrieval', compares term-weighting approaches for automatic text retrieval. Historical attribution of the inverse document frequency component to a specific originator should be checked against primary sources before it is stated.
When to use it
Choose TF-IDF when you have a collection of text documents and need numeric features for retrieval, ranking, keyword extraction, or comparing documents by their vocabulary. It assumes a bag-of-words representation, so it suits questions where which terms appear matters more than their order or syntax. It also requires consistent tokenisation across the whole corpus. Be cautious with a small collection: the IDF component is computed from document counts, so the weights become unstable when the number of documents containing each term is small, and it is worth checking whether a simpler word-frequency analysis answers the question. Avoid it when your question depends on word order, negation or meaning, since those are not represented. TF-IDF cannot be computed without text data.
Limitations and considerations
- The bag-of-words assumption discards word order and local context, so "not good" and "good" are not distinguished by the representation itself.
- The IDF component depends on document frequencies; in a small collection a term found in only a few documents receives a large weight simply because the sample is small, which makes the weights unstable.
- Raw term counts favour longer documents unless the variant used normalises for length, so the vectors can reflect document length as well as content.
- Several TF and IDF variants are in use, including alternative weightings and normalised TF-IDF, and they produce different numeric values for the same corpus. Results are comparable only when the variant used is reported with them.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Each weight can be traced to two counts: the number of times a term occurs in a document and the number of documents that contain the term.
- Produces a fixed-length numeric vector for each document, so the output can be passed directly to standard retrieval, classification and clustering methods.
- Reduces the influence of words that occur across most of the corpus, so the most heavily weighted terms tend to be those specific to individual documents.
Applications
- Representing documents as feature vectors for text classification.
- Ranking documents by their similarity to a query in search applications.
- Identifying the most distinctive keywords that characterise individual documents.
- Building inputs for document clustering and similarity comparison.
How it works
How it works
The corpus is cleaned and split into terms (tokens) that will be counted and weighted. Lowercasing, punctuation handling and decisions about what counts as a token change the vocabulary and the counts, so they must be applied the same way to every document for the resulting weights to be comparable.
For each document, the term frequency (TF) records how often each term occurs in that document. Terms repeated many times within one document receive a higher local weight, which reflects their prominence in that document's text. Raw counts favour longer documents unless the chosen variant normalises for document length.
Across the whole collection, the inverse document frequency (IDF) depends on how many documents contain each term. For a term t in a collection D, the classical form is IDF(t,D) = ln(|D| / |{d in D : t in d}|), the logarithm of the total number of documents divided by the number of documents that contain t. A term found in most documents receives a low IDF, and a term found in only a few documents receives a high IDF. The logarithm corrects the bias of ranking in favour of rare terms. This is the component that suppresses words spread across the corpus and gives weight to terms that separate documents from one another. Because it is computed from document counts, it becomes unreliable when the collection is small.
Formula
\mathrm{IDF}(t,D)=\ln\left(\frac{|D|}{|\{d\in D : t\in d\}|}\right)The TF and IDF components are combined into one weight per term, TF-IDF(t,d,D) = TF(t,d) x IDF(t,D), and each document is represented as a vector of these weights over the shared vocabulary. These document-term vectors are the input that downstream tasks such as search, classification and clustering use.
Formula
\mathrm{TFIDF}(t,d,D)=\mathrm{TF}(t,d)\cdot\mathrm{IDF}(t,D)Common pitfalls
- Applying TF-IDF to a small collection and interpreting the highest-weighted terms as meaningful distinctions; with few documents, rare terms can dominate the weights for chance reasons.
- Changing tokenisation, casing or stop-word handling between documents or between the training and new collections, which makes the vocabularies and weights incompatible.
- Reading high TF-IDF weights as evidence of meaning or topic; the score reflects frequency and rarity of surface terms, so synonyms and paraphrases receive no shared weight.
- Comparing TF-IDF vectors across corpora with different IDF statistics as if the weights were on a common scale.
Frequently asked questions
What do TF and IDF each measure?
Term frequency (TF) measures how often a term appears within a single document, capturing how central it is locally. Inverse document frequency (IDF) measures how rare the term is across the whole collection, downweighting words that appear in many documents. Multiplying them gives high weight to terms that are frequent in one document but uncommon elsewhere.
How large does my corpus need to be?
The sources used for this page give no fixed minimum corpus size. IDF is computed from the number of documents containing each term, so in a small collection those counts are few and the weights can be unstable. In very small collections, a simpler word-frequency analysis may be more informative, and the IDF weighting should be compared against that baseline before its results are interpreted.
Does TF-IDF understand meaning or context?
No. TF-IDF rests on the bag-of-words assumption: it weights individual terms by frequency and rarity and ignores word order, syntax, and semantics. For context or semantic similarity, embedding methods such as Word2Vec are more appropriate.
What preprocessing does it require?
The corpus must be cleaned and tokenised consistently before weighting: normalising text and splitting it into comparable term units. Skipping this step or applying it inconsistently leaves noisy tokens and incomparable counts that degrade the weights.
Related methods
Sources
Salton, G. & Buckley, C. (1988). Term-weighting approaches in automatic text retrieval. Information Processing & Management, 24(5), 513-523.
Open DOI reference2018_Book_DataScienceAndPredictiveAnalyt, Section 20.3 TF-IDF, p. 677