2
$\begingroup$

Bug introduced in 8.0.4 or earlier and later fixed in 11.3 or earlier.


I've been trying to use FindClusters function in Mathematica and it wasn't working well when I wanted it to cluster this array:

    In[171]:= FindClusters[{9.9, 9.9, 9.9, 9.9, 9.9, 19.9, 19.9, 19.9}]

    Out[171]= {{9.9}, {9.9, 9.9, 9.9, 9.9}, {19.9, 19.9, 19.9}}

Even when I set the number of groups it still gives me the wrong result:

   In[348]:= FindClusters[{9.9, 9.9, 9.9, 9.9, 9.9, 19.9, 19.9, 19.9}, 2]

   Out[348]= {{9.9, 9.9, 9.9, 9.9, 19.9, 19.9, 19.9}, {9.9}}

Edit:

When I use Method->"Agglomerate" I get the right result:

    In[366]:= FindClusters[{9.9, 9.9, 9.9, 9.9, 9.9, 19.9, 19.9, 19.9}, 
    Method -> "Agglomerate"]

    Out[366]= {{9.9, 9.9, 9.9, 9.9, 9.9}, {19.9, 19.9, 19.9}}

But for another very simple array, I don't:

    v={9.96, 9.97, 9.97, 9.96, 9.97, 19.96, 19.96, 19.96, 19.96, 19.96, \
    29.95, 29.95, 29.95, 29.95, 29.96, 39.94, 39.94, 39.94, 39.94, 39.94, \
    49.93, 49.91, 49.91, 49.92, 49.91, 59.9, 59.9, 59.89, 59.89, 59.89, \
    54.9, 54.9, 54.89, 54.89, 54.9, 44.95, 44.94, 44.93, 44.94, 44.93, \
    34.94, 34.97, 34.95, 34.95, 34.95, 24.96, 24.95, 24.95, 24.95, 24.96, \
    14.97, 14.96, 14.96, 14.96, 14.96}

    In[365]:= FindClusters[v, Method -> "Agglomerate"]

    Out[365]= {{9.96, 9.97, 9.97, 9.96, 9.97}, {19.96, 19.96, 19.96, 
    19.96, 19.96, 24.96, 24.95, 24.95, 24.95, 24.96}, {29.95, 29.95, 
    29.95, 29.95, 29.96, 39.94, 39.94, 39.94, 39.94, 39.94, 34.94, 
    34.97, 34.95, 34.95, 34.95}, {49.93, 49.91, 49.91, 49.92, 49.91, 
    59.9, 59.9, 59.89, 59.89, 59.89, 54.9, 54.9, 54.89, 54.89, 54.9, 
    44.95, 44.94, 44.93, 44.94, 44.93}, {14.97, 14.96, 14.96, 14.96, 
    14.96}}

[edit 2]

@Bill helped me with a distance function:

    f[x_,y_]:=If[Abs[x-y]<.1,0,1]; 
    FindClusters[v,DistanceFunction->f]

But it didn't work for another array:

    {9.95579, 9.96598, 9.96792, 9.96304, 9.97097, 19.9552, 19.9589, \
    19.9582, 19.9616, 19.9616, 29.9521, 29.9509, 29.9482, 29.9519, \
    29.9556, 39.9377, 39.9372, 39.942, 39.9371, 39.9388, 49.9277, \
    49.9068, 49.9056, 49.9159, 49.9108, 59.8957, 59.9004, 59.8921, \
    59.8913, 59.8883, 54.9004, 54.8977, 54.8945, 54.8866, 54.8977, \
    44.9469, 44.9401, 44.931, 44.938, 44.9349, 34.9433, 34.9684, 34.9507, \
    34.9477, 34.9464, 24.9636, 24.947, 24.9497, 24.9519, 24.9577, \
    14.9664, 14.9598, 14.9568, 14.9609, 14.9635}

which gave me the following result:

    {{9.95579, 9.96598, 9.96792, 9.96304, 9.97097}, {19.9552, 19.9589, 
    19.9582, 19.9616, 19.9616}, {29.9521, 29.9509, 29.9482, 29.9519, 
    29.9556}, {39.9377, 39.9372, 39.942, 39.9371, 39.9388}, {49.9277, 
    49.9068, 49.9056, 49.9159, 49.9108}, {59.8957, 59.9004, 59.8921, 
    59.8913, 59.8883}, {54.9004, 54.8977, 54.8945, 54.8866, 54.8977, 
    24.9636, 24.947, 24.9497, 24.9519, 24.9577, 14.9664, 14.9598, 
    14.9568, 14.9609, 14.9635}, {44.9469, 44.9401, 44.931, 44.938, 
    44.9349}, {34.9433, 34.9684, 34.9507, 34.9477, 34.9464}}

Thank you!

$\endgroup$
  • 2
    $\begingroup$ Both of your first two examples work appropriately with Mathematica 12, Windows 10. The last example works properly, too. What version of Mathematica are you using? $\endgroup$ – JimB Jul 25 at 16:17
  • $\begingroup$ Thank you for your comments. @Bill, in the second example, what I was expecting is that it would separate my values into 11 groups with values around: 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 and 60. $\endgroup$ – Fábio Jul 25 at 17:15
  • 1
    $\begingroup$ Looks like it's a version issue. In 10.4.1 (Windows 10) I get the same results as you do. $\endgroup$ – JimB Jul 25 at 17:20
  • 2
    $\begingroup$ same issue in version 9 (windows 10/64b) $\endgroup$ – kglr Jul 25 at 19:36
  • 2
    $\begingroup$ Same issue in version 8.0.4 (Win7 x64), in versions 11.3 and 12.0 on the same system it works as expected (after automatic updating what doesn't happen in version 8.0.4). It's a shame that during so many years this bug wasn't fixed... :( $\endgroup$ – Alexey Popkov Jul 25 at 22:10

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.