You're offline — cached pages and worlds still work
Drishti Innovations logo
Drishti Innovations

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 TypeLabels?GoalExample
SupervisedYesPredict a labelSpam detection
UnsupervisedNoDiscover structureCustomer 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:

StepAction
1. InitialiseRandomly place K centroids in the data space
2. AssignAssign each data point to the nearest centroid
3. UpdateRecalculate each centroid as the mean of all points assigned to it
4. RepeatRepeat 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.

KWCSS
15000
22000
3800
4750
5740

Plot K vs WCSS. The "elbow" — where the drop in WCSS significantly slows — suggests the optimal K. Here: K = 3.

Limitations of K-Means

LimitationDescription
Sensitive to initialisationDifferent starting centroids can produce different final clusters
Requires K upfrontK must be chosen before running — often not obvious
Assumes spherical clustersPerforms poorly on elongated or non-convex cluster shapes
Sensitive to outliersOutliers pull centroids away from true cluster centres
Not suitable for categorical dataEuclidean 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

DomainApplication
BusinessCustomer segmentation by purchasing behaviour
HealthcareGrouping patients by symptoms for diagnosis
Image processingColour quantisation (reducing colours in an image)
GeographyIdentifying urban zones, traffic clustering
BiologyGrouping genes with similar expression patterns

Quick check

  1. K-Means is an unsupervised algorithm. What does this mean, and how does it differ from a supervised algorithm like logistic regression?

  2. Describe the four steps of the K-Means algorithm in your own words. What is the termination condition?

  3. 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?

  4. 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?

  5. 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