pub fn recognize_kmeans(
stmts: &[Stmt<'_>],
interner: &Interner,
) -> Result<KmeansSpec, KmeansReject>Expand description
Recognize a k-means clustering anywhere in a LOGOS statement list — the epoch loop of Lloyd’s algorithm:
While epoch is less than iters:
(zero `sums` (k · f) and `counts` (k), each with a loop setting every item to 0)
Let mutable p be 0.
While p is less than n:
Let mutable best be 0.
Let mutable bestDist be 0.
Let mutable c be 0.
While c is less than k:
Let mutable dist be 0.
Let mutable d be 0.
While d is less than f:
Set dist to dist + (item (p * f + d + 1) of pts - item (c * f + d + 1) of cts) ** 2.
Set d to d + 1.
If c is 0 or dist is at most bestDist:
Set best to c.
Set bestDist to dist.
Set c to c + 1.
(a loop over the features adding the point's row to row `best` of `sums`)
Set item (best + 1) of counts to item (best + 1) of counts + 1.
Set p to p + 1.
(a loop over the centroids, and in it over the features: `If cnt is 0:` the centroid's feature set to 0,
`Otherwise:` to its sum over `cnt`, `cnt` the centroid's count)
Set epoch to epoch + 1.where cts starts as the first k · f items of pts (a loop pushing item (q + 1) of pts onto it). Every loop
is counted and every body holds nothing else but immutable names and the counters’ resets. dist is less than bestDist keeps the first of equally near centroids, at most the last.