Skip to main content

Welcome to a Grasp lesson

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

Lesson 58

Optimal K-Means Clustering: Elbow Method & Silhouette Scores

Recommended resources

Hello! Welcome to your next lesson on unsupervised learning.

In our last session, you learned how to apply the k-means algorithm to partition a dataset. We successfully grouped data points, but we did so by manually choosing the number of clusters, K. This raises a crucial question: in a real-world scenario where the "right" number of groups isn't obvious, how do we choose an optimal value for K?

Today's lesson directly answers that question. We will focus on two of the most popular techniques for this task, allowing you to meet the learning outcome: Select the number of clusters for k-means using the elbow method or silhouette scores.

We will explore each method's underlying logic, learn how to interpret their results, and understand their respective strengths and weaknesses. This will give you a systematic and justifiable way to select K, a key skill for applying clustering in practice.

1. The Elbow Method: Measuring Cluster Compactness

The first and most intuitive method for finding the optimal K is the Elbow Method. The core idea is to run the k-means algorithm for a range of K values and measure how "compact" the resulting clusters are for each K. We want clusters where the data points are packed tightly around their centroids.

The Metric: Within-Cluster Sum of Squares (WCSS)

To quantify compactness, we use a metric called the Within-Cluster Sum of Squares (WCSS). For a given set of clusters, WCSS is the sum of the squared distances of each data point to its assigned centroid. A smaller WCSS value means the clusters are denser.

In scikit-learn, this value is easily accessible as the inertia_ attribute of a fitted KMeans object. Your engineering background might find it helpful to think of WCSS as being analogous to the total variance within all clusters; minimizing WCSS is like minimizing this internal variance.

To understand the concept and calculation of WCSS more deeply, let's turn to a video.

Elbow Method | Silhouette Coefficient Method in K Means Clustering Solved Example by Mahesh Huddar

This video by Mahesh Huddar explains the elbow method conceptually, starting with how WCSS is calculated and how it's used to create the characteristic plot.

Watch from 01:53 to 04:55. Focus on how WCSS is defined and how the plot of WCSS against the number of clusters is generated and interpreted.

Finding the "Elbow"

As the video explained, the process is as follows:

  1. Run k-means for a range of K values (e.g., from 2 to 10).
  2. For each K, calculate the total WCSS.
  3. Plot the number of clusters (K) on the x-axis and the corresponding WCSS on the y-axis.

As you increase K, the WCSS will always decrease. This is because with more clusters, each cluster becomes smaller and tighter. If K equals the number of data points, WCSS would be zero, but this is useless as each point would be its own cluster. We are looking for a point of diminishing returns.

This point is called the "elbow"—it's the spot on the graph where the WCSS starts to decrease much more slowly.

Elbow Method and Silhouette Score Method for K-Means Clustering
This image shows two methods for choosing K. For now, focus on the left plot, "Elbow Method." The WCSS (Inertia) decreases as K increases. The "elbow," or the point where the curve's descent slows, suggests an optimal K. Here, it appears to be around K=4.

Drawbacks of the Elbow Method

While intuitive, the Elbow Method has a significant practical limitation: the elbow is often not clear.

Elbow Method: Definition, Drawbacks, vs. Silhouette Score

This article from 'Built In' highlights the main drawback of the elbow method and introduces why a second method is often necessary.

Read the section titled 'What Are the Drawbacks of the Elbow Method?'. It contains two graphs that perfectly illustrate the difference between an ideal elbow curve and what you often see with real-world data.

This ambiguity makes the method subjective and sometimes unreliable. This is why we need a more robust technique, which brings us to the Silhouette Method.


2. The Silhouette Method: A More Robust Approach

The Silhouette Method is generally preferred because it considers two criteria instead of one:

  • Cohesion: How close are the data points within the same cluster? (Measured by average intra-cluster distance, a).
  • Separation: How far apart are the data points from other clusters? (Measured by average nearest-cluster distance, b).

A good clustering has high cohesion (small a) and high separation (large b).

The Metric: Silhouette Score

The silhouette score for a single data point is calculated with the formula:

The score ranges from -1 to +1:

  • +1: The point is well-clustered, far from other clusters.
  • 0: The point is on or very close to the boundary between two clusters.
  • -1: The point is likely assigned to the wrong cluster.

