2.5.2Unsupervised Learning

Choosing K (elbow method, silhouette score)

2,143 words10 min readdifficulty · medium1 backlinks

The Elbow Method

The elbow method finds the K where adding more clusters stops giving you much benefit.

How It Works: Inertia as Your Guide

WHY this formula?
Each term xμi2\|\mathbf{x} - \boldsymbol{\mu}_i\|^2 is the squared distance from a point to its cluster center. Summing over all points in all clusters gives total "compactness". Lower inertia = tighter clusters.

Derivation from First Principles:

  1. Goal: Minimize variance within each cluster

    • Variance measures spread. For cluster CiC_i with centroid μi\boldsymbol{\mu}_i: Var(Ci)=1CixCixμi2\text{Var}(C_i) = \frac{1}{|C_i|}\sum_{\mathbf{x} \in C_i} \|\mathbf{x} - \boldsymbol{\mu}_i\|^2
    • Total variance across all clusters: multiply by Ci|C_i| and sum → that's inertia!
  2. Why squared distance?

    • Penalizes outliers heavily (point 2× farther contributes 4× to inertia)
    • Yields a closed-form centroid: minimizing xμ2\sum \|\mathbf{x} - \boldsymbol{\mu}\|^2 over μ\boldsymbol{\mu} gives μ=mean\boldsymbol{\mu} = \text{mean} of the points. This is why K-means updates centroids to the mean—it's a coordinate-descent (Lloyd's algorithm), alternating point-assignment and mean-recomputation, not gradient descent.
    • Matches Euclidean geometry assumption
  3. The Elbow:

    • As K increases, inertia always decreases (more clusters = points closer to some centroid)
    • At K = N (number of points), inertia = 0 (each point is its own cluster)
    • But somewhere between K=1 and K=N, the rate of decrease sharply slows—that's the elbow!
Figure — Choosing K (elbow method, silhouette score)

HOW to Apply:

  1. Run K-means for = 1, 2, 3, .., K_max
  2. Record inertia for each K
  3. Plot K (x-axis) vs Inertia (y-axis)
  4. Find the "elbow"—where the curve bends sharply

Silhouette Score

The silhouette score measures how well each point fits its cluster compared to neighboring clusters.

Derivation: Cohesion vs Separation

For a single point xi\mathbf{x}_i in cluster CIC_I:

  1. Cohesion (how close to own cluster):
    a(i)=1CI1xjCI,jid(xi,xj)a(i) = \frac{1}{|C_I| - 1} \sum_{\mathbf{x}_j \in C_I, j \neq i} d(\mathbf{x}_i, \mathbf{x}_j) Average distance to all other points in the same cluster.
    WHY CI1|C_I| - 1 in the denominator? There are CI|C_I| points in the cluster, but we exclude xi\mathbf{x}_i itself, leaving CI1|C_I| - 1 neighbors to average over.

  2. Separation (how far from nearest other cluster):
    b(i)=minJI1CJxjCJd(xi,xj)b(i) = \min_{J \neq I} \frac{1}{|C_J|} \sum_{\mathbf{x}_j \in C_J} d(\mathbf{x}_i, \mathbf{x}_j) Average distance to points in the nearest neighboring cluster CJC_J.
    WHY minimum? We care about the closest alternative—if xi\mathbf{x}_i is far from all other clusters, it's well-placed.

  3. Silhouette coefficient:
    s(i)=b(i)a(i)max(a(i),b(i))s(i) = \frac{b(i) - a(i)}{\max(a(i), b(i))}

Derivation logic:

  • If xi\mathbf{x}_i is well-clustered: a(i)a(i) small (close to own cluster), b(i)b(i) large (far from others) → s(i)1s(i) \approx 1
  • If poorly clustered: a(i)b(i)a(i) \approx b(i)s(i)0s(i) \approx 0
  • If misclassified: a(i)>b(i)a(i) > b(i)s(i)<0s(i) < 0

WHY divide by max? Normalizes to [1,1][-1, 1] range regardless of distance scale.

Using Silhouette to Choose K

  1. Compute silhouette score for K = 2, 3, .., K_max
  2. Pick K with highest average silhouette
  3. Bonus: Plot silhouette for each point (silhouette diagram)—clusters with many negative scores are poorly defined

Combining Elbow and Silhouette

