Index Ai Academy
Phase 3 · Machine learning · Unsupervised

Clustering

Group similar items without labels (k-means).

About 35 minutes · Free account required

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

  1. Learning without labels
  2. k-means in four steps
  3. k-means by hand on six customers
  4. Scale the features first
  5. How many clusters?
  6. From clusters to segments
  7. Clustering text with embeddings
  8. 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
# …
In the lesson you can change this and run it in your browser.

Try a quiz question

What does the k-means update step do?

  1. Moves each center to the average of the points assigned to it
  2. Assigns each point to its nearest center
  3. Removes outliers
  4. Chooses k

Answer it at the end of the lesson to see if you're right, and why.

Before this lesson