K-Means Clustering
Comprehensive notes, formulas, and practice questions for K-Means Clustering.
What you'll learn
- The difference between supervised and unsupervised learning
- The four steps of the K-Means algorithm
- How to choose the right number of clusters K using the elbow method
- Limitations of K-Means (sensitivity to initialisation, non-convex shapes)
- Real-world applications of clustering
Key concepts
Unsupervised Learning
In unsupervised learning, data has no labels. The algorithm finds hidden structure — natural groupings — in the data on its own.
| Learning Type | Labels? | Goal | Example |
|---|---|---|---|
| Supervised | Yes | Predict a label | Spam detection |
| Unsupervised | No | Discover structure | Customer segmentation |
The K-Means Algorithm
Goal: Partition n data points into K clusters such that each point belongs to the cluster with the nearest centroid.
Step-by-step:
| Step | Action |
|---|---|
| 1. Initialise | Randomly place K centroids in the data space |
| 2. Assign | Assign each data point to the nearest centroid |
| 3. Update | Recalculate each centroid as the mean of all points assigned to it |
| 4. Repeat | Repeat steps 2–3 until centroids stop moving (convergence) |
Distance metric (Euclidean):
d(x, c) = √[(x₁ − c₁)² + (x₂ − c₂)²]
Convergence: The algorithm converges when no data point changes its cluster assignment between iterations.
Worked Example (2D)
Data: students clustered by (study hours, exam score).
Iteration 1: Centroids placed randomly
Iteration 2: Points assigned to nearest centroid
Iteration 3: Centroids shift to group means
Iteration 4: Converged — cluster centres stable
Result: Three natural groups — high-study/high-score, low-study/low-score, mid-range.
Choosing K: The Elbow Method
Run K-Means for K = 1, 2, 3, … and record the Within-Cluster Sum of Squares (WCSS) — total squared distance from each point to its centroid.
| K | WCSS |
|---|---|
| 1 | 5000 |
| 2 | 2000 |
| 3 | 800 |
| 4 | 750 |
| 5 | 740 |
Plot K vs WCSS. The "elbow" — where the drop in WCSS significantly slows — suggests the optimal K. Here: K = 3.
Limitations of K-Means
| Limitation | Description |
|---|---|
| Sensitive to initialisation | Different starting centroids can produce different final clusters |
| Requires K upfront | K must be chosen before running — often not obvious |
| Assumes spherical clusters | Performs poorly on elongated or non-convex cluster shapes |
| Sensitive to outliers | Outliers pull centroids away from true cluster centres |
| Not suitable for categorical data | Euclidean distance is not meaningful for non-numeric features |
Fix for initialisation sensitivity: Use K-Means++ initialisation — centroids are chosen with probability proportional to distance from already-chosen centroids, spreading them out intelligently.
Applications of K-Means
| Domain | Application |
|---|---|
| Business | Customer segmentation by purchasing behaviour |
| Healthcare | Grouping patients by symptoms for diagnosis |
| Image processing | Colour quantisation (reducing colours in an image) |
| Geography | Identifying urban zones, traffic clustering |
| Biology | Grouping genes with similar expression patterns |
Quick check
-
K-Means is an unsupervised algorithm. What does this mean, and how does it differ from a supervised algorithm like logistic regression?
-
Describe the four steps of the K-Means algorithm in your own words. What is the termination condition?
-
A student runs K-Means with K = 3 twice and gets different cluster assignments each time. What causes this, and how can it be fixed?
-
The elbow method gives a WCSS plot that shows a sharp drop from K=1 to K=4, then a very gradual decrease for K=5, 6, 7. What value of K should be chosen?
-
K-Means performs poorly on a dataset where two clusters form crescent (moon) shapes. Explain why, and name a clustering algorithm better suited to non-convex shapes.
Key Takeaways (TL;DR)
- What you'll learn
- Key concepts
- Quick check
Master this topic with Drishti OS
Get unlimited mock tests, AI-powered mentorship, and complete video courses when you join.
Start Free Practice