How k-means works
- Choose k, the number of clusters you want.
- Place k starting centres. This tool uses k-means++, which spreads the first centres out so results are more stable.
- Assign every point to its nearest centre.
- Update each centre to the average position of the points assigned to it.
- Repeat steps 3 and 4 until no point changes cluster. The algorithm has converged.
What is inertia?
Inertia is the sum of the squared distances from each point to its centre. It drops with every iteration. Lower inertia means tighter clusters, but it always falls as k grows, so compare runs with the same k.
How to choose k
Run the tool with k from 2 to 6 and note the final inertia each time. The "elbow", where adding another cluster stops reducing inertia much, is usually a good choice.
Code
For a complete C++ implementation with example input, see the related tutorial below.