Phase 3 · Machine learning · Unsupervised
Clustering
Group similar items without labels (k-means).
Open this lesson Create a free account
Why it matters
Splitting customers into groups is one of the oldest jobs in marketing. Clustering does it from data instead of hunches. The same tool can sort thousands of support tickets, reviews or survey answers by topic, once each text is turned into an embedding. That's how teams find themes that nobody labeled. Clustering is also your first model with no right answers to check against. So you'll judge it in a new way: by whether the groups make sense and are useful.
What you'll be able to do
- Explain unsupervised learning and what clustering can and can't promise
- Run k-means and describe its assign-then-average loop
- Choose k with the elbow method, silhouette scores and judgment
- Profile clusters into segments, and cluster text through embeddings
Inside this lesson
- 12 short sections
- 5 code examples you can edit and run
- a practice exercise that checks your code
- a 5-question quiz
- 5 review cards for spaced repetition
What it covers
- Learning without labels
- k-means in four steps
- k-means by hand on six customers
- Scale the features first
- How many clusters?
- From clusters to segments
- Clustering text with embeddings
- Limits worth knowing
A taste of the code
orders = [1, 2, 3, 10, 11, 12]
centers = [1.0, 2.0] # start on the first two customers
for round_number in range(1, 10):
# Assign: each customer joins the nearest center
labels = [min(range(len(centers)), key=lambda j: abs(x - centers[j])) for x in orders]
# Update: each center moves to the average of its customers
new_centers = []
for j in range(len(centers)):
members = [x for x, label in zip(orders, labels) if label == j]
new_centers.append(sum(members) / len(members))
print(f"round {round_number}: labels {labels} centers {new_centers}")
if new_centers == centers:
break
# …
Try a quiz question
What does the k-means update step do?