# How to ask FindClusters to return the loss function value in the output?

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];
ListPlot[clts1]


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];
ListPlot[clts2]


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!

• some info in cc = ClusterClassify[pts, 3, Method -> {"KMeans", DistanceFunction -> "EuclideanDistance"}, RandomSeeding -> 111 ]; Options[cc]  may be useful. – kglr Aug 12 '18 at 5:57
• Since you have the clusters you can compute the cost function values yourself. – Anton Antonov Aug 13 '18 at 7:19
• @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. – H42 Aug 13 '18 at 16:52