liveBook · Manning

Term frequency-inverse document frequency

As explained in Natural Language Processing in Action, Second Edition

A TF-IDF model represents text using term-frequency and inverse-document-frequency weights for terms in documents.

Term frequency–inverse document frequency (TF-IDF) represents each document as real-valued weights rather than simple word counts. It highlights terms that occur often in one document but appear in relatively few documents, making them useful for relevance ranking, search, spam filtering, and document classification. section 3.0

Build a TF-IDF weight in three stages:

  • Calculate term frequency: measure how often a term occurs in a document, usually relative to the document's length. section 3.6.1
  • Calculate inverse document frequency: compare the corpus size with the number of documents containing the term; smoothing prevents zero-frequency problems. section 3.6.1
  • Combine the two values to give terms greater weight when they are locally frequent and globally informative. section 3.0 section 3.6.1
  • Apply logarithmic scaling to reduce Zipf-distribution extremes, so similarly frequent words do not receive vastly different scores. section 3.6.1

Ranking a document corpus

With scikit-learn's TfidfVectorizer, fit a vocabulary on 41,531 Hacker Public Radio sentences and transform them into a sparse 41,531 × 56,404 matrix. Transform the query "where is the lost audio," multiply its sparse vector by the document matrix transpose, and select the highest dot product to retrieve the most relevant sentence. Keeping the matrix sparse avoids allocating roughly 2.3 billion dense values. section 3.6.2 section 3.6.2

>>> log_tf = log(term_occurences_in_doc) -\
...     log(num_terms_in_doc)                  #1
>>> log_log_idf = log(log(total_num_docs) -\
...     log(num_docs_containing_term))     #2
>>> log_tf_idf = log_tf + log_log_idf    #3

Inspect a compact logarithmic TF-IDF implementation that computes log-scaled term frequency, log-scaled inverse document frequency, and their sum. section 3.6.1

Compare normalized TF-IDF vectors with cosine similarity rather than relying on Euclidean distance. Exact spelling remains a boundary: synonyms, paraphrases, and sometimes antonyms can receive misleadingly distant or close vectors. Topic vectors and semantic embeddings address these meaning gaps, while TF-IDF remains useful for full-text search features. section 3.10 section 4.1.1 section 3.8

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Derives logarithmic weighting, smoothing, sparse-matrix ranking, and cosine comparison, but defers semantic meaning representation.
Examples
Uses a Hacker Public Radio corpus, a lost-audio query, sparse matrices, and logarithmic TF-IDF code.
What it assumes
Assumes word-count vectors and document collections, while teaching continuous numerical weights from term and document frequencies.
What you can do after
Apply it via scikit-learn to vectorize documents, preserve sparsity, compare vectors, and rank search results.
How it connects
Connects bag-of-words, n-grams, Zipf's law, cosine similarity, stemming, lemmatization, topic vectors, and semantic embeddings.

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 Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.