Implement the **Silhouette Score** for evaluating clustering quality.
For each point i:
a(i) = average distance to all other points in the SAME cluster
b(i) = minimum average distance to points in any OTHER cluster
s(i) = (b(i) - a(i)) / max(a(i), b(i))
The overall silhouette score is the mean of s(i) across all points.
**Range:** -1 to +1
Example:
X = [[1,2], [1.5,1.8], [5,8], [8,8], [1,0.6], [9,11]]
labels = [0, 0, 1, 1, 0, 1]
silhouette_score(X, labels) → ~0.66
Constraints:
Test Cases
Test Case 1
Input:
X=[[1,2],[1.5,1.8],[5,8],[8,8],[1,0.6],[9,11]], labels=[0,0,1,1,0,1]Expected:
~0.66Test Case 2
Input:
Perfect clustering (well-separated groups)Expected:
close to 1.0Test Case 3
Input:
Random labels on clustered dataExpected:
close to 0 or negative+ 2 hidden test cases