All articles
Machine Learning
ml-unsupervised

Mastering K-Means Clustering: An Essential Guide for Machine Learning Students

Unlock the fundamentals of K-means clustering, the cornerstone of unsupervised learning. Learn how to partition data into meaningful groups with step-by-step examples.

Math Instructor AI 22 September 2026 8 min read

Introduction to K-Means Clustering

In the landscape of machine learning, unsupervised learning stands out as a powerful approach for discovering hidden patterns in unlabelled data. Unlike supervised learning, where we rely on target labels, unsupervised learning tasks—specifically clustering—require the algorithm to find structure on its own. K-means clustering is arguably the most popular and intuitive algorithm for this purpose, making it a fundamental topic for any undergraduate machine learning module.

Understanding K-means is not just about memorising steps; it is about grasping how we mathematically define 'similarity' and 'grouping'. Whether you are preparing for an exam or building your first model, mastering this algorithm provides the foundation for more complex techniques like dimensionality reduction and anomaly detection. This guide will walk you through the mechanics of the algorithm, the mathematics of centroids, and the practical pitfalls you must avoid.

The Core Algorithm

K-means clustering aims to partition $n$ observations into $k$ distinct clusters. Each cluster is defined by a centroid, which acts as the 'centre of gravity' for that group. The algorithm follows an iterative process to minimise the within-cluster sum of squares, often referred to as inertia.

  1. Initialisation: Choose $k$ initial centroids. These can be selected randomly from the dataset or via other heuristics.
  2. Assignment: Assign each data point $x_i$ to the nearest centroid $\mu_j$ based on the Euclidean distance: $||x_i - \mu_j||^2$.
  3. Update: Recompute the centroid $\mu_j$ by taking the mean of all points assigned to that cluster: $\mu_j = \frac{1}{|C_j|} \sum_{x \in C_j} x$.
  4. Convergence: Repeat the assignment and update steps until the centroids no longer change significantly or a maximum number of iterations is reached.

Worked Example 1: Simple 2D Clustering

Let us consider a small dataset with four points in 2D space: $A(1, 1)$, $B(2, 1)$, $C(4, 3)$, and $D(5, 4)$. We want to find $k=2$ clusters.

Step 1: Initialisation Let us pick $A(1, 1)$ as $\mu_1$ and $D(5, 4)$ as $\mu_2$.

Step 2: Assignment (Iteration 1)

  • Point $A(1, 1)$: Distance to $\mu_1$ is 0, to $\mu_2$ is $\sqrt{(5-1)^2 + (4-1)^2} = 5$. Assign to $C_1$.
  • Point $B(2, 1)$: Distance to $\mu_1$ is 1, to $\mu_2$ is $\sqrt{(5-2)^2 + (4-1)^2} = \sqrt{18} \approx 4.24$. Assign to $C_1$.
  • Point $C(4, 3)$: Distance to $\mu_1$ is $\sqrt{(4-1)^2 + (3-1)^2} = \sqrt{13} \approx 3.6$, to $\mu_2$ is $\sqrt{(5-4)^2 + (4-3)^2} = \sqrt{2} \approx 1.41$. Assign to $C_2$.
  • Point $D(5, 4)$: Distance to $\mu_1$ is 5, to $\mu_2$ is 0. Assign to $C_2$.

Step 3: Update

  • New $\mu_1 = \text{mean}({A, B}) = (1.5, 1)$.
  • New $\mu_2 = \text{mean}({C, D}) = (4.5, 3.5)$.

In the next iteration, the assignments remain the same, so the algorithm has converged.

Worked Example 2: Centroid Calculation

Suppose after an assignment step, Cluster 1 contains points $(2, 2), (4, 4), (6, 6)$. To update the centroid $\mu_1$:

$$\mu_1 = \left( \frac{2+4+6}{3}, \frac{2+4+6}{3} \right) = (4, 4)$$

This demonstrates that the centroid is simply the arithmetic mean of the coordinates of all points assigned to that cluster. If a cluster becomes empty, the algorithm may need a strategy to re-initialise that centroid to avoid a division by zero error.

Common Mistakes

  • Sensitive to Initialisation: K-means is a local search algorithm. Starting with poor initial centroids can lead to suboptimal clustering. Always consider running the algorithm multiple times with different random seeds.
  • Ignoring Feature Scaling: Because K-means relies on Euclidean distance, features with larger ranges will dominate the distance calculation. Always standardise your data (e.g., using Z-score normalisation) before clustering.
  • Assuming Spherical Clusters: K-means assumes clusters are convex and isotropic. It struggles with elongated or non-spherical shapes, which often leads to poor results on complex datasets.
  • Misinterpreting K: Choosing the wrong number of clusters is a frequent error. Use the 'Elbow Method' (plotting inertia against $k$) to find the optimal balance between model complexity and cluster cohesion.

Frequently Asked Questions

What is the difference between K-means and K-nearest neighbours? K-means is an unsupervised clustering algorithm used to group data, whereas K-nearest neighbours is a supervised classification or regression algorithm used to predict labels based on proximity.

Does K-means always converge? Yes, K-means is guaranteed to converge to a local optimum because each step (assignment and update) strictly decreases the total within-cluster sum of squares.

How do I handle outliers? K-means is highly sensitive to outliers because they can significantly pull the mean (centroid) away from the true centre of a cluster. Consider removing outliers or using K-medoids as a more robust alternative.

Conclusion

K-means clustering is a vital tool in your machine learning toolkit, offering a clear, iterative approach to unsupervised learning. By understanding the mechanics of centroid updates and the importance of feature scaling, you are well-equipped to tackle clustering problems in your coursework. To see these concepts in action with interactive visualisations, head over to MathInstructor AI and generate a free animated lesson on K-means clustering today.

Topics

k means clustering
unsupervised learning
machine learning
clustering
centroids
ml-unsupervised
data science
algorithm convergence
feature scaling

Want this explained out loud?

Turn any question into a narrated, animated lesson in seconds.

Try the Studio free