Back to all lessons
Machine LearningBeginner

🔵Clustering & K-Means

Finding groups without labels

Take your time with this one. The interactive parts are here to help you test the idea, not rush through it.

20 min- Explore at your own pace

Before We Begin

What we are learning today

Finding order in chaos. Without labels, clustering groups similar points to reveal hidden structure we might otherwise miss.

How this lesson fits

Here’s where the magic shows up: we stop hand-writing every rule and let data teach the model. Think of it as coaching instead of scripting.

The big question

How can a machine spot patterns from examples the way a student learns from practice problems?

Tell the difference between predicting numbers and discovering patternsInterpret simple models and talk through their outputs in plain EnglishCompare the strengths and tradeoffs of common ML methods

Why You Should Care

Not all ML starts with answer keys. Sometimes the goal is simply to reveal structure and patterns.

Where this is used today

  • Customer segmentation for marketing
  • Image compression (color quantization)
  • Grouping search results by topic

Think of it like this

Like sorting a box of LEGO bricks without instructions. You could cluster by color or size—several answers can make sense.

Easy mistake to make

K-means doesn’t discover a single “true” answer. Different choices of K can produce different but valid groupings.

By the end, you should be able to say:

  • Explain what makes clustering unsupervised
  • Describe the two repeating steps of K-means
  • Interpret the elbow method as a way to choose K

Think about this first

If you sorted a box of mixed items without labels, what clues would you use to form groups?

Words we will keep using

clustercentroidassignmentinertiaunsupervised

Clustering: Finding Hidden Groups

Clustering is like sorting a bucket of mixed LEGOs when you lost the instruction manual. You don't know what the groups are supposed to be, so you organize them by what looks similar—color, size, or shape.

K-Means Algorithm

  1. Guess: Drop K center points (centroids) randomly on the map.
  2. Assign: Every data point joins the team of the closest centroid.
  3. Update: Each team finds its new center of gravity and moves the centroid there.
  4. Repeat until nothing moves anymore.
Inertia (WCSS): This is a score for how tightly each group holds together. Lower means the points sit closer to their cluster center.
Choosing K: The tricky part is deciding how many groups really make sense. The elbow method helps you notice when adding more clusters stops buying you much.

Step-by-Step K-Means

Press start and watch the two repeating moves: assign points, then move centroids.

Iterations: 0
Phase: Init
Points: 0/90 assigned

Choosing K — The Elbow Method

How many clusters should you use? The "Elbow Method" is a rule of thumb: keep adding clusters until the improvement slows down. It's like eating pizza—the first slice is amazing, the fifth one is just okay.

Red dot = elbow at K=3. Adding more clusters beyond this gives diminishing returns.

Inertia = WCSSThis is the total squared distance from each point to its assigned centroid. It always goes down as K increases, so you should not blindly chase the smallest possible value.
Other methods for choosing K
  • Silhouette score: asks whether points are close to their own cluster and far from other clusters
  • Gap statistic: compares your clustering result to what random data would look like
  • Domain knowledge: sometimes you already know how many groups make sense

Other Clustering Methods

DBSCANUseful when the groups have messy shapes and some points should really be treated as noise instead of forced into a cluster.
HierarchicalBuilds clusters inside bigger clusters, almost like a family tree showing which groups sit inside others.
GMMAllows softer membership, so a point can partly belong to more than one cluster instead of getting a strict yes-or-no label.