~/bend-docscommunity

main.bend relies on unsafe/foreign

raw source on the hub · import mlbend-split@0.1.1.0/main.bend as Main

mlbend-split: a seeded, deterministic split of the rows, stratified by label.

import mlbend-split@0.1.0.0/main.bend as Split

The rows of a class are shuffled and then dealt out over the folds, so every fold holds the same proportion of every class — the rule scikit-learn's StratifiedKFold uses. The shuffle is bend-kit-random's xoshiro128**, seeded: the same seed gives the same split, and another seed gives another one. Nothing here generates numbers of its own.

The labels are class indices, 0 to the number of classes.

What is proved: every row of the dataset is placed in exactly one fold (split_covers) — the fold list holds one entry per row, so the folds are a partition of the rows: the test rows of a fold and its training rows are disjoint, and together they hold every row. What is tested: that the sizes and the classes of the folds agree with scikit-learn's (reference/test_split.py).

2 imports
import Base
import bend-kit-random@0.1.0.1/random.bend as Rand

Laws

law split_covers provedsource · line 33 · raw

@labels:List<&2, Nat> -> @+orders:List<&2, List<&2, Nat>> -> @+k:Nat -> @+i:Nat -> {count_rows(folds_from(labels, orders, k, i)) == List.length(&2, Nat, labels) : Nat}

LAW (split_covers): every row of the dataset is placed in a fold — the fold list holds one entry per row, so the folds are a partition of the rows: the test rows of a fold and its training rows are disjoint, and together they hold every row.

Types

type Split source · line 26 · raw

Data

Definitions

def rows_of_class source · line 41 · raw

@labels:List<&2, Nat> -> @+c:Nat -> @+i:Nat -> @+acc:List<&2, Nat> -> List<&2, Nat>

The rows of one class, with their indices.

def shuffled_list source · line 50 · raw

@pair:Pair(List<&2, Nat>, 0x46b9429cbffa7a8c1111ce19c60a3d6a/random.Rng) -> List<&2, Nat>

The list of a pair: the list, and the generator that comes after it.

def shuffled_class source · line 55 · raw

@labels:List<&2, Nat> -> @+c:Nat -> @+seed:U32 -> List<&2, Nat>

The rows of one class, shuffled by the generator.

def class_orders_go source · line 60 · raw

@+labels:List<&2, Nat> -> @+seed:U32 -> @remaining:Nat -> @+next:Nat -> @+acc:List<&2, List<&2, Nat>> -> List<&2, List<&2, Nat>>

The shuffled rows of every class, one class after the other.

def class_orders source · line 70 · raw

@labels:List<&2, Nat> -> @seed:U32 -> @c:Nat -> List<&2, List<&2, Nat>>

The shuffled rows of every class, one class after the other.

def largest_of source · line 74 · raw

@labels:List<&2, Nat> -> @+acc:Nat -> Nat

The largest label of the labels.

def classes_of source · line 82 · raw

@labels:List<&2, Nat> -> Nat

The number of classes of the labels: one more than the largest label.

def count_rows source · line 86 · raw

@fs:List<&2, Nat> -> Nat

The number of rows a list of folds holds.

def position_go source · line 94 · raw

@order:List<&2, Nat> -> @+i:Nat -> @+at:Nat -> Nat

The place of a row among the shuffled rows of its class.

def position_of source · line 102 · raw

@order:List<&2, Nat> -> @i:Nat -> Nat

The place of a row among the shuffled rows of its class.

def fold_in_of source · line 106 · raw

@order:List<&2, Nat> -> @k:Nat -> @i:Nat -> Nat

The fold of a row, given the shuffled rows of its class.

def order_head source · line 110 · raw

@orders:List<&2, List<&2, Nat>> -> List<&2, Nat>

The first of the shuffled rows of every class.

def order_tail source · line 118 · raw

@+orders:List<&2, List<&2, Nat>> -> List<&2, List<&2, Nat>>

The orders of every class but the first.

def order_at source · line 126 · raw

@n:Nat -> @+orders:List<&2, List<&2, Nat>> -> List<&2, Nat>

The shuffled rows of the class of a label: the n-th of the orders.

def fold_in source · line 135 · raw

@orders:List<&2, List<&2, Nat>> -> @k:Nat -> @label:Nat -> @i:Nat -> Nat

The fold of a row: its place among the shuffled rows of its class, dealt out over the folds in turn.

def folds_from source · line 140 · raw

@labels:List<&2, Nat> -> @+orders:List<&2, List<&2, Nat>> -> @+k:Nat -> @+i:Nat -> List<&2, Nat>

The fold of every row from the row i on, given the shuffled rows of every class.

def folds source · line 149 · raw

@+labels:List<&2, Nat> -> @seed:U32 -> @k:Nat -> List<&2, Nat>

The fold of every row of the dataset, in the order of the rows.

def test_row source · line 153 · raw

@fold:Nat -> @f:Nat -> @+test:List<&2, Nat> -> @i:Nat -> List<&2, Nat>

The row, in the fold.

def train_row source · line 157 · raw

@fold:Nat -> @f:Nat -> @+train:List<&2, Nat> -> @i:Nat -> List<&2, Nat>

The row, out of the fold.

def split_of source · line 161 · raw

@fs:List<&2, Nat> -> @+f:Nat -> @+i:Nat -> @+test:List<&2, Nat> -> @+train:List<&2, Nat> -> Split

The rows of one fold: the ones that test and the ones that train.

def split source · line 169 · raw

@labels:List<&2, Nat> -> @seed:U32 -> @k:Nat -> @f:Nat -> Split

The rows of one fold: the ones that test and the ones that train.

def test_of_go source · line 182 · raw

@+fs:List<&2, Nat> -> @+f:Nat -> @+i:Nat -> @+acc:List<&2, Nat> -> List<&2, Nat>

The rows of the fold f: the ones that test.

def test_of source · line 190 · raw

@fs:List<&2, Nat> -> @f:Nat -> List<&2, Nat>

The rows of the fold f: the ones that test.

def train_of_go source · line 194 · raw

@+fs:List<&2, Nat> -> @+f:Nat -> @+i:Nat -> @+acc:List<&2, Nat> -> List<&2, Nat>

The rows of the fold f: the ones that train.

def train_of source · line 202 · raw

@fs:List<&2, Nat> -> @f:Nat -> List<&2, Nat>

The rows of the fold f: the ones that train.