Skip to main content

Welcome to a Grasp lesson

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

Lesson 49

K-Means Clustering: Implementation and Interpretation

Recommended resources

Hello! Welcome to a new module in your data science journey. We are now shifting from supervised learning, where the goal was to predict a known target like a house price or a customer's churn status, to the fascinating world of unsupervised learning. Here, our goal is not to predict anything specific, but to discover hidden patterns, structures, and relationships within the data itself.

Our first topic in this new domain is clustering, a technique for grouping similar data points together. Today, we will focus on the most fundamental and popular clustering algorithm: K-Means.

By the end of this 60-minute lesson, you will be able to apply K-Means clustering and interpret the resulting clusters. This is a foundational skill in unsupervised learning, used for tasks like customer segmentation, anomaly detection, and image compression.

Let's begin by visualizing the core idea.

K-Means Clustering: Before and After
This image illustrates the goal of K-Means. On the left, we have a set of unorganized data points. On the right, K-Means has grouped them into three distinct clusters based on their similarity.

1. Understanding the K-Means Algorithm

K-Means aims to partition a dataset into a pre-determined number of clusters, denoted by 'K'. It does this by finding 'K' central points, called centroids, and assigning each data point to the cluster of its nearest centroid. The "nearness" is typically measured using Euclidean distance—the straight-line distance between two points.

The algorithm is iterative and follows a two-step process called expectation-maximization:

  1. Initialization: First, we must choose the number of clusters, K. Then, the algorithm randomly places K centroids in the feature space.
  2. Assignment Step (Expectation): Each data point is assigned to the cluster whose centroid is closest to it.
  3. Update Step (Maximization): The position of each centroid is recalculated to be the mean (the average) of all data points currently assigned to its cluster.
  4. Convergence: Steps 2 and 3 are repeated until the centroids no longer move significantly, meaning the cluster assignments have stabilized.

The algorithm's objective is to minimize the Sum of Squared Errors (SSE), which is the sum of the squared Euclidean distances of each point to its closest centroid. A lower SSE generally indicates a better clustering solution.

The animated GIF below shows this iterative process in action. Notice how the centroids (black 'x' marks) move with each iteration and how the cluster assignments (colors) change until they stabilize. It also highlights that the random starting positions of the centroids can lead to different final clusters and SSE values.

K-Means Clustering: Impact of Initialization on First Iteration
This animation shows the K-Means algorithm iterating. Data points are assigned to the nearest centroid, and centroids are then updated to the mean of their assigned points. This process repeats until the clusters are stable. The two initializations show that the starting point matters.

To solidify your understanding of these steps, please read the following brief article which lays out the recipe for K-Means.

The K-means algorithm: A step-by-step recipe

The article 'Getting started with K-means clustering in Python' from Domino Data Lab provides a clear, step-by-step conceptual breakdown of the algorithm.

Please read the section titled 'The K-means algorithm: A step-by-step recipe'. Focus on understanding the sequence of the six steps outlined.



2. Applying K-Means in Python with Scikit-learn

Now that you understand the theory, let's put it into practice. As you know from your previous work with supervised learning, scikit-learn provides a powerful and easy-to-use implementation. For K-Means, we'll use the KMeans class from sklearn.cluster.

A critical prerequisite for K-Means is feature scaling. Because the algorithm relies on distances, features with larger scales and variances (e.g., annual income in thousands of dollars) can dominate features with smaller scales (e.g., age in years), leading to poor results. You've already learned about StandardScaler and MinMaxScaler; these are essential here.

The following video will guide you through a complete, practical example of applying K-Means for customer segmentation. This is a classic use case where a business wants to understand its customer base by grouping them into distinct segments.

Sklearn K-Means Python Example | Interpreting Clustering results

This video from Kunaal Naik provides an excellent walkthrough of K-Means using a mall customer dataset. It covers everything from initial data exploration to the final, crucial step of interpreting what the clusters actually mean.

