Four Rules, One Map

Machine Learning in CodeMedium+10 XP+10

Four Rules, One Map

A town planner has plotted houses as [x, y] points in hundreds of metres. There are two neighbourhoods, and sometimes a thin row of houses along a road joins them.

The linkage rule decides what "distance between two groups" means, and the four rules can split the same map very differently.

Task: write sizes_by_linkage(points, n_groups).

  • For each rule in this order: "ward", "complete", "average", "single", fit AgglomerativeClustering(n_clusters=n_groups, linkage=rule) on the points as given. Don't scale them.
  • Count the points in each cluster and sort the counts from largest to smallest.
  • Return a dict mapping each rule's name to its sorted counts.

Examples

points = [[-0.14, 0.53], [-0.1, 0.53], [-0.5, -0.13], [-0.26, 0.73], [-0.33, -0.67], [1.12, 0.41], [2.4, -0.1], [4.0, 0.01], [5.6, 0.01], [7.2, 0.06], [10.24, -0.15], [10.67, -0.23], [9.54, 0.37], [9.15, -0.59], [9.65, -0.67], [10.97, -1.3]]
n_groups = 2
returns {'ward': [9, 7], 'complete': [9, 7], 'average': [9, 7], 'single': [10, 6]}
points = [[0.09, -0.3], [0.6, -0.67], [0.2, -0.22], [0.27, -0.06], [0.16, 0.63], [-0.07, -0.39], [2.4, 0.04], [4.0, -0.08], [5.6, 0.04], [7.2, 0.02], [9.69, -0.07], [11.36, 0.7], [10.08, -0.09], [9.94, 0.36], [10.14, -0.34], [10.46, 0.23], [5.0, 9.0]]
n_groups = 2
returns {'ward': [11, 6], 'complete': [9, 8], 'average': [16, 1], 'single': [16, 1]}

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