Silhouette Clustering Score
A cluster assignment can be compact yet poorly separated. Compare each point’s mean distance within its cluster with the nearest other cluster; singletons score zero.
within, nearest_other = 1.0, 9.5
print((nearest_other-within)/max(within, nearest_other))From arithmetic to an experiment
A cluster assignment can be compact yet poorly separated. Compare each point’s mean distance within its cluster with the nearest other cluster; singletons score zero. The trace reads the local variables of your function as it executes. Change one input or parameter, predict the result, then run the checks again. A small deterministic example makes arithmetic mistakes easier to locate than a long training run. State the shape and meaning of the returned value before coding. Keep preprocessing, evaluation and model updates in their proper order, and examine the boundary case in each check. The companion lab lets you replay intermediate values while the same algorithm runs.
Your task
silhouette(points, labels) returns the mean silhouette for 1D points using absolute distance. There are at least two nonempty clusters. A singleton has score 0; if a=b=0 also use 0.
The explanation opens here once every check passes.