One Step of k-means
Run exactly one iteration of k-means by hand: assign every point to its nearest centroid, move the centroids, and report the inertia.
Statement
Story
A delivery company has k pickup lockers placed around town and n customers who each use their nearest locker. Every night the company moves each locker to the centre of its customers. That is one iteration of k-means, the clustering algorithm behind customer segmentation, image palettes and vector quantisation. Implement it from scratch.
Task
You are given n points and k current centroids in d dimensions. Perform one iteration:
- Assign every point to the nearest centroid by Euclidean distance. If two or more centroids are equally near, pick the one with the lowest index. Indexes start at 0.
- Update every centroid to the mean of the points assigned to it. A centroid that received no points stays where it was.
- Inertia = the sum, over all points, of the squared distance to the centroid the point was assigned to (the old centroids used in step 1).
Input
- Line 1:
n k d(1 ≤ n ≤ 2000, 1 ≤ k ≤ 8, 1 ≤ d ≤ 4) - Next
nlines: the coordinates of a point (dnumbers) - Next
klines: the coordinates of a centroid (dnumbers)
Output
- Line 1:
nintegers, the centroid index of every point in input order, separated by spaces - Next
klines: the new centroids (dnumbers each) - Last line: the inertia
Print at least 4 decimals; every number within 0.0001 of the exact value is accepted.
Example
6 2 2
1 1
1.5 2
3 4
5 7
3.5 5
4.5 5
1 1
5 7
Output:
0 0 0 1 1 1
1.8333 2.3333
4.3333 5.6667
24.75
The point (3, 4) is exactly 13 away (squared) from both centroids, so the lower index 0 wins. The inertia is 0 + 1.25 + 13 + 0 + 6.25 + 4.25 = 24.75.
Sample input
6 2 2 1 1 1.5 2 3 4 5 7 3.5 5 4.5 5 1 1 5 7
Hints
Stuck? Hints are optional, and each one lowers the points you earn for solving this challenge.
- Hint 1Costs 20 points
- Hint 2Costs 25 points
Reveal the previous hint first
- Hint 3Costs 35 points
Reveal the previous hint first
Lessons that teach this
Stuck on the ideas rather than the puzzle? These lessons cover what you need.