Silhouette Score
Compute the silhouette score for a clustering result.
For each point with cluster label :
- = mean distance from to all other points in the same cluster
- = minimum over other clusters of the mean distance from to points in that cluster
If a point is the only member of its cluster, .
Return the mean silhouette score across all points, rounded to 4 decimal places.
Example
X = [[0], [1], [10], [11]] labels = [0, 0, 1, 1]
0.8997
- The points are clustered into two groups: [0], [1] in one cluster (label 0) and [10], [11] in another cluster (label 1).
- For each point, we calculate and :
- For points [0] and [1], is the mean distance to the other point in the cluster, which is and , respectively. is the mean distance to points in the other cluster, which is and , respectively.
- For points [10] and [11], and are calculated similarly, resulting in and .
- We then calculate the silhouette score for each point using the formula: .
- The mean silhouette score across all points is , but since the points [0] and [1] have the same distance to the other cluster and the points [10] and [11] have the same distance to the other cluster, the actual calculation will be the same for all points, and when rounded to 4 decimal places, the result is when considering the actual formula and rounding.
Constraints
- X: 2D list of data points
- labels: list of cluster labels (integers)
- Return float: mean silhouette score rounded to 4 decimal places
- Single-point clusters have silhouette 0