What is TF-IDF?
TF-IDF (term frequency times inverse document frequency) is a score for how important a word is to one document within a collection. It rises when a word appears often in that document and falls when the word appears in many documents. Words like "the" end up near zero, and topic words like "whale" or "rocket" stand out.
TF-IDF turns each document into a vector of word weights, which makes text usable by any method that works on numbers. It powered keyword search engines for decades, and its descendant BM25 is still the default ranking function in Lucene and Elasticsearch. It is also a strong baseline for spam filtering, topic classification, document clustering, and keyword extraction, and it handles the keyword side of hybrid search in retrieval-augmented generation (RAG).
Key ideas
- TF measures how often a term appears in one document, usually divided by the document's length.
- IDF = log(N / df) is high for terms found in few documents and zero for terms found in all of them.
- TF-IDF multiplies the two, so a high score needs a term that is frequent locally and rare globally.
- Each document becomes a sparse vector with one dimension per vocabulary term.
- Cosine similarity between TF-IDF vectors ranks related documents and search results.
- TF-IDF matches exact words only, so it misses synonyms and confuses different senses of one word.
Common questions
What is the difference between TF-IDF and word embeddings?
TF-IDF gives every word its own dimension and assigns weights from counts, so "car" and "automobile" are as unrelated as "car" and "banana". Word embeddings such as word2vec learn dense vectors in which related words sit close together. TF-IDF needs no training and is easy to interpret; embeddings capture meaning but need a trained model.
Is TF-IDF still used?
Yes. It is a fast, strong baseline for text classification and clustering, and BM25, its refined form, still runs first-stage retrieval in many search systems. Hybrid search often combines BM25 scores with dense embedding scores because keyword matching catches exact names, codes, and rare terms that embeddings can blur.
How do you compute TF-IDF in Python?
scikit-learn's TfidfVectorizer tokenizes the text, builds the vocabulary, computes smoothed IDF weights, and returns an L2-normalized sparse matrix with one row per document. Calling fit_transform on the training documents and transform on new ones keeps the IDF values fixed to the training corpus.
Can TF-IDF use phrases instead of single words?
Yes. Treating n-grams such as "new york" or "not good" as extra terms lets TF-IDF keep some word order and negation. The vocabulary grows quickly, so n-gram features are usually limited by a minimum document frequency or a maximum feature count.
Related explainers
- Bag of Words — Turn text into numbers — count word occurrences to create the simplest document representation in NLP
- Tokenization & BPE — How text becomes numbers — from characters to subword tokens via byte-pair encoding
- Text Classification & Sentiment — Teach a model to assign labels to text — from spam detection to sentiment analysis
- Norms & Distance Metrics — L1, L2, cosine similarity — measuring closeness in vector spaces
- Word2Vec & Word Embeddings — Learn dense vector representations where similar words live near each other in space
- Retrieval-Augmented Generation — Ground language model outputs in retrieved documents to reduce hallucination
References
- Spärck Jones (1972), "A Statistical Interpretation of Term Specificity and Its Application in Retrieval", Journal of Documentation
- Salton, Wong & Yang (1975), "A Vector Space Model for Automatic Indexing", Communications of the ACM
- Robertson & Zaragoza (2009), "The Probabilistic Relevance Framework: BM25 and Beyond"
- Jurafsky & Martin, "Speech and Language Processing" (3rd ed. draft)
Last updated