Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 71

Understanding K-Means Clustering

Recommended resources

Introduction

Welcome back! In our previous lesson, we established the "what" and "why" of clustering: it's a fundamental unsupervised learning technique for discovering hidden groups in unlabeled data. You learned that its goal is to partition data so that points within a cluster are similar, and points between clusters are dissimilar, with applications ranging from customer segmentation to anomaly detection.

Today, we move from the "why" to the "how." Your learning outcome is to describe the k-means clustering algorithm step-by-step. This is one of the most widely-used clustering algorithms due to its simplicity and effectiveness. In line with your preference for a theory-first approach, we will first understand the mathematical objective k-means tries to achieve, and then we will break down the iterative process it uses to get there.


The Objective: What Makes a Good Clustering?

At its core, k-means is an optimization algorithm. It aims to find the best cluster assignments by minimizing a specific mathematical quantity. Intuitively, we want clusters that are "tight" or "compact." This means the data points within a cluster should be as close as possible to that cluster's center.

K-means formalizes this idea by trying to minimize the Within-Cluster Sum of Squares (WCSS), also known as inertia.

To understand this, let's look at the foundational math.

K-means Clustering: Complete Guide with Algorithm, Implementation & Best Practices

The article 'K-means Clustering: Complete Guide' provides an excellent mathematical foundation. We will focus on the section that defines the objective function.

Read the sections titled 'The Core Question: What Makes a Good Clustering?' and 'Building the Objective Function'. Focus on understanding the WCSS formula and what each part represents.

As you just read, the objective is to find the clusters that minimize this function:

Let's break this down:

  • : The number of clusters you want to find (you have to specify this).
  • : The set of all data points belonging to cluster .
  • : The centroid of cluster , which is the mean (average position) of all points in that cluster.
  • : The squared Euclidean distance from a data point to the center of its own cluster . Squaring the distance penalizes points that are far from their centroid more heavily.

In simple terms, k-means is trying to find the cluster centers and assignments that make all the clusters as tight as possible. Finding the absolute best solution is computationally very difficult (NP-hard), so k-means uses a clever iterative approach to find a good solution.


The K-Means Algorithm: A Step-by-Step Guide

The algorithm works by repeating two main phases—assigning points and updating centroids—until it finds a stable solution. Let's walk through the process.

K-Means Clustering Step-by-Step Example
This diagram provides a high-level visual of the iterative process in k-means. It starts with unclustered data, then shows the cycle of assigning points to centroids and updating the centroids' positions until the final clusters are formed.

Now let's break down each step in detail.

Master K-Means Clustering: Intuition to Code | Let's Data Science

The 'LetsDataScience' blog post, 'Mastering K-Means Clustering', offers a very clear and concise breakdown of the algorithm's iterative steps, explaining each one in plain English alongside the mathematics.

Read the subsection titled 'The Iterative Steps'. This will give you a clear, four-step summary of the algorithm: Initialize, Assignment, Update, and Convergence.

Let's solidify those steps with a more detailed explanation and a visual aid.

StatQuest: K-means clustering

StatQuest videos are renowned for their clarity. This video by Josh Starmer will walk you through the entire k-means process with simple, intuitive animations.

Watch the video from the beginning to 04:12. Pay close attention to how each step you just read about is visualized: Step 1: Initialization (01:04) - Choosing K and randomly placing the initial centroids. Step 2: Assignment (01:34) - Measuring the distance from each point to the centroids and assigning it to the closest one. Step 3: Update (02:44) - Calculating the new centroid by finding the mean of all points in a cluster. Step 4: Convergence (02:55) - Repeating the assignment and update steps until the cluster assignments no longer change.

To summarize, the step-by-step process is:

  1. Initialization Step:

    • Choose the number of clusters, . This is a critical parameter that you, the user, must provide.
    • Randomly initialize centroids. These are the starting "guesses" for the centers of your clusters. A common way is to pick random data points from your dataset to be the initial centroids.
  2. Assignment Step:

    • For each data point in your dataset, calculate its distance to every one of the centroids. The most common distance metric used is the Euclidean distance.
    • Assign each data point to the cluster of the nearest centroid. This step effectively partitions the data space into regions (called Voronoi cells).
  3. Update Step:

    • For each of the clusters, recalculate its centroid by taking the mean (average) of all the data points that were assigned to it in the previous step.
    • This moves the centroid to the new "center of gravity" of its assigned points.
  4. Convergence Step:

    • Repeat the Assignment Step and Update Step until a stopping criterion is met.
    • Convergence occurs when the cluster assignments no longer change between iterations, or the centroids move by less than a certain small threshold. At this point, the algorithm has found a stable (though not necessarily perfect) solution.

A Concrete Example

Let's walk through a simple numerical example to see these steps in action.

K-means Clustering: Complete Guide with Algorithm, Implementation & Best Practices

To solidify your understanding, let's review a worked-out example from the 'K-means Clustering: Complete Guide' article. This makes the abstract steps concrete.

Read the 'Example' section. Follow the calculations for the 8 data points and k=2 clusters. Notice how the algorithm converges very quickly in this simple case because the initial centroids were well-placed.

This example demonstrates how the iterative process of assigning points and updating centers leads to a logical grouping of the data.


Important Considerations

As you've seen, the k-means algorithm is straightforward and powerful. However, its behavior depends on two key choices you make.

  1. The choice of : The algorithm cannot determine the "correct" number of clusters on its own. If you ask for 3 clusters in data that clearly has 2, it will still give you 3 clusters. Choosing an appropriate value for is a crucial part of the modeling process.
  2. The initial placement of centroids: K-means is sensitive to where the centroids start. A different random initialization can lead to a different final clustering. As the StatQuest video mentioned, sometimes the algorithm can find a "terrible" clustering. To mitigate this, standard practice is to run the algorithm multiple times with different random initializations and choose the clustering that results in the lowest WCSS.

We will not delve deeper into solving these issues today, but it is important to be aware of them.


Conclusion

In this lesson, you have dissected the k-means algorithm, moving from its theoretical objective to its practical, step-by-step execution. You now understand how this iterative process helps solve the optimization problem of finding compact clusters in unlabeled data.

Key Takeaways:

  • The objective of k-means is to minimize the Within-Cluster Sum of Squares (WCSS), which measures the total squared distance between each point and its cluster's centroid.
  • The algorithm operates through a four-step iterative cycle:
    1. Initialize: Choose and place random initial centroids.
    2. Assign: Assign each data point to its nearest centroid.
    3. Update: Recalculate each centroid as the mean of its assigned points.
    4. Converge: Repeat the assign and update steps until the cluster assignments stabilize.
  • The final result can be sensitive to the initial choice of and the random starting positions of the centroids.

Preview of the Next Lesson:
Now that you have mastered the theory behind the k-means algorithm, you are ready to apply it. In the next lesson, you will implement k-means clustering using the scikit-learn library in Python. You will also learn about a common technique, the Elbow Method, to help you select the optimal number of clusters, .