Silhouette Score for Clustering

easy
Unit 5 · Clustering & Decision Trees0/3 to milestone

How good is a clustering when there are no labels? The silhouette compares how close each point is to its own cluster versus the nearest other cluster.

For point i: a = mean distance to the other points in its cluster, b = the smallest mean distance to the points of any other cluster, and si=b−amax⁡(a,b)s_i = \frac{b - a}{\max(a, b)}. A point alone in its cluster gets si=0s_i = 0.

Implement (with Euclidean distances):

  • silhouette_samples(X, labels): the array of sis_i
  • silhouette_score(X, labels): their mean
  • best_k(X, label_sets): given a dict {k: labels}, the k with the highest score (ties: smallest k)
#clustering#metrics#unsupervised2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Silhouette Score for Clustering · TechGradify