main.bend checks
raw source on the hub · import mlbend-cluster@0.1.0.0/main.bend as Main
mlbend-cluster: k-means, functional.
import mlbend-cluster@0.1.0.0/main.bend as Cluster
The clustering is Lloyd's algorithm with a fixed number of rounds: the centres are the first k rows, then every round assigns every row to its nearest centre and moves each centre to the mean of the rows assigned to it. A fixed number of rounds means the result is a function of the arguments: there is no loop that waits for the centres to settle, so a run is reproducible and the same rows give the same centres.
A row is a list of F32 and a centre is a row: the distance is the sum of the squared differences, so the arithmetic is on whole rows and no column is found by its place.
What is proved: every row gets exactly one cluster (cluster_covers)
— the assignment holds one label per row, so a row cannot be in two
clusters and cannot be left out. What is tested: the assignment, the
cluster sizes and the inertia against NumPy (reference/test_cluster.py),
and the k-means example that reads the wine dataset through it
(reference/test_kmeans.py).
1 import
import Base
Laws
law cluster_covers provedsource · line 33 · raw
@+centres:List<&2, List<&2, F32>> -> @rows:List<&2, List<&2, F32>> -> {count_labels(assign(centres, rows)) == count_rows(rows) : Nat}LAW (cluster_covers): every row gets exactly one cluster — the assignment holds one label per row, so a row cannot be in two clusters and cannot be left out.
Types
type Model source · line 27 · raw
Data
Model@centres:List<&2, List<&2, F32>> -> @k:Nat -> Model
Definitions
def count_labels source · line 39 · raw
@labels:List<&2, Nat> -> Nat
How many labels an assignment holds.
def count_rows source · line 47 · raw
@rows:List<&2, List<&2, F32>> -> Nat
How many rows there are.
def sum_squares source · line 55 · raw
@a:List<&2, F32> -> @b:List<&2, F32> -> @+acc:F32 -> F32
The squares of the differences of two rows, added up.
def distance source · line 67 · raw
@a:List<&2, F32> -> @b:List<&2, F32> -> F32
The squared distance of two rows.
def nearest_go source · line 71 · raw
@centres:List<&2, List<&2, F32>> -> @+row:List<&2, F32> -> @+n:Nat -> @+best:Nat -> @+best_d:F32 -> Nat
The nearest centre, walking the centres in order.
def nearest source · line 83 · raw
@centres:List<&2, List<&2, F32>> -> @row:List<&2, F32> -> Nat
The index of the nearest centre: the first one when two are equally far.
def assign source · line 87 · raw
@+centres:List<&2, List<&2, F32>> -> @rows:List<&2, List<&2, F32>> -> List<&2, Nat>
The cluster of every row: the index of its nearest centre.
def zip_add source · line 105 · raw
@a:List<&2, F32> -> @b:List<&2, F32> -> List<&2, F32>
Two rows, added cell by cell.
def column_sums_go source · line 117 · raw
@rows:List<&2, List<&2, F32>> -> @+acc:List<&2, F32> -> List<&2, F32>
The sum of every column, over the rows.
def column_sums source · line 125 · raw
@rows:List<&2, List<&2, F32>> -> List<&2, F32>
The sum of every column, over the rows.
def zip_scale source · line 133 · raw
@a:List<&2, F32> -> @+k:F32 -> List<&2, F32>
One row and a number, cell by cell.
def means_of source · line 141 · raw
@+rows:List<&2, List<&2, F32>> -> List<&2, F32>
The mean of every column of the rows.
def members_go source · line 150 · raw
@rows:List<&2, List<&2, F32>> -> @labels:List<&2, Nat> -> @+k:Nat -> @+acc:List<&2, List<&2, F32>> -> List<&2, List<&2, F32>>
The rows of one cluster, gathered.
def members_of source · line 164 · raw
@rows:List<&2, List<&2, F32>> -> @labels:List<&2, Nat> -> @k:Nat -> List<&2, List<&2, F32>>
The rows of one cluster.
def means_of_clusters_go source · line 170 · raw
@indices:List<&2, Nat> -> @+rows:List<&2, List<&2, F32>> -> @+labels:List<&2, Nat> -> List<&2, List<&2, F32>>
The mean of every cluster, in the order of the cluster indices. A cluster with no rows keeps an empty centre, so the indices never move.
def means_of_clusters source · line 180 · raw
@indices:List<&2, Nat> -> @+rows:List<&2, List<&2, F32>> -> @+labels:List<&2, Nat> -> List<&2, List<&2, F32>>
The mean of every cluster, in the order of the cluster indices.
def starts source · line 185 · raw
@k:Nat -> @+rows:List<&2, List<&2, F32>> -> List<&2, List<&2, F32>>
The first k rows: where the centres start.
def round source · line 189 · raw
@+indices:List<&2, Nat> -> @+centres:List<&2, List<&2, F32>> -> @+rows:List<&2, List<&2, F32>> -> List<&2, List<&2, F32>>
One round: assign, then move every centre to the mean of its rows.
def rounds_go source · line 194 · raw
@fuel:List<&2, Nat> -> @+indices:List<&2, Nat> -> @+centres:List<&2, List<&2, F32>> -> @+rows:List<&2, List<&2, F32>> -> List<&2, List<&2, F32>>
The rounds, the fuel first.
def rounds source · line 204 · raw
@n:Nat -> @indices:List<&2, Nat> -> @centres:List<&2, List<&2, F32>> -> @rows:List<&2, List<&2, F32>> -> List<&2, List<&2, F32>>
The centres after the rounds.
def fit source · line 209 · raw
@+k:Nat -> @rounds_count:Nat -> @+rows:List<&2, List<&2, F32>> -> Model
The clustering: the k centres, after the rounds, over the rows.
def centre_of source · line 213 · raw
@m:Maybe<&2, List<&2, F32>> -> List<&2, F32>
A centre, or an empty row when there is none.
def centre_at source · line 221 · raw
@centres:List<&2, List<&2, F32>> -> @n:Nat -> List<&2, F32>
The centre at an index, or an empty row when there is none.
def inertia_go source · line 225 · raw
@+centres:List<&2, List<&2, F32>> -> @+rows:List<&2, List<&2, F32>> -> @labels:List<&2, Nat> -> @+acc:F32 -> F32
The squared distances from the rows to their centres, added up.
def inertia source · line 238 · raw
@centres:List<&2, List<&2, F32>> -> @rows:List<&2, List<&2, F32>> -> @labels:List<&2, Nat> -> F32
The sum of the squared distances from every row to its centre.
def count_go source · line 243 · raw
@labels:List<&2, Nat> -> @+k:Nat -> @+acc:Nat -> Nat
The number of rows in one cluster, walking the labels.
def count_of source · line 251 · raw
@labels:List<&2, Nat> -> @k:Nat -> Nat
The number of rows in one cluster.
def sizes_go source · line 255 · raw
@indices:List<&2, Nat> -> @+labels:List<&2, Nat> -> List<&2, Nat>
The number of rows of every cluster, in the order of the cluster indices.
def sizes source · line 263 · raw
@labels:List<&2, Nat> -> @k:Nat -> List<&2, Nat>
The number of rows of every cluster, in the order of the cluster indices.