Before text can go into a machine learning model, it has to become numbers. TF-IDF is one of the oldest and most useful ways to do that: it scores each word in a document by how much it tells you about that document, and it still makes a strong, fast baseline for search and text classification.
Counting words is not enough
The simplest approach, called bag of words, counts how often each word appears in a document. The trouble is that the most frequent words, such as “the” and “is”, appear everywhere and say nothing about what a document is about. We need a score that rewards words that are common in one document but rare across the collection.
The two parts of TF-IDF
Term frequency, or TF, measures how often a word appears in a document. Inverse document frequency, or IDF, measures how rare the word is across all documents: words that appear in many documents get a low IDF, and words that appear in few get a high one. Multiplying the two gives a score that is high for words that are frequent here but uncommon elsewhere, which are usually the words that characterize the document. Exact formulas vary slightly between libraries, but the idea is the same.
A short example in scikit-learn
from sklearn.feature_extraction.text import TfidfVectorizer
docs = [
"data pipelines move data",
"models learn from data",
"pipelines need tests",
]
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(docs)
print(vectorizer.get_feature_names_out())
print(X.shape)
The result is a table with one row per document and one column per distinct word, holding each word’s TF-IDF score. Because most documents use only a small fraction of the vocabulary, scikit-learn stores it as a sparse matrix, which keeps memory use manageable.
Using TF-IDF for classification
Feed the TF-IDF matrix into a simple classifier such as logistic regression and you have a text classifier that is quick to train and easy to inspect, because each coefficient corresponds to a word. Put the vectorizer and the classifier in one pipeline, as described in scikit-learn pipelines, so the vocabulary and IDF values are learned from training data only.
Using TF-IDF for search
To find documents relevant to a query, turn the query into a TF-IDF vector in the same space and rank documents by cosine similarity, a measure of how closely two vectors point the same way. This is a classic keyword-style retrieval method, and it is a good first version of a search feature. My Knowledge-Base Search (RAG) project is an example of retrieval over a document collection.
Useful settings
- Stop words. Removing very common words can help, though TF-IDF already down-weights them.
- N-grams. Including pairs of words, such as “data pipeline”, captures short phrases that single words miss.
- Minimum and maximum document frequency. Dropping extremely rare or extremely common terms shrinks the vocabulary and reduces noise.
- Lowercasing and tokenization. Decide how text is split into words, because it changes the vocabulary.
Where TF-IDF falls short
TF-IDF treats words as unrelated symbols. It ignores word order, so “dog bites man” and “man bites dog” look the same, and it cannot tell that “car” and “automobile” mean similar things. A search for one will miss documents that use the other. Embeddings address this by representing meaning, as explained in text embeddings.
Start with TF-IDF as a baseline
TF-IDF with a linear model is cheap to build and often surprisingly competitive, particularly with small datasets or clearly worded text. Treat it as the baseline model that a heavier approach must beat to justify its cost.
A quick checklist
- Is the vectorizer fitted on training text only, inside a pipeline?
- Did you try single words and short phrases (n-grams)?
- Did you limit the vocabulary with minimum and maximum document frequency?
- Do you compare against an embedding-based approach before settling?
- Does your task depend on word meaning or order, which TF-IDF cannot capture?
FAQ
What does TF-IDF stand for?
Term frequency times inverse document frequency. It scores a word by how often it appears in a document, weighted by how rare it is across all documents.
What is the difference between bag of words and TF-IDF?
Bag of words uses raw word counts. TF-IDF rescales those counts so words that appear in many documents count for less and distinctive words count for more.
Is TF-IDF still useful with modern language models?
Yes. It is fast, cheap and interpretable, and it makes a strong baseline, especially for small datasets and keyword-style search.

