How Big Is Each Group?

Machine Learning in CodeEasy+10 XP+10

How Big Is Each Group?

A bike courier has marked her drop-off points on a city map. Each point is [x, y], both in kilometres, so the two columns already share the same units.

She wants to split the points into k delivery zones and see how many stops land in each zone.

Task: write cluster_sizes(points, k).

  • Fit KMeans(n_clusters=k, random_state=0, n_init=10) on the points exactly as given. Don't scale them.
  • Count how many points ended up in each cluster.
  • Return the counts as a list, sorted from largest to smallest.

The cluster numbers k-means hands out are arbitrary, which is why you return sorted counts and not the labels themselves.

Examples

points = [[0.96, 0.75], [5.04, 8.27], [9.25, 1.07], [2.11, 2.29], [1.83, 2.82], [8.85, 3.34], [8.34, 2.5], [4.97, 8.24], [2.12, 2.44], [1.34, 2.3], [5.87, 7.51], [9.11, 3.65], [1.6, 2.92]]
k = 3
returns [6, 4, 3]
points = [[0.32, 1.64], [-0.01, -0.4], [11.06, 0.87], [0.33, 0.81], [-0.73, -1.89], [1.89, 0.01], [9.58, 1.11], [-0.24, -0.27], [8.76, 0.02], [1.91, -0.38]]
k = 2
returns [7, 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