Method What It Optimizes Strength Weakness
Elbow Inertia drop rate Simple, fast Subjective (where's the elbow?)
Silhouette Cohesion vs separation Quantitative score Biased toward fewer clusters

Best practice workflow:

  1. Plot elbow curve → identify 2-3 candidate K values
  2. Compute silhouette for those candidates
  3. Check silhouette diagram (per-point scores)—look for balanced cluster sizes, few negatives
  4. Validate with domain experts or downstream task
Recall Explain to a 12-Year-Old

Imagine you're sorting your toys into boxes. You could use1 giant box (everything together) or 100 tiny boxes (one per toy). Neither is useful!

Elbow method: Make a graph of "how mesy the boxes are" vs. "number of boxes." At first, adding boxes helps a LOT (mesy drops fast). But then it barely helps. The spot where it stops helping much? That's the elbow—your sweet spot.

Silhouette score: For each toy, check: "Am I way closer to toys in my box than toys in other boxes?" If yes, you get a high score. If you're kinda in-between boxes, low score. Average everyone's score—higher = better sorting.

Use both! Elbow finds where to stop, silhouette checks if your sorting actually makes sense.


Connections

  • K-Means Clustering: These methods only work for algorithms that need K upfront
  • DBSCAN: Density-based clustering doesn't require choosing K (finds it automatically)
  • Hierarchical Clustering: Use dendrograms instead of elbow/silhouette
  • Gap Statistic: Advanced alternative—compares inertia to null distribution
  • Hyperparameter Tuning: Choosing K is like choosing learning rate—requires validation
  • Bias-Variance Tradeoff: Too few clusters = high bias, too many = high variance (overfitting noise)

Flashcards

#flashcards/ai-ml

What does inertia measure in K-means? :: Within-cluster sum of squared distances—how tightly points cluster around centroids. Lower = tighter clusters.

Elbow method: What is the "elbow"?
The K value where inertia stops decreasing rapidly. After the elbow, adding clusters gives diminishing returns.
Why does inertia always decrease as K increases?
More clusters means points are closer to some centroid. At K=N (each point is its own cluster), inertia = 0.
Why does K-means use the mean as the centroid update?
Because minimizing sum of squared distances has a closed-form solution: the minimizer is the mean. This makes Lloyd's algorithm a coordinate-descent, not gradient descent.

Silhouette coefficient formula point i :: s(i)=b(i)a(i)max(a(i),b(i))s(i) = \frac{b(i) - a(i)}{\max(a(i), b(i))} where a(i)a(i) = avg distance to own cluster, b(i)b(i) = avg distance to nearest other cluster.

In the cohesion term a(i), why is the denominator |C_I| - 1?
The cluster has |C_I| points, but we exclude the point itself, leaving |C_I| - 1 neighbors to average distances over.
Silhouette score ranges and interpretation
Range: [-1, 1]. 1 = perfect (tight, separated), 0 = overlapping clusters, -1 = misclassified points.
Why does silhouette often favor K=2?
Splitting data in half maximizes separation. With more clusters, some inevitably overlap, lowering the score. Domain knowledge must override this bias.
What does a negative silhouette coefficient mean for a point?
The point is closer to a neighboring cluster than its own—likely misclassified.
Elbow method weakness
The elbow can be ambiguous or absent in mesy real-world data (no sharp bend in the curve).
Why divide silhouette by max(a, b)?
Normalizes the score to [-1, 1] regardless of distance scale, making it comparable across datasets.

Best practice for choosing K :: Use elbow + silhouette + domain knowledge together. No single method is perfect; they vote on the best K.

Concept Map

needed by

requires K before running

method 1

method 2

uses

sums

minimizes

closed-form solution

enables

always decreases as K grows

bend where benefit slows

selects

validates

Choosing K

K-means clustering

Elbow Method

Silhouette Score

Inertia WCSS

Squared distances to centroid

Within-cluster variance

Centroid equals mean

Lloyd's algorithm

Plot K vs Inertia

Elbow point

Optimal K

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Jab ap K-means clustering karte ho, toh sabse pehla sawal ata hai—kitne clusters chahiye? Yeh number randomly nahi choose kar sakte, kyunki galat K se ya toh sab dataek pile mein mix ho jaayega, ya phir bahut zyada chhote-chhote groups ban jaayenge jo kaam ke nahi. Elbow method aur silhouette score do famous techniques hain jo is problem ko solve karti hain.

Elbow method ka logic simple hai—ap different values of K try karte ho (jaise 2, 3, 4 clusters) aur dekhte ho ki har K pe "inertia" (matlab points apne cluster center se kitna door hain) kaise change hoti hai. Initially jab K badhate ho, inertia fast drop hoti hai, lekin ek point ke bad improvement bahut slow ho jata hai. Us turning point ko "elbow" kehte hain, aur wahi optimal K hota hai. Ek important baat—K-means centroid ko har baar mean pe update karta hai, kyunki squared distance minimize karne ka closed-form solution hi mean hota hai. Ye

Go deeper — visual, from zero

Test yourself — Unsupervised Learning

Connections