TF-IDF › TF-IDF: Term Frequency-Inverse Document Frequency

Michael BrenndoerferUpdated March 21, 20263 min read
Language AI HandbookAll chapters
Part 2 of 67Classical Text Representations
Chapter 13TF-IDF › TF-IDF: Term Frequency-Inverse Document Frequency
Reading section 1 of 13 · TF-IDF: Term Frequency-Inverse Document Frequency · 3 minSection 1 of 13 · 3 min

Book contents

Section 1 of 13 · 3 min read

TF-IDF › TF-IDF: Term Frequency-Inverse Document FrequencyOne section · Sections in this chapter1 / 13 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

You've already seen how raw word counts fail as text representations. A document about machine learning will contain hundreds of occurrences of "the," "is," and "of," and none of them tell you anything useful about the document's content. In the previous chapters, you explored term frequency as a way to measure how prominently a word features within a single document, and inverse document frequency as a way to measure how rarely a word appears across an entire collection. TF-IDF is the synthesis of these two ideas, and it's one of the most durable innovations in the history of information retrieval.

The insight driving TF-IDF is elegantly simple: a word's importance to a document depends on two simultaneous conditions. First, it must appear frequently enough within that document to signal relevance. Second, it must be rare enough across the broader corpus to carry discriminative information. A word satisfying only the first condition (like "the") is a stopword. A word satisfying only the second condition (like "backpropagation" appearing just once in a long article) is a faint signal. The words you really want to identify are those satisfying both conditions at once: appearing prominently in this particular document while remaining rare elsewhere. Those are the words that define what a document is about.

TF-IDF, short for Term Frequency-Inverse Document Frequency, captures this intuition through multiplication. Multiply how frequently a term appears in a document by how rare it is across the corpus, and you get a score that is high only when both conditions hold simultaneously. This seemingly simple formula has powered document search, information retrieval, and text classification for more than five decades. It remains a first-resort tool in NLP today because it works well.

This chapter brings together everything from the previous two chapters. You'll learn the exact TF-IDF formula and its principal variants, implement it from scratch and understand every design choice, explore normalization strategies, compute document similarity using cosine similarity, build a working search system, apply TF-IDF as feature input to machine learning classifiers, and understand how BM25 extends and improves on the basic formula. By the end, you'll know exactly when TF-IDF is the right tool, when it falls short, and how to use scikit-learn's TfidfVectorizer with confidence in real-world settings.