programdemos/k-means/k-means.xtl
k-means, step by step: 300 points in five blobs, five centers. Each step gives every point the nearest center, then moves each center to the mean of its points (the Learn library's k_step: a few array expressions over all the points at once). From a poor start, five points of one blob, the centers travel across the plane to the blobs; from Learn's farthest-first start they barely need to.
The points: five blobs of 60, each its own center and spread
ᵘn̲oise : Int -> Float
A little noise, the sum of two draws (so it bunches toward the middle).
ᵘn̲oise ← { m → (f̲loat (r̲oll! m r̲eshape 1001) + (r̲oll! m r̲eshape 1001) − 1002) ÷ 1000.0 }
Used in: X
The map: a grid of g x g points over the square -1 .. 1
grid : Float
grid ← o̲\ (2 c̲at g × g) r̲eshape (r̲avel (o̲ffsets g) 'r̲ight t̲able c) c̲at r̲avel (r̲ev c) 'l̲eft t̲able o̲ffsets g
Used in: demos/k-means/k-means.xtl:43
Two starts, the same steps
C0 : Float
A poor start: the first five points, all in the first blob.
C0 ← k t̲ake X
Used in: demos/k-means/k-means.xtl:37, C10
F0 : Float
Learn's start: the first point, then each time the farthest from those so far.
F0 ← k ᵐˡf̲arthest X
Used in: demos/k-means/k-means.xtl:38
ᵘr̲ound : Float -> Float
The inertia (the sum of squared distances to the nearest center) after 0 to 10 steps, from each start: it never rises.
ᵘr̲ound ← { a → (f̲loat f̲loor 0.5 + 1000.0 × a) ÷ 1000.0 }