Introduction

Welcome to our Cluster Performance Unveiled course lesson! Here, we leverage Silhouette Scores, the Davies-Bouldin Index, and Cross-Tabulation Analysis to assess DBSCAN, a top-performing clustering algorithm focused on density. Exciting, right?

Applying DBSCAN and Calculating Silhouette Score

DBSCAN is especially useful when the number of clusters is unknown and density is a key factor in cluster formation. In R, the dbscan package provides a straightforward way to apply the DBSCAN algorithm.

library(dbscan)

# Our dataset
X <- matrix(c(1, 2,
              2, 2,
              2, 3,
              8, 7,
              8, 8,
              25, 80), 
            ncol = 2, byrow = TRUE)

# Apply DBSCAN with eps and minPts parameters
db <- dbscan(X, eps = 3, minPts = 2)

# View cluster assignments
print(db$cluster) # Output: 1 1 1 2 2 0

In this implementation, eps specifies the maximum distance between neighboring points, and minPts sets the minimum number of points required to form a dense region (core point). After fitting the algorithm, we need a quantitative measure of clustering quality. The Silhouette Score is a robust indicator, reflecting how similar each point is to its own cluster compared to other clusters.

Calculating the Silhouette Score for DBSCAN Clusters

To calculate the Silhouette Score in R, we use the cluster package:

library(cluster)

# Compute Silhouette Score (excluding noise points, which are labeled as 0)
sil <- silhouette(db$cluster, dist(X))

# Print average Silhouette Score (excluding noise)
avg_sil <- mean(sil[db$cluster != 0, "sil_width"])
cat("Silhouette Score:", avg_sil, "\n") 
# Output:
# Silhouette Score: 0.8673032 

Why exclude noise points (cluster 0)?
In DBSCAN, points labeled as cluster 0 are considered "noise"—they do not belong to any cluster. Including these noise points in the Silhouette Score calculation would distort the metric, as noise points are, by definition, not assigned to any cluster and may be far from all clusters. This would artificially lower the average Silhouette Score and not accurately reflect the quality of the actual clusters. By excluding noise points, the Silhouette Score more accurately measures the cohesion and separation of the true clusters found by DBSCAN.

A higher Silhouette Score indicates that clusters are dense and well separated.

Applying Davies-Bouldin Index and Cross-Tabulation Analysis with DBSCAN
Cross-Tabulation Analysis for DBSCAN Clustering

To further evaluate the clustering, we can perform a Cross-Tabulation Analysis to compare the clustering results with actual labels (if available):

# Suppose true_labels contains the actual labels for the data points
true_labels <- c(1, 1, 1, 2, 2, 3)  # Example true labels

# Cross-tabulation of clustering results vs. true labels
cross_tab <- table(db$cluster, true_labels)
print(cross_tab)

# Output:
#  true_labels
#   1 2 3
# 0 0 0 1
# 1 3 0 0
# 2 0 2 0

Cross-Tabulation Analysis produces a matrix that compares the clustering assignments to the actual labels, helping assess the clustering performance. Here, the row for cluster 0 shows how many noise points correspond to each true label, which can be useful for understanding what kind of points DBSCAN considers as noise.

Interpreting Results and Concluding Remarks

When interpreting these metrics, remember that noise points (cluster 0) are excluded from the Silhouette Score and Davies-Bouldin Index calculations to ensure these metrics accurately reflect the quality of the clusters themselves. A high Silhouette Score indicates effective clustering, while a lower Davies-Bouldin Index suggests better cluster separation. In Cross-Tabulation, the diagonal elements represent accurate classifications, and the row for cluster 0 shows the distribution of noise points across true labels.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal