You will probably often be disappointed by the solution that any particular run of "the k-means algorithm" (i.e. Lloyd's algorithm) comes up with. That's because Lloyd's algorithm often gets stuck in bad local minima.
Luckily, Lloyd's is just one way of solving k-means. And there's an approach that almost always finds a better local minima.
The trick is to update the cluster assignments of the data points one at a time. You can do this efficiently by keeping a count of the number of points n
assigned to each mean. So that you can re-calculate the cluster mean m
after removing the point x
as follows:
m_new = (n * m - x) / (n - 1)
And add x
to cluster mean m
using:
m_new = (n * m + x) / (n + 1)
Of course because it can't be vectorized, it's a bit painful to run in MATLAB but not too bad in other languages.
If you are really keen on getting the best possible local minima, and you don't mind using exemplar-based clustering, you should look at affinity propagation. MATLAB implementations are available on the Frey lab affinity propagation page.