← CS Lab AI & Machine Learning / TF-IDF Open standalone ↗

TF-IDF scores a word by how much it pins down this document. It rewards a term that shows up often inside one document but rarely across the whole collection — and quietly mutes words like "the" that are everywhere and tell you nothing.

Where you meet it

What it does

It turns a document into a vector of weights, one per vocabulary term. Each weight balances two signals against each other: how much the term belongs to this document (TF) versus how generic it is across the corpus (IDF). The result is a sparse, interpretable representation — every dimension is a real word you can point at.

A word in every document scores exactly zero — TF-IDF rewards what sets a document apart, not what it merely contains.

How it works

Two factors, multiplied. Term frequency counts how often term t appears in document d. Inverse document frequency looks at the document frequency df — how many of the N documents contain t at all — and inverts it on a log scale:

idf(t)    = log(N / df(t))
tf-idf(t,d) = tf(t,d) · idf(t)

A term in every document has df = N, so idf = log(1) = 0 — and its weight collapses to zero no matter how often it occurs. A term in just one document gets a high IDF. The product peaks for words that are frequent in this doc but rare in the corpus: those are the discriminating ones. That is exactly why "the" scores 0 while "mat" wins.

Watch out

  • Purely lexical. It matches surface strings, not meaning — "car" and "automobile" are unrelated dimensions. For synonymy and context you need dense embeddings (word2vec, transformers).
  • Bag of words. The vector is sparse and order-blind: "dog bites man" and "man bites dog" look identical.
  • Tokenization-dependent. Results swing with lowercasing, stemming, n-grams and your stop-word list — garbage tokens in, garbage weights out.
  • Many variants. Sublinear (log) TF, smoothed IDF, and L2 normalization all change the numbers — scikit-learn defaults to log((1+N)/(1+df))+1 plus L2, not the textbook log(N/df).

Go deeper

TF-IDF bewertet ein Wort danach, wie stark es genau dieses Dokument kennzeichnet. Es belohnt einen Term, der innerhalb eines Dokuments oft, im gesamten Korpus aber selten vorkommt — und schaltet Wörter wie "the", die überall stehen und nichts aussagen, leise stumm.

Wo es vorkommt

Was es tut

Es verwandelt ein Dokument in einen Vektor aus Gewichten, eines pro Vokabular-Term. Jedes Gewicht wägt zwei Signale gegeneinander ab: wie sehr der Term zu diesem Dokument gehört (TF) gegen seine Allgegenwart im Korpus (IDF). Heraus kommt eine sparse, interpretierbare Repräsentation — jede Dimension ist ein echtes Wort, auf das man zeigen kann.

Ein Wort in jedem Dokument zählt exakt null — TF-IDF belohnt, was ein Dokument abhebt, nicht was es bloß enthält.

Wie es funktioniert

Zwei Faktoren, multipliziert. Die Term-Häufigkeit zählt, wie oft Term t in Dokument d vorkommt. Die inverse Dokumenthäufigkeit nimmt die Dokumenthäufigkeit df — in wie vielen der N Dokumente t überhaupt auftaucht — und invertiert sie logarithmisch:

idf(t)    = log(N / df(t))
tf-idf(t,d) = tf(t,d) · idf(t)

Ein Term in jedem Dokument hat df = N, also idf = log(1) = 0 — sein Gewicht bricht auf null zusammen, egal wie oft er vorkommt. Ein Term in nur einem Dokument bekommt ein hohes IDF. Das Produkt erreicht sein Maximum bei Wörtern, die in diesem Dok häufig, im Korpus aber selten sind: genau die unterscheidenden. Deshalb landet "the" bei 0, während "mat" gewinnt.

Worauf achten

  • Rein lexikalisch. Es vergleicht Zeichenketten, keine Bedeutung — "Auto" und "Wagen" sind unabhängige Dimensionen. Für Synonymie und Kontext brauchst du dichte Embeddings (word2vec, Transformer).
  • Bag of Words. Der Vektor ist sparse und reihenfolgenblind: "Hund beißt Mann" und "Mann beißt Hund" sehen identisch aus.
  • Tokenisierungs-abhängig. Ergebnisse kippen mit Kleinschreibung, Stemming, n-Grammen und der Stoppwort-Liste — schlechte Tokens rein, schlechte Gewichte raus.
  • Viele Varianten. Sublineares (log-)TF, geglättetes IDF und L2-Normierung verändern die Zahlen — scikit-learn nutzt per Default log((1+N)/(1+df))+1 plus L2, nicht das Lehrbuch-log(N/df).

Mehr dazu

Next up
AI & Machine LearningNamed Entity Recognition AI & Machine LearningConvolution & CNNs All concepts →