liveBook · Manning

Term frequency-inverse document frequency

As explained in AI Agents in Action

TF-IDF is a term-weighting method that scores words by their frequency in a document relative to their frequency across a document collection.

TF-IDF weights words by combining their frequency within one document with their rarity across a document collection. It provides a simple way to transform text into numerical vectors for similarity matching. section 8.3.1

Calculate the weight in two stages:

  • Measure term frequency (TF) by dividing a word’s occurrences in a document by the document’s total word count. In “The sky is blue and beautiful,” blue appears once among six words, giving TF = 0.16. section 8.3.1
  • Measure inverse document frequency (IDF) by taking the logarithm of the total document count divided by the count of documents containing the word. section 8.3.1
  • Combine TF and IDF so repeated words matter within a document while common collection-wide words receive less weight. section 8.3.1

Vectorizing a document collection

A Python example places eight short documents into a list, creates a TfidfVectorizer, and fits it to produce document vectors. The resulting vectors can then support document similarity comparisons. section 8.3.1

Listing 8.1 document_vector_similarity (transform to vector)
import plotly.graph_objects as go
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

documents = [      #1
    "The sky is blue and beautiful.",
    "Love this blue and beautiful sky!",
    "The quick brown fox jumps over the lazy dog.",
    "A king's breakfast has sausages, ham, bacon, eggs, toast, and beans",
    "I love green eggs, ham, sausages and bacon!",
    "The brown fox is quick and the blue dog is lazy!",
    "The sky is very blue and the sky is very beautiful today",
    "The dog is lazy but the brown fox is quick!"
]

vectorizer = TfidfVectorizer()     #2
X = vectorizer.fit_transform(documents)      #3

Inspect the runnable document-vector example, including its sample corpus and scikit-learn vectorizer setup. section 8.3.1

How AI Agents in Action covers it

How deep it goes
Defines TF and IDF, works through a sample calculation, and stops short of detailing the vectorizer’s internals.
Examples
Uses “The sky is blue and beautiful,” an eight-document corpus, and exercises comparing TF-IDF with Word2Vec or BERT embeddings.
What you can do after
Apply it via scikit-learn to vectorize documents and compare their similarity.
How it connects
Connects document vectors with cosine similarity, semantic search, vectorization, and retrieval.

Also explained in

Compare how 10 books explain term frequency-inverse document frequency →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents in Action. The section links point to the parts of the book it draws on.