Recommended resources
The freely available resources this lesson will direct you to.
Introduction
In our previous module, we concluded our exploration of advanced supervised learning methods by dissecting the kernel trick in Support Vector Machines, a powerful technique for handling non-linear data. We are now pivoting from a world where data comes with explicit labels to one where it does not. This marks our entry into unsupervised learning, a paradigm focused on discovering inherent structure, patterns, and representations directly from the data itself.
This lesson introduces one of the most fundamental and widely used unsupervised learning algorithms: K-means clustering. Our objective is to explain the algorithm, formally define its mathematical objective function, and detail the iterative optimization process it uses to find a solution. You will learn how K-means partitions data into groups and what "optimality" means in this context, providing a foundational pillar for understanding pattern discovery in unlabeled datasets.
What is Clustering?
At its core, clustering is the task of partitioning a set of data points into a number of groups, or clusters, such that points within the same cluster are more "similar" to one another than to those in other clusters. This is a cornerstone of exploratory data analysis, with applications spanning numerous domains:
- Marketing: Segmenting customers into groups with similar purchasing habits for targeted campaigns.
- Biology: Grouping genes with similar expression patterns to identify functional pathways.
- Computer Vision: Segmenting an image into regions of similar color or texture.
- Natural Language Processing: Grouping documents by topic.
To perform clustering, we first need a mathematical definition of "similarity." For algorithms like K-means, which operate on data in a vector space , similarity is typically quantified by a distance metric, most commonly the squared Euclidean distance.
The K-means Objective Function
The K-means algorithm aims to partition data points into a pre-specified number, , of clusters. The core idea is to find cluster centroids and assign each data point to the nearest centroid, such that the total "within-cluster sum of squares" (WCSS) is minimized.
This objective can be expressed formally. Let be a binary indicator variable, where if data point is assigned to cluster , and otherwise. The objective function, often denoted by , is the sum of squared Euclidean distances from each point to the centroid of its assigned cluster:
The goal of K-means is to find the set of assignments and centroids that minimize this function .
A deeper look at this objective reveals that it is equivalent to minimizing the average pairwise squared distance between points within the same cluster, a natural measure of cluster cohesion. The following resource provides a formal definition of the K-means criterion and shows this equivalence. It also makes a critical point for someone with your background: finding the global minimum of this objective is an NP-hard problem.
[PDF] 1 Clustering 2 The k-means criterion - UC Davis Mathematics
This document from UC Davis formally introduces the K-means objective function.
Read the section titled "The k-means criterion". Pay close attention to the derivation that shows the equivalence between minimizing the intra-cluster pairwise distances (Equation 1) and minimizing the sum of squared distances to the cluster centroids (Equation 2). Note the statement that solving this problem is NP-hard.
The Iterative Optimization Process: Lloyd's Algorithm
Since finding the exact solution to the K-means objective is computationally intractable, we rely on a heuristic iterative method known as Lloyd's Algorithm. This algorithm is a classic example of coordinate descent, an optimization strategy where you iteratively minimize a function with respect to different subsets of variables, one at a time. In this case, the two subsets of variables are the cluster assignments and the cluster centroids .
The algorithm proceeds as follows:
-
Initialization: Randomly initialize the cluster centroids . A common strategy is to pick random data points from the dataset to serve as the initial centroids.
-
Iterative Refinement: Repeat the following two steps until the cluster assignments no longer change:
- Assignment Step (E-step): For each data point , assign it to the closest cluster centroid. This step minimizes the objective with respect to the assignments , holding the centroids fixed.
- Update Step (M-step): For each cluster , recalculate its centroid as the mean of all data points assigned to it. This step minimizes the objective with respect to the centroids , holding the assignments fixed.
The names "E-step" and "M-step" are borrowed from the more general Expectation-Maximization (EM) algorithm, which we will encounter later. For K-means, the "Expectation" corresponds to assigning points, and "Maximization" (or in this case, minimization) corresponds to updating the centroids.
The following resource provides a clear pseudocode for Lloyd's algorithm and a rigorous derivation of these two steps as a coordinate descent procedure on the objective function.
[PDF] K-Means Clustering and Related Algorithms - cs.Princeton
This document from Princeton University provides a clear and complete derivation of the K-means algorithm.
First, familiarize yourself with the pseudocode in Algorithm 1. This clearly lays out the iterative process. Next, read section 3, "Derivation", carefully. It explains how the algorithm performs coordinate descent on the objective function. The subsection "Minimizing the rn" formally justifies the assignment step. The subsection "Minimizing the µk" provides the calculus-based derivation for the update step by taking the gradient of the objective function with respect to \boldsymbol{\mu}_k and setting it to zero.
For a different perspective, the following two short video lectures walk through the exact same derivation, which may help reinforce the concepts.
(ML 16.1) K-means clustering (part 1)
The first part of this two-part lecture series by mathematicalmonk introduces the K-means objective and the assignment step.
Watch from the beginning to understand the intuition behind K-means. Then, see the formal introduction of the objective function. Finally, observe the explanation of the alternating optimization process and the derivation of the assignment step.
(ML 16.2) K-means clustering (part 2)
The second video in the series derives the centroid update rule.
Watch from we want to minimize for the detailed vector calculus derivation of the update step, where the gradient of the loss is set to zero. The lecturer then confirms that the solution is a minimum by checking the Hessian. Finally, watch the summary of the complete algorithm and its convergence properties.
Visualizing the Algorithm
The iterative nature of K-means is best understood visually. The algorithm begins with randomly placed centroids, and with each iteration, the centroids move towards the center of their assigned points, and the cluster boundaries (known as a Voronoi tessellation) shift accordingly. This process continues until the assignments stabilize.

This iterative process is guaranteed to converge. At each step (both assignment and update), the value of the objective function either decreases or stays the same. Since is bounded below by zero, the algorithm must converge to a local minimum. However, because the objective function is non-convex, this local minimum is not guaranteed to be the global minimum. The final result is sensitive to the initial placement of the centroids. For this reason, in practice, K-means is often run multiple times with different random initializations, and the solution with the lowest objective function value is chosen.
Conclusion
In this lesson, we have formally introduced K-means, a foundational algorithm in unsupervised learning. You now have a theoretical grasp of its core components.
The key takeaways are:
- Clustering is an unsupervised learning task for finding group structures in unlabeled data.
- The K-means objective function seeks to minimize the within-cluster sum of squares (WCSS), which is the total squared Euclidean distance between each data point and the centroid of its assigned cluster.
- Solving the K-means objective is NP-hard, necessitating a heuristic approach.
- Lloyd's Algorithm is an iterative optimization process based on coordinate descent. It alternates between an assignment step (assigning points to the nearest centroid) and an update step (re-computing centroids as the mean of assigned points).
- The algorithm is guaranteed to converge to a local minimum of the objective function, but its sensitivity to initialization means multiple runs are often required in practice.
This lesson has established the principle of finding structure through geometric partitioning. Our next topic will address another fundamental task in unsupervised learning: dimensionality reduction. We will begin by exploring Principal Component Analysis (PCA), a technique that finds a lower-dimensional representation of data while preserving as much variance as possible.