Please watch the video, paying close attention to the following stages: Data Exploration (00:00 - 05:21): Notice how pair plots are used to visually inspect the data and form an initial hypothesis about potential clusters. This is a key part of the EDA process we've discussed. Applying K-Means (05:21 - 06:54): Observe the simple syntax for initializing and fitting the KMeans model from scikit-learn. The presenter chooses k=5 based on the visual exploration. Assigning Labels (06:54 - 08:11): See how the predicted cluster labels are accessed via the .labels_ attribute and added as a new column to the original DataFrame. This is a vital step for interpretation. Visualizing and Interpreting Clusters (09:06 - 11:45): This is the most important part. Focus on how the presenter plots the clusters and then translates the visual patterns into actionable business insights by describing the characteristics of each customer segment.



3. The Art of Interpretation

As you saw in the video, running the K-Means algorithm is just the first step. The output is a series of numbers (0, 1, 2, ...), but these have no intrinsic meaning. The real value comes from interpreting these clusters.

To interpret your clusters, you should:

  1. Add the cluster labels back to your original data, as shown in the video.
  2. Analyze the cluster centroids. The kmeans.cluster_centers_ attribute gives you the coordinates of each centroid. These coordinates represent the "average" member of each cluster. Remember to inverse-transform these values if you used a scaler.
  3. Calculate summary statistics for each feature, grouped by cluster. Using pandas, a command like df.groupby('cluster').mean() is incredibly powerful. This shows you the average characteristics of each group.
  4. Visualize the results. Create plots (scatter plots for 2D, box plots, etc.) and color the data points by their assigned cluster to see the separation and characteristics of the groups.

The goal is to create a "persona" for each cluster. For the mall dataset, you might end up with segments like:

  • Cluster 0: "Careful Savers" (Low income, low spending)
  • Cluster 1: "Mainstream Customers" (Average income, average spending)
  • Cluster 2: "High-Value Targets" (High income, low spending - potential for growth!)
  • ...and so on.

This interpretation is what turns a machine learning output into a strategic business tool.



4. (Optional) Building K-Means From Scratch

You have a strong preference for hands-on, practical learning, and sometimes the best way to understand an algorithm is to build it yourself. While using scikit-learn is the standard in practice, seeing the implementation from scratch can solidify your understanding of the mechanics.

If you are curious and have extra time, this video from Dataquest provides a step-by-step guide to coding the K-Means algorithm from the ground up using NumPy and pandas.

K-means Clustering From Scratch In Python [Machine Learning Tutorial]

This optional video is for those who want to look under the hood. It walks through implementing the K-Means algorithm from scratch, covering scaling, centroid initialization, distance calculation, and the iterative update loop.

This is a detailed, 30+ minute video. You don't need to code along, but watching it will give you a deep appreciation for what sklearn.cluster.KMeans does in just a few lines of code. It will connect directly to the theoretical steps we discussed earlier.



Conclusion

Congratulations on completing your first lesson in unsupervised learning! You've learned the theory behind one of the most important clustering algorithms and, more importantly, how to apply it and extract meaningful insights.

Key Takeaways:

  • Unsupervised learning is about finding hidden patterns in data without a pre-defined target.
  • K-Means is an iterative algorithm that partitions data into K clusters by minimizing the distance from each point to its cluster's centroid.
  • Feature scaling is crucial for distance-based algorithms like K-Means to ensure all features are treated equally.
  • Applying K-Means with scikit-learn involves instantiating KMeans, fitting it to your scaled data, and retrieving the cluster assignments from the .labels_ attribute.
  • The most critical step is interpretation: analyzing the characteristics of each cluster to give them meaning and business value.

Preview of the next lesson:
In this lesson, we conveniently chose k=5 for the mall customer data based on a visual guess. But in most real-world problems, the optimal number of clusters isn't obvious. How do we choose the right K? In the next lesson, we will explore systematic methods like the Elbow Method and the Silhouette Score to answer this critical question.