Sub topic 13
Describing Clusters with c-TF-IDF
Prerequisites
This chapter assumes that you understand tokenisation, stop words, TF-IDF, and the basic BERTopic pipeline. You should also know that a document embedding is a numeric representation of a document's meaning.
What c-TF-IDF measures
BERTopic first groups documents into clusters. It then combines all documents in each cluster into one class document. Class-based TF-IDF, or c-TF-IDF, scores words that describe one cluster better than the others.
This differs from ordinary TF-IDF:
- TF-IDF scores a word for one document.
- c-TF-IDF scores a word for one cluster.
- A high score means that the word is frequent in the cluster and relatively uncommon across other clusters.
For a word w and cluster c, the calculation has two parts:
- Term frequency measures the word's share of all words in the cluster.
- Inverse document frequency reduces the score of words that occur in many clusters.
A simplified form is:
c-TF-IDF(w, c) = tf(w, c) × log(1 + A / f(w))
Here, tf(w, c) is the count of the word in cluster c divided by the total word count for that cluster. A is the average number of words per cluster, and f(w) is the word's total count across clusters.
Where it fits in BERTopic
Words such as the may occur often, but preprocessing usually removes them. A word such as election can receive a high score if it is common in one cluster and rare elsewhere. The highest-scoring words become the topic representation.
A small implementation
The following example calculates the scores for two already-created clusters.
import math
from collections import Counter
clusters = {
"sports": ["team wins match", "player scores goal"],
"politics": ["party wins election", "minister debates policy"],
}
counts = {
name: Counter(" ".join(docs).split())
for name, docs in clusters.items()
}
total_words = {name: sum(counter.values()) for name, counter in counts.items()}
average_words = sum(total_words.values()) / len(total_words)
all_counts = Counter()
for counter in counts.values():
all_counts.update(counter)
for name, counter in counts.items():
scores = {}
for word, count in counter.items():
tf = count / total_words[name]
idf = math.log(1 + average_words / all_counts[word])
scores[word] = tf * idf
print(name, sorted(scores.items(), key=lambda item: item[1], reverse=True))In BERTopic, the model performs this operation after clustering. You can inspect the result with topic_model.get_topic(topic_id). The returned list contains words and their c-TF-IDF weights.
Common errors and limitations
A cluster can receive a poor description when:
- documents are too short to provide enough word counts;
- preprocessing removes important domain terms;
- a generic word appears in many clusters;
- the cluster contains several unrelated subjects;
- the vectoriser uses unsuitable n-gram or stop-word settings.
Inspect example documents alongside the top words. A label that looks plausible can still hide a mixed or accidental cluster.
Exercise
Why can a word be frequent in a cluster but still receive a low c-TF-IDF score?
Show answer
It may also occur frequently in many other clusters, so its inverse-frequency factor is small.