Understanding Silhouette Score in Clustering

FARSHAD K
4 min readNov 26, 2024

--

Clustering is one of the core tasks in unsupervised machine learning, used to group similar data points based on their features. But how do we evaluate the quality of these clusters? This is where Silhouette Score comes into play. It provides a quantitative way to measure how well data points are grouped and separated into clusters.

In this blog, we’ll dive deep into the Silhouette Score, how it works, and why it’s a powerful tool for evaluating clustering performance.

What is the Silhouette Score?

The Silhouette Score measures how similar a data point is to its own cluster compared to other clusters. It considers two aspects:

  1. Cohesion (a(i)): How close a data point is to other points in its own cluster.
  2. Separation (b(i)): How far a data point is from the points in the nearest neighboring cluster.

The balance between cohesion and separation determines how well a point fits into its cluster.

Formula for Silhouette Score

For a single data point i, the Silhouette Score is calculated as:

Where:

  • a(i): Average distance from i to all other points in its cluster.
  • b(i): Average distance from i to all points in the nearest neighboring cluster.

Range of Silhouette Score

  • +1: Perfect clustering — the point is far from other clusters and well-matched to its own cluster.
  • 0: Borderline clustering — the point is equidistant between two clusters.
  • -1: Poor clustering — the point is closer to another cluster than its own.

How to Interpret Silhouette Scores

The mean Silhouette Score across all data points in a dataset provides an overall measure of cluster quality:

  • A higher mean score indicates well-defined and compact clusters.
  • A lower mean score suggests overlapping or poorly separated clusters.

Why Use Silhouette Score?

Strengths

  1. Unsupervised Evaluation: Works without the need for true cluster labels, making it ideal for unsupervised learning.
  2. Promotes Good Clustering: Rewards clusters that are both compact (cohesion) and well-separated (separation).
  3. Visual Insight: Silhouette plots provide a graphical understanding of cluster quality.

Limitations

  1. Distance Metric Sensitivity: Works best with Euclidean distance; struggles with non-Euclidean metrics.
  2. Cluster Shape Sensitivity: Assumes spherical clusters; less effective for irregular shapes (e.g., in DBSCAN).
  3. Scalability: Computationally expensive for large datasets due to pairwise distance calculations.

Silhouette Score in Action

Let’s evaluate the performance of K-Means Clustering on a synthetic dataset using Python.

from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt
# Generate synthetic data
X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.6, random_state=42)
# Apply K-Means Clustering
kmeans = KMeans(n_clusters=4, random_state=42)
labels = kmeans.fit_predict(X)
# Calculate Silhouette Score
sil_score = silhouette_score(X, labels)
print(f"Silhouette Score: {sil_score:.2f}")
# Visualize Clusters
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=300, c='red', marker='X')
plt.title("K-Means Clustering")
plt.show()

Output

  • Silhouette Score: A numeric value summarizing the clustering quality.
  • Cluster Visualization: Visual confirmation of compactness and separation.

Choosing the Optimal Number of Clusters

Silhouette Score can guide us in selecting the right number of clusters (kkk) for algorithms like K-Means. Evaluate the Silhouette Score for a range of kkk values and pick the one that maximizes the score.

scores = []
for k in range(2, 10):
kmeans = KMeans(n_clusters=k, random_state=42)
labels = kmeans.fit_predict(X)
scores.append(silhouette_score(X, labels))
plt.plot(range(2, 10), scores, marker='o')
plt.title("Silhouette Score vs. Number of Clusters")
plt.xlabel("Number of Clusters")
plt.ylabel("Silhouette Score")
plt.show()

Visualizing with Silhouette Plots

A Silhouette Plot shows how well each data point fits its cluster. Each bar represents a single data point’s score.

from sklearn.metrics import silhouette_samples
import numpy as np
# Calculate Silhouette Samples
silhouette_vals = silhouette_samples(X, labels)
# Plot Silhouette Scores
y_lower = 10
for i in range(4): # Number of clusters
cluster_sil_vals = silhouette_vals[labels == i]
cluster_sil_vals.sort()
y_upper = y_lower + len(cluster_sil_vals)
plt.fill_betweenx(np.arange(y_lower, y_upper), 0, cluster_sil_vals, alpha=0.7)
plt.text(-0.05, y_lower + 0.5 * len(cluster_sil_vals), str(i))
y_lower = y_upper + 10
plt.axvline(x=sil_score, color="red", linestyle="--")
plt.title("Silhouette Plot")
plt.xlabel("Silhouette Coefficient")
plt.ylabel("Cluster")
plt.show()

Insights

  • Clusters with longer bars indicate better separation and cohesion.
  • Outliers or poorly clustered points are evident as shorter or negative bars.

Real-Life Applications

  • Customer Segmentation: Grouping customers for targeted marketing campaigns.
  • Image Segmentation: Clustering pixels for object detection in images.
  • Genomics: Identifying similar gene expressions.

Example: Customer Segmentation

Suppose a business wants to segment its customers based on purchasing behavior. By applying K-Means and evaluating with the Silhouette Score, the business can determine the most meaningful customer groups for personalized marketing strategies.

Conclusion

The Silhouette Score is an essential metric for assessing clustering quality in unsupervised learning. It helps ensure clusters are well-formed and distinct, making it a valuable tool for a wide range of applications, from marketing to image analysis.

By combining the Silhouette Score with visualizations and domain knowledge, you can achieve more insightful and reliable clustering solutions. Try it on your datasets and take your clustering skills to the next level!

Sign up to discover human stories that deepen your understanding of the world.

--

--

FARSHAD K
FARSHAD K

Written by FARSHAD K

Learning data science and machine learning with a strong curiosity in AI technologies and a drive to stay ahead in innovation.

No responses yet

Write a response