I have some data points pts as below:-

pts = BlockRandom[SeedRandom[123]; RandomReal[1, {100, 2}]];

Now I want to separate them into 3 clusters by using k-mean. And I used FindClusters as below:-

clts1 = FindClusters[pts, 3, Method -> {"KMeans", DistanceFunction -> "EuclideanDistance"}, RandomSeeding -> 111];

enter image description here

Since the initialization of k-mean is random, I would like to perform the process several times. Say I just perform twice, and below is the 2nd time by using another random seed:-

clts2 = FindClusters[pts, 3, Method -> {"KMeans", DistanceFunction -> "EuclideanDistance"}, RandomSeeding -> 222];

enter image description here

As you can see, the two clustering result is different. As we know, k-mean is, in fact, optimizing the cost function $J = \frac{1}{m} \sum_{i=1}^{m}{\| x^{(i)}-{\mu}^{(i)} \|}^{2}$, which is the average square of distance between the data points $x^{(i)}$ and their respective cluster centroids ${\mu}^{(i)}$. I want to know whether clts1 or clts2 is having a lower cost function value $J$.

FindClusters may be using some other formula of $J$ instead of the above that I stated, which doesn't matter to me. I just want a cost function value for comparison purpose.

As such, how can I get the loss function value $J$ when I perform FindClusters?

Many thanks!

  • 1
    $\begingroup$ some info in cc = ClusterClassify[pts, 3, Method -> {"KMeans", DistanceFunction -> "EuclideanDistance"}, RandomSeeding -> 111 ]; Options[cc] may be useful. $\endgroup$ – kglr Aug 12 '18 at 5:57
  • $\begingroup$ Since you have the clusters you can compute the cost function values yourself. $\endgroup$ – Anton Antonov Aug 13 '18 at 7:19
  • $\begingroup$ @Anton Antonov . Thanks. If there is no other option, I will do it in this way. But it will duplicate the effort so when the scale of the problem (no matter number of data or dimensions) is large, the difference can be huge. Besides, for some problem (say clustering in image processing), that's more difficult to calculate the cost function ourselves and thus it would be good if we can just extract the value from FindClusters or ClusterClassify. $\endgroup$ – H42 Aug 13 '18 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.