# KMeans, level=detailed

Summary: K-means (k = 3, init = array) ran 7 iterations (no label changed). Inertia fell from 3,620 to 229; 95% of the drop came in the first 2 iterations.

[0] init, iteration 0
- Init: the 3 centroids passed as init. Next: assign each point to its nearest centroid.
  (Centroid: the centre of a cluster, at the average position of its points.)

[1] assign, iteration 1
- Assigned all 120 points to their nearest centroid; cluster sizes 42, 78 and 0. Inertia: 3,620.
  (Inertia: the sum of squared distances from every point to its centroid; lower means tighter clusters.)

[2] update, iteration 1
- Cluster 2 was empty; its centroid was moved onto row 114, which was 10.5 from its centroid in cluster 1, farther than any other point.
- Centroid 1 moved 4.86, mostly along x1 (-4.19), to the mean of its 77 points.
- Centroid 0 moved 1.6.
- Inertia 3,620 → 1,586 (-56.2%).

[3] assign, iteration 2
- 63 of 120 points (52%) changed cluster, taking inertia 1,586 → 639 (-59.7%). Largest flow: 36 from cluster 1 to 2; sizes now 69, 14 and 37.

[4] update, iteration 2
- Centroid 1 moved 2.19, mostly along x0 (+1.69), because 63 points left (now 14 points).
- Centroid 2 moved 1.38, mostly along x1 (+1.25), because 36 points joined it (now 37 points).
- Centroid 0 moved 1.16.
- Inertia 639 → 409 (-36%).

[5] assign, iteration 3
- 7 of 120 points (6%) changed cluster, taking inertia 409 → 365 (-10.7%). Largest flow: 4 from cluster 0 to 1; sizes now 65, 15 and 40.

[6] update, iteration 3
- Centroid 1 moved 1.42, mostly along x1 (+1.03), because 4 points joined it and 3 points left (now 15 points).
- Centroid 2 moved 0.179, mostly along x0 (+0.136), because 3 points joined it (now 40 points).
- Centroid 0 moved 0.164.
- Inertia 365 → 332 (-9.08%).

[7] assign, iteration 4
- 17 of 120 points (14%) changed cluster, taking inertia 332 → 291 (-12.2%). Largest flow: 17 from cluster 0 to 1; sizes now 48, 32 and 40.

[8] update, iteration 4
- Centroid 0 moved 0.704, mostly along x1 (+0.703), because 17 points left (now 48 points).
- Centroid 1 moved 0.663, mostly along x1 (+0.658), because 17 points joined it (now 32 points).
- Centroid 2 kept the same points, so it stayed put.
- Inertia 291 → 253 (-13%).

[9] assign, iteration 5
- 6 of 120 points (5%) changed cluster, taking inertia 253 → 236 (-6.77%). Largest flow: 6 from cluster 0 to 1; sizes now 42, 38 and 40.

[10] update, iteration 5
- Centroid 0 moved 0.297, mostly along x1 (+0.296), because 6 points left (now 42 points).
- Centroid 1 moved 0.191, mostly along x1 (+0.189), because 6 points joined it (now 38 points).
- Centroid 2 kept the same points, so it stayed put.
- Inertia 236 → 231 (-2.15%).

[11] assign, iteration 6
- 2 of 120 points (2%) changed cluster, taking inertia 231 → 229 (-0.745%). Largest flow: 2 from cluster 0 to 1; sizes now 40, 40 and 40.

[12] update, iteration 6
- Centroid 0 moved 0.0982, mostly along x1 (+0.0899), because 2 points left (now 40 points).
- Centroid 1 moved 0.0865, mostly along x1 (+0.0789), because 2 points joined it (now 40 points).
- Centroid 2 kept the same points, so it stayed put.
- Inertia 229.3 → 228.6 (-0.299%).

[13] assign, iteration 7
- 0 of 120 points changed cluster; inertia unchanged at 229.

[14] update, iteration 7
- None of the 3 centroids moved, because no point joined or left any cluster.
- Inertia unchanged at 229.

[15] done, iteration 7
- Converged after 7 iterations: no label changed in the last one. Final inertia 229.
  (Iteration: one assignment pass followed by one update pass.)

Diagnostics:
- [step 2] Cluster 2 lost all its points in iteration 1, so its centroid had to be moved.
  Fix: Empty clusters usually mean poor starting centroids or too many clusters: try init='k-means++', more restarts (n_init=10) or a smaller n_clusters.
