FunctionRacerProblemsRace

Move the Centers

HardNumPyPython

Each row of points and of centers is a point. Do one round of clustering:

1. Give every point to its nearest center (straight-line distance; on a tie, the center with the lower row).
2. Return the centers after each one moves to the mean of its points. A center with no points stays where it is. No loops: for and while are not allowed.

Example 1

points [[0,0],[2,0],[10,10]]
centers [[0,0],[9,9]]
returns [[1.0,0.0],[10.0,10.0]]

The first two points are nearest to center 0, which moves to their mean (1, 0). The third point pulls center 1 to (10, 10).

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More NumPy problems