To find the best K, we typically calculate the average silhouette score for all data points for each value of K. The K that yields the highest average score is considered optimal.

To understand this concept more clearly, the following video explains the calculation and interpretation.

Elbow Method | Silhouette Coefficient Method in K Means Clustering Solved Example by Mahesh Huddar

Let's return to the video by Mahesh Huddar. This section introduces the silhouette coefficient, its formula, and how to interpret the resulting score.

Watch from 04:55 to 09:24. Pay close attention to the definitions of a and b, the intuition behind the formula, and how the final plot of silhouette score vs. K is used to find the optimal number of clusters.

As the video shows, and in contrast to the elbow plot, we look for the peak (maximum value) in the silhouette score plot. Refer back to the right-hand plot in the "Elbow Method and Silhouette Score Method" image above—the highest score is at K=4.

A Deeper Look with Silhouette Plots

While the average score is a good indicator, a much more powerful diagnostic tool is the silhouette plot. This visualization shows the silhouette score of every single data point, grouped by cluster.

Elbow Method and Silhouette Score Explained

This article by Aryan Upadhyay provides an excellent, detailed guide on how to interpret silhouette plots. This is key to making a truly informed decision about K.

First, read the sections 'The Silhouette Score' and 'Silhouette score' to solidify your understanding of the metric. Then, carefully study 'Visualizing Clustering Performance with Silhouette Plots' and 'How to Read a Silhouette Plot'. Focus on the four criteria for analyzing the plots (high average score, well-formed blades, similar thickness, and few negative scores).

To summarize the key points from the article, when comparing silhouette plots for different K values, you are looking for:

  1. Highest Average Score: The red dotted line should be as high as possible.
  2. Uniform Thickness: The "blades" (the colored shapes representing each cluster) should be of similar thickness, indicating that clusters are of a balanced size.
  3. Few Low Scores: Most points in a cluster should have scores above the average. Wide blades are good.
  4. No Negative Scores: The presence of scores below zero indicates that points may have been misclassified.

Using these criteria provides a much richer and more reliable way to select K than just looking at the average score or a potentially ambiguous elbow.

Test your understanding!

Below are two silhouette plots for the same dataset, one for K=3 and one for K=4.

Plot A (K=3):

  • Average score: 0.55
  • All three clusters have blades of similar thickness and extend well past the average score line. There are no negative scores.

Plot B (K=4):

  • Average score: 0.49
  • One of the four clusters (let's call it Cluster 3) is very thin compared to the others.
  • A significant portion of points in Cluster 3 have silhouette scores below the average, and a few are even negative.

Which value of K would you choose, and why?

Show answer

The better choice is K=3.

Here's the justification:

  1. Higher Average Score: K=3 has a higher average silhouette score (0.55 vs. 0.49).
  2. Better Cluster Quality: The plot for K=4 shows signs of poor clustering. The thin blade for Cluster 3 suggests it's a very small cluster, and the negative scores indicate some of its points are actually closer to another cluster's centroid. This suggests that K=4 may be "over-clustering" the data by splitting a natural group into smaller, less meaningful ones. The plot for K=3 shows more balanced, well-defined clusters.

Conclusion

You now have a powerful and practical toolkit for one of the most common questions in clustering. While data exploration and domain knowledge always play a role, these quantitative methods provide a strong, data-driven foundation for your modeling decisions.

Key Takeaways:

  • Choosing the right number of clusters, K, is essential for meaningful results with the k-means algorithm.
  • The Elbow Method uses WCSS (inertia) to find the "point of diminishing returns" in cluster compactness, but the result can be subjective and ambiguous.
  • The Silhouette Method is more robust because it evaluates both cluster cohesion (a) and separation (b).
  • You can select K by finding the peak in an average silhouette score plot or, for a more detailed analysis, by inspecting silhouette plots to find the K that produces clusters of uniform size and high scores.
  • It is often wise to use both methods as a cross-check to build confidence in your choice of K.

Preview of the Next Lesson:

In this and the previous lesson, we've focused on a clustering method that requires us to specify the number of clusters beforehand. But what if we don't want to make that choice? In our next lesson, we will explore a different approach: Hierarchical Clustering, where we will build a tree of clusters and interpret the resulting structure using a dendrogram.