sourcelibs/Learn/demos/blobs.xtl
1⍝!/usr/bin/env xetal
2⍝│ Learn's four models on one set of points: three blobs of 20 (around
3⍝│ 0 0, 6 6 and 0 6, a fixed ripple instead of chance). k-means finds
4⍝│ them without their labels; kNN and logistic regression learn them
5⍝│ from their labels; PCA finds the direction they spread along most.
6⍝│ Run with "just demo-lib Learn blobs".
7
8"ml:" u̲se< "Learn"
9
10i ← o̲ffsets 60
11blob ← 1 + i d̲iv 20
12cx ← (1 + blob = 2) s̲elect 0.0 6.0
13cy ← (1 + blob = 1) s̲elect 6.0 0.0
14X ← o̲\ 2 60 r̲eshape (cx + 0.8 × s̲in 1.3 × f̲loat i) c̲at cy + 0.8 × c̲os 2.1 × f̲loat i
15⍝│ k-means: the three centers found, and which cluster each point is in.
16C ← (3, 10) ᵐˡk̲means X
17ᵘr̲ound ← { a → (f̲loat f̲loor 0.5 + 100.0 × a) ÷ 100.0 }
18ᵘr̲ound C
19C ᵐˡa̲ssign X
20⍝│ kNN (5 neighbors) and logistic regression on four new points.
21Q ← 4 2 r̲eshape 1.0 1.0 5.0 5.0 0.5 5.0 3.0 3.0
22(5, X, blob) ᵐˡk̲nn Q
23W ← (200, 0.5) ᵐˡl̲ogistic (X, blob)
24W ᵐˡp̲redict Q
25⍝│ PCA: the direction of most spread, and the share of the variance on it.
26(mu, V, ev) ← 2 ᵐˡp̲ca X
27ᵘr̲ound r̲avel 1 t̲ake₂ V
28ᵘr̲ound (1 s̲elect ev) ÷ '+ r̲/ ev