Sub topic 5
Finding themes in unlabelled reviews
What this chapter assumes
You should know basic Python, lists and dictionaries, and what a vector is. You should also have read the chapter on text preparation, so the reviews you use here are already cleaned, deduplicated and free of personal data. The goal is to discover themes when you have no labels yet.
The working model
Theme discovery is unsupervised. You give an algorithm the reviews, and it groups the ones that look alike. It does not name the groups. A person reads each group and decides what it means.
The steps are:
- Turn each review into a vector of numbers.
- Group nearby vectors into clusters.
- Inspect each cluster and give it a name.
- Check that the names make sense, then decide whether to keep them in your taxonomy.
Step 1: TF-IDF vectors
TF-IDF stands for term frequency, inverse document frequency. A word scores high in a review if it appears often there and rarely in other reviews. Common words such as "the" score low, and distinctive words such as "damaged" score high.
TF-IDF counts words. It does not understand meaning. "Late" and "delayed" are two unrelated columns to it. For many review sets this is good enough, and the result is easy to explain.
Step 2: k-means clustering
K-means is a clustering algorithm. You choose a number of clusters, called k. The algorithm places k centers and assigns each review to the nearest center. It then moves each center to the average of its reviews and repeats until the groups stop changing.
The example below uses six short reviews and two clusters. It prints the three highest-weighted terms of each cluster center.
from sklearn.cluster import KMeans
from sklearn.feature_extraction.text import TfidfVectorizer
reviews = [
"Delivery was late and the box was damaged",
"Package arrived two days late",
"Courier left the parcel damaged and late",
"Great battery life and bright screen",
"The screen is bright and the battery lasts all day",
"Battery easily lasts two days, screen looks great",
]
vectorizer = TfidfVectorizer(stop_words="english")
X = vectorizer.fit_transform(reviews)
kmeans = KMeans(n_clusters=2, n_init=10, random_state=0).fit(X)
terms = vectorizer.get_feature_names_out()
for cluster in range(kmeans.n_clusters):
top = kmeans.cluster_centers_[cluster].argsort()[::-1][:3]
print(cluster, [terms[i] for i in top])The cluster numbers can differ between runs on other data, but you should see one list built around words like "late" and "damaged", and another around "battery" and "screen". A person reading these lists would name them "delivery problems" and "battery and display praise".
Notice that the second theme mixes a product feature with positive sentiment. Clustering groups by vocabulary, so it can blend topic and tone. Keep that in mind when you compare it with sentiment scores.
Step 3: Read the clusters
Top terms are a starting point. For each cluster, also read a random sample of 10 to 20 reviews, and look at the cluster size. A good cluster has a coherent top-term list and sample reviews that fit one idea.
Warning signs:
- A cluster whose top terms are generic, such as "good", "product" or "item".
- A cluster that holds half the data. It is often a catch-all.
- Two clusters with almost the same top terms. Your k is probably too high.
Choosing k
There is no single correct k. Two practical approaches work together:
- Compute a score such as the silhouette score for several values of k, to see which values separate the groups well.
- Run several values, read the clusters, and choose the k whose clusters a person can name without overlap.
The second approach usually decides, because the themes must be useful to the team reading them.
Embeddings as an alternative
An embedding model turns text into a vector that captures meaning, so "late" and "delayed" land close together. You can feed these vectors to k-means in the same way as TF-IDF vectors.
| Property | TF-IDF | Embeddings |
|---|---|---|
| Captures synonyms | No | Yes |
| Explaining a cluster | Top terms are direct | Needs example reviews or a labelling step |
| Cost | Very low, runs locally | Model or API calls |
| Short or slangy text | Weak | Usually better |
Start with TF-IDF as a baseline. Move to embeddings when your clusters split one idea across several groups because reviewers use different words.
Common errors
- Skipping cleaning. Duplicates form their own tight cluster and make a minor complaint look like a major theme.
- Trusting the cluster size. A big cluster may be big because of one template or one campaign, not because many customers care.
- Treating names as facts. A label you wrote after reading ten reviews is a hypothesis. Test it on other reviews before you report it.
- Forcing every review into a cluster. K-means assigns every review somewhere, even off-topic ones. Keep an "other" bucket for reviews that sit far from every center.
- Unfixed randomness. Set
random_stateso results can be reproduced, and usen_inithigh enough that one bad start does not decide the outcome.
Exercise
You run k-means with k=8 on 5,000 reviews. Two clusters have the top terms "shipping, arrived, late, days" and "delivery, courier, delayed, parcel". Both are about the same issue. Name one likely cause and one fix.
Show answer
The cause is that TF-IDF treats different words for the same idea as unrelated, and k may also be too high. Fixes: lower k and re-read the clusters, or switch to embeddings so synonyms such as "late" and "delayed" fall into one cluster. You can also merge the two clusters by hand into one "delivery" theme after reading samples from each.
Exercise
Why should you read sample reviews from a cluster rather than rely only on its top terms?
Show answer
Top terms show vocabulary but not context. A cluster can have plausible terms and still mix several ideas, or include reviews that only share a common word. Reading a random sample shows whether the cluster is coherent and whether your proposed name fits.