Two Scores for One Grouping

Metrics & EvaluationMedium+20 XP+20

Two Scores for One Grouping

In Choosing K you judged clusterings by eye: a good one keeps every point close to its own centroid and keeps the centroids far apart. Two standard scores turn that judgement into numbers, so you can compare groupings of the same data without drawing a picture.

All distances here are ordinary straight-line (Euclidean) distances. Cluster iii holds nin_ini​ points, and its centroid cic_ici​ is the average of those points. The centre of all the data, cˉ\bar ccˉ, is the average of every point. There are nnn points and kkk clusters.

Davies–Bouldin score (lower is better)

Each cluster's spread sis_isi​ is the average distance from its points to its centroid. For any two different clusters iii and jjj, their ratio is

Rij=si+sj∥ci−cj∥R_{ij} = \frac{s_i + s_j}{\lVert c_i - c_j \rVert}Rij​=∥ci​−cj​∥si​+sj​​

This ratio is large when two clusters are wide and close together, which is bad.

  1. Find every cluster's spread sis_isi​. Use plain distances, not squared ones.
  2. Compute RijR_{ij}Rij​ for every pair of different clusters.
  3. Each cluster keeps its worst ratio, Di=max⁡j≠iRijD_i = \max_{j \ne i} R_{ij}Di​=maxj=i​Rij​. That is the ratio with the cluster it is most easily confused with.
  4. The score is the average of DiD_iDi​ over all kkk clusters.

Calinski–Harabasz score (higher is better)

B=∑ini ∥ci−cˉ∥2,W=∑i  ∑x∈cluster i∥x−ci∥2B = \sum_{i} n_i \, \lVert c_i - \bar c \rVert^2, \qquad W = \sum_{i} \; \sum_{x \in \text{cluster } i} \lVert x - c_i \rVert^2B=i∑​ni​∥ci​−cˉ∥2,W=i∑​x∈cluster i∑​∥x−ci​∥2 CH=B/(k−1)W/(n−k)\text{CH} = \frac{B / (k - 1)}{W / (n - k)}CH=W/(n−k)B/(k−1)​

BBB measures how far the centroids sit from the middle of the data, with each centroid counted once for every point it holds. WWW is the error left inside the clusters, the same quantity the elbow plot tracks.

Task: write cluster_scores(points, labels). It returns the tuple (davies_bouldin, calinski_harabasz), each rounded to 4 decimal places.

  • points is a list of points. Each point is a list of numbers, and all points have the same length (2, 3 or more coordinates).
  • labels[i] is the cluster of points[i]. Labels can be any values, numbers or strings, and only the grouping they describe matters.
  • There are always at least 2 clusters and more points than clusters. No two clusters share a centroid, and not every point sits exactly on its centroid.

A coffee shop's customers, plotted by visits per week and average spend, can be grouped in many ways. Score two groupings with this function, and the better one usually wins on both numbers: a lower Davies–Bouldin score and a higher Calinski–Harabasz score. The two scores measure the idea differently, so now and then they disagree.

Examples

points = [[1, 1], [2, 1], [1, 2], [6, 5], [7, 6], [6, 6]]
labels = [0, 0, 0, 1, 1, 1]
returns (0.1977, 98.5)
points = [[1, 1], [2, 1], [1, 2], [6, 5], [7, 6], [6, 6]]
labels = [0, 0, 1, 1, 1, 1]
returns (0.5772, 4.2206)

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run