I want to write a Kmeans loop using design script. I have successfully got the code working WITH OUT looping. The script used cross lacing. I found out that this is not supported in imperative Code Block. I’m now struggling to implementing the “for i” method to get the code working as a loop.
//Find the closest K value
distance = points<1>.DistanceTo(Ks<2>);
min = List.MinimumItem(distance<1L>);
Kindices = Flatten(List.AllIndicesOf((min<1L> == distance<1L>)<1L>, true));
//Group Points by closest K value
grps = List.GroupByKey(points, Kindices);
grpPnts = __TryGetValueFromNestedDictionaries(grps, “groups”);
Kin = __TryGetValueFromNestedDictionaries(grpPnts, “unique keys”);
//Find New Centroid Location
Xave = Math.Average(grpPnts.X);
Yave = Math.Average(grpPnts.Y);
Knew = Point.ByCoordinates(Xave, Yave, 0);
//Test
Switch = AllTrue(Knew.DoesIntersect(Ks));
//Out Puts Groups and their Number
grpPnts;
Knew;