One Tree, Many Cuts

Machine Learning in CodeEasy+10 XP+10

One Tree, Many Cuts

A gardener has measured spots in a field where seedlings came up, as [x, y] in metres. Hierarchical clustering builds one tree of merges, and you choose afterwards how many groups to cut it into.

Task: write cut_sizes(points, levels).

  • For each number in levels, in the given order, fit AgglomerativeClustering(n_clusters=n) on the points as given. Keep the default linkage and don't scale the points.
  • Count how many points are in each cluster.
  • Return one list per level, each sorted from largest to smallest, in the same order as levels.

Examples

points = [[-0.3, -0.69], [24.05, -0.22], [19.9, 0.61], [4.57, -0.5], [19.67, -0.24], [23.44, -0.31], [-1.16, 1.21], [20.47, -0.08], [20.96, -0.12], [4.61, 0.58], [0.51, -0.33], [3.84, -0.21]]
levels = [2, 4]
returns [[6, 6], [4, 3, 3, 2]]
points = [[0.38, 0.18], [3.59, 3.0], [2.47, 2.98], [-0.18, -0.22], [14.45, 14.4], [14.85, 15.23], [0.1, 0.2], [2.26, 2.98], [2.84, 3.72], [15.64, 16.21], [0.23, -0.52], [3.46, 3.29]]
levels = [1, 2, 3]
returns [[12], [9, 3], [5, 4, 3]]

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