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.

source · imports kt: libs/Kit/src/Kit.xtl; ml: work/xetal-ml/libs/Learn/src/Learn.xtl; mf: libs/Manifold/src/Manifold.xtl; ch: work/xetal-libraries/libs/Check/src/Check.xtl work/xetal-libraries/libs/Check/src/Check.xtlm

ᵘs̲plit : (Int, Int) -> (Int, Int)

function · line 24

ᵘ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) }
Used in: ᵘr̲un

ᵘr̲un : (Float, Int, Int, Int) -> Float

function · line 33

ᵘ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

function · line 43

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

function · line 48

ᵘ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 }
Used in: distortion

k : Int

value · line 51
k ← 5

splitSeed : Int

value · line 52
splitSeed ← 11

Flat clusters

F : Float

value · line 60

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)
Used in: F, flat

fy : Int

value · line 60

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)
Used in: F, flat

flat : Float

value · line 61
flat ← ᵘr̲un (F, fy, k, splitSeed)

F5 : Float

value · line 64
(F5, f5y) ← ᵏᵗf̲lat (2, 4, 30, 5, 1.6)
Used in: F5, flat5

f5y : Int

value · line 64
(F5, f5y) ← ᵏᵗf̲lat (2, 4, 30, 5, 1.6)
Used in: F5, flat5

flat5 : Float

value · line 65
flat5 ← ᵘr̲un (F5, f5y, k, splitSeed)

Directions

D : Float

value · line 76

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

value · line 76

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)
Used in: D, directions, unit

directions : Float

value · line 77
directions ← ᵘr̲un (D, dy, k, splitSeed)

unit : Float

value · line 84

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

value · line 97

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)
Used in: distortion

L : Float

value · line 98
L ← ᵏᵗt̲reeLayout (4, 2)
Used in: tree, distortion

ty : Int

value · line 99
ty ← ᵏᵗt̲reeClasses (4, 2)
Used in: tree

tree : Float

value · line 100
tree ← ᵘr̲un (L, ty, 3, splitSeed)

distortion : Float

value · line 103
distortion ← T ᵐᶠd̲istortion ᵘc̲hords L