programdemos/baseline-knn/baseline-knn.xtl
Baseline, nearest neighbors: k nearest neighbors under the straight-line distance on the three data sets of the kit (flat clusters, directions, a tree), each split the same seeded way, the held-out accuracy and the cost of each run recorded as the experiment schema says; and for the tree, how badly a plain layout of it keeps its distances. The row every comparison of H1 is set against: no geometry chosen yet.
ᵘs̲plit : (Int, Int) -> (Int, Int)
ᵘs̲plit (seed, n): the held-out quarter of n points, seeded: a
seeded order of 1 to n (the grade of seeded uniform numbers), the
first quarter held out and the rest trained on; (train, held), two
vectors of row numbers.
ᵘs̲plit ← { (seed, n) → order ← g̲rade ᵏᵗu̲niform (seed, n)◆ h ← n d̲iv 4◆ (h d̲rop order, h t̲ake order) }
ᵘr̲un : (Float, Int, Int, Int) -> Float
ᵘr̲un (X, y, k, seed): one run of k nearest neighbors on the set
(X, y) under the straight-line distance (Learn's k_nn), the
held-out quarter guessed from the rest; the record, as the schema
says: the held-out accuracy (measured), how many were held out,
the numbers trained (none: nearest neighbors learns no number) and
the operations counted (one distance per held-out point per
training point, each of d coordinates). Runtime is never recorded.
ᵘr̲un ← { (X, y, k, seed) → (train, held) ← ᵘs̲plit (seed, f̲irst s̲hape X) guess ← (k, train s̲elect X, train s̲elect y) ᵐˡk̲nn held s̲elect X right ← '+ r̲/ guess = held s̲elect y accuracy ← (f̲loat right) ÷ f̲loat t̲ally held accuracy c̲at (f̲loat t̲ally held) c̲at 0.0 c̲at f̲loat (t̲ally train) × (t̲ally held) × 2 s̲elect s̲hape X }
ᵘc̲ompare : (Num a, Num b) => a -> b -> Float
a ᵘc̲ompare b: two records side by side and their difference,
measure by measure (the second less the first), as three rows.
ᵘc̲ompare ← { a b → (3 c̲at t̲ally a) r̲eshape (f̲loat a) c̲at (f̲loat b) c̲at (f̲loat b) − f̲loat a }
ᵘc̲hords : Float -> Float
ᵘc̲hords Q: the straight-line distance between every pair of
rows (a general function, for X_eTaL-libraries: GL1; written out
here as in sphere-hello).
ᵘc̲hords ← { Q → n ← f̲irst s̲hape Q◆ (D, _) ← (2 s̲elect s̲hape Q) '{ (d, k) → c ← k s̲elect₂ Q◆ (d + (c '− t̲able c) ^ 2.0, k + 1) } p̲ower ((n c̲at n) r̲eshape 0.0, 1)◆ s̲qrt D }
Flat clusters
F : Float
Three clusters of 40 points in the plane, spread 1.6 about centers a few units apart (seed 1), so the clusters touch; and four clusters of 30 in five dimensions, spread 1.6 (seed 2). The record: accuracy, held out, numbers trained, operations.
(F, fy) ← ᵏᵗf̲lat (1, 3, 40, 2, 1.6)
fy : Int
Three clusters of 40 points in the plane, spread 1.6 about centers a few units apart (seed 1), so the clusters touch; and four clusters of 30 in five dimensions, spread 1.6 (seed 2). The record: accuracy, held out, numbers trained, operations.
(F, fy) ← ᵏᵗf̲lat (1, 3, 40, 2, 1.6)
Directions
D : Float
Four classes of 40 points that differ by direction (cone 0.6 about each class direction, so the classes overlap at their edges) and not by length (lengths from 0.5 to 2; seed 5). The straight-line distance mixes the lengths in, which carry nothing about the class: this is the row the sphere is compared against (representation-explorer).
(D, dy) ← ᵏᵗd̲irections (5, 4, 40, 0.6)
dy : Int
Four classes of 40 points that differ by direction (cone 0.6 about each class direction, so the classes overlap at their edges) and not by length (lengths from 0.5 to 2; seed 5). The straight-line distance mixes the lengths in, which carry nothing about the class: this is the row the sphere is compared against (representation-explorer).
(D, dy) ← ᵏᵗd̲irections (5, 4, 40, 0.6)
unit : Float
The same set with every point put onto the unit sphere (lengths dropped), still under the straight-line distance between the unit points: what the lengths were costing, measured; the comparison rows are (lengths kept, lengths dropped, the difference).
unit ← ᵘr̲un (ᵐᶠs̲phereProject D, dy, k, splitSeed)
A tree
T : Float
The tree of depth 4 and branching 2 (31 nodes), each node's class its branch, and the plain layout of it in the plane: nearest neighbors on the layout's coordinates (k = 3, the held-out quarter is 7 nodes), and the distortion of the layout's straight-line distances against the distances along the tree (Manifold's: the mean and the worst pair), which is what the hyperbolic disk is compared against (representation-explorer).
T ← ᵏᵗt̲reeDistances (4, 2)
distortion : Float
distortion ← T ᵐᶠd̲istortion ᵘc̲hords L