# Why doesn't Mathematica find the obvious clusters?

I run into this problem when analyzing data with obvious clusters. Why doesn't Mathematica find the four clusters?

stat = RandomChoice[{{0, 0, 0}, {1, 0, 0}, {1, 1, 1}, {0, 0, 1}},1000];
clu = FindClusters[stat];
mclu = Map[Mean, clu]


{{1, 0, 0}, {16/33, 16/33, 1}, {0, 0, 0}}

clu = FindClusters[stat, 4];
mclu = Map[Mean, clu]


{{1, 0, 0}, {120/247, 120/247, 1}, {0, 0, 0}, {0, 0, 1}}

• What's your version? it works perfectly fine on my computer~
– Wjx
Aug 31, 2016 at 23:26
• I use version 9.0.1.0 Sep 2, 2016 at 8:31

The problem may be the Method of clustering, that is, the clustering algorithm. Although it isn't immediately obvious from the Mathematica documentation, the default method may be incremental or local, where points are assigned to clusters as they are sequentially considered or in small batches.

Thus for instance, if the first two points are {0,0,0} and {0,0,1} they may be considered by the algorithm to be a single cluster, and updated accordingly.

If you use Method -> "Agglomerate" this problem vanishes.

Likewise, if you first Sort the data, this problem vanishes:

stat = RandomChoice[{{0, 0, 0}, {1, 0, 0}, {1, 1, 1}, {0, 0, 1}},
1000];
clu = FindClusters[Sort[stat], 4];
mclu = Map[Mean, clu]


(* {{0, 0, 0}, {0, 0, 1}, {1, 0, 0}, {1, 1, 1}} *)

• Yes... that's what I stated and coded, but didn't paste the proper code. Now my answer is fixed. Thanks. Aug 31, 2016 at 23:53
• Agglomerate works but only if you enter the number of clusters in advance. Sort fails to help at least for me. Make the data slightly more complicated by adding a few stray values and agglomerate fails as well when a stray value gets its own tiny cluster. Sep 2, 2016 at 9:14
• @Mats Lijendahl Really? You run the (Sort) code I posted and don't get the output I posted? Then I have no idea what might cause such a problem. Sep 2, 2016 at 17:30
• Yes really. I double checked. May it be that I use version 9? Sep 2, 2016 at 21:56
• I'm using version 10.0.4, but that "explains" nothing... Sep 3, 2016 at 0:49