# Comparison of k-means clustering output

I am trying to compare the outputs of k-means algorithm coded by me and the outputs of R's kmeans. Since the objective of the algorithm is to minimize the total within cluster sum of squares (WCSS), I have to look at the withinss output of kmeans and my WCSS. What I'm not able to figure out is, how much of variation should I allow i.e. how close should these values be so I can say that they are close.
(I know that I can also verify using the centroids, but when the data is not that well clustered, the centroids can be very different but the total WCSS can be close, and hence I'm looking at WCSS.)

• how much of variation should I allow To be sure that your code is all right there should be no difference in results between your implementation and a reference implementation at all. Until you reach that regularly you can't tell if the difference is due to a code bug or due to some minor algorithmic divergence. – ttnphns Mar 4 '13 at 16:44
• I agree that there should be no difference. I'm using the Iris dataset and we know that there are 2 clear clusters in the data. Hence when I give k=2, the output perfect matches with R's. In fact, the output is perfect for k=3 and k=4 too (I use 'nstart' to get the best output). But for k=5 and above, the values are varying. The total WCSS for R's kmeans for k=5 was 46.4 while the same for my algorithm was 49.4. So, should I allow this as a close output since I know that the actual number of clusters are 2 and that further clustering can happen in multiple ways? – Prateek Kulkarni Mar 5 '13 at 5:57
• There must exist an option in k-means to set initial cluster centres. Compare the 2 programs in that regime (give identical initial centres for both programs). Test many times: different data sets, vary k, vary the initial centres. The 2 programs should give identical results consistenly. Only then you can be sure there is no bug in both programs. – ttnphns Mar 5 '13 at 6:28
• Thanks. I tried what you just said for k=5 and the outputs unfortunately, are not the same. But they are very close. Only one point has been clustered differently. I think this is because that point might be equidistant from 2 centroids, and in the 2 algorithms, this point is put into different clusters (since equidistant points would be clustered randomly). And they were consitent for other k values too. The difference in WCSS for k=5 was 0.009. I feel this close enough, but I wish I had a quantitative threshold above which I would approve it as good enough clustering. – Prateek Kulkarni Mar 5 '13 at 10:00
• Oops, I meant '..below which I would approve it as good enough clustering.' – Prateek Kulkarni Mar 5 '13 at 10:24