Notes

Term Frequency (TF)

The simplest useful question you can ask about a document is how many times a given word appears in it. Term frequency is that count — and nearly every count-based text representation is built on top of it.

Counting, then taming the count
Fix a vocabulary, count each term's occurrences in each document, and you have the document-term matrix: one row per document, one column per term, term frequencies in the cells. Raw counts are rarely used as they stand, because a 10,000-word report outscores a 200-word abstract on every term it mentions. The usual repairs:

  • Relative frequency — divide by the document's length.
  • Log normalisation — use 1 + log(tf), so the fifth occurrence of a word adds less than the second.
  • Maximum normalisation — divide by the highest count in that document.
  • Binary occurrence — collapse the count to present or absent.

Why it matters
Term frequency is the evidence half of keyword search and of every sparse classifier: a document that says mortgage nine times really is more about mortgages than one that mentions it once. What it cannot do is tell an informative word from furniture — the and of have the highest counts in almost any document, which is exactly the gap inverse document frequency fills to give TF-IDF. BM25 goes further and saturates the count, on the sound argument that the twentieth mention adds almost nothing over the tenth. In scikit-learn you get raw counts from CountVectorizer and the log-damped form by passing sublinear_tf=True. Skip normalisation altogether and your rankings tilt towards whatever happens to be longest.

Term frequency is the number of times a term occurs in a document — the cell value of a document-term matrix and the raw signal behind bag-of-words features. It is normally damped or length-normalised, since raw counts favour long documents, and weighted by inverse document frequency to form TF-IDF. Without it a sparse model has no measure of how strongly a document is about a word; without normalisation, length dominates relevance.

Hand someone an article and ask what it is about. One quick trick: see which words keep coming back. If “vaccine” appears fifteen times, that is a strong hint.

Term frequency is software doing exactly that — counting how many times each word shows up in a piece of writing. It is one of the oldest and most useful ideas for turning text into something a computer can measure. On its own it is a blunt instrument, because the words that appear most are usually “the” and “and”, which tell you nothing. So the counts get adjusted — shrunk for very long documents, discounted for words that turn up everywhere — before anyone trusts them.