v2026.11.2
All Bundles
Bundle

Experiment support for a parameter sweep: a deterministic stratified hold-out split, class-ratio resampling of the training half against a fixed test set, a wall-clock stopwatch and label helpers. Compile with -lib ml,ml_lab,csv,json, or with the ai alias group. Design: docs/ML_LAB_SUPPORT_DESIGN.md.

Partition

A deterministic, stratified train/test hold-out split.

Allocation is per stratum and exact: a stratum of n rows contributes Float->Round(n * test_fraction) rows to the test half, clamped so that every stratum has at least one row in each half. Within a stratum the rows are shuffled from the seed (Fisher-Yates over a MINSTD generator), so the same inputs and seed select the same rows on every platform. Each half comes back in ascending source-row order.

Named Partition rather than Split because System.ML already has a Split class (a tree's split point), and a program that uses both bundles would find the name ambiguous.

Example

split := Partition->TrainTest(X, y, 0.3, 42, true);
model->Fit(split->GetTrainX(), split->GetTrainY());
preds := model->PredictClass(split->GetTestX());

Operations

Indices # function

The row indexes of a stratified split, without copying any data.

Strata holding a single distinct value give an unstratified split of Float->Round(rows * test_fraction) test rows.

function : Indices(strata:Float[], test_fraction:Float, seed:Int) ~ IndexSplit

Parameters

NameTypeDescription
strataFloatone code per row
test_fractionFloatshare of each stratum that goes to the test half, in (0, 1)
seedIntseeds the shuffle

Return

TypeDescription
IndexSplitthe indexes, each half ascending; raises LabError on bad input or a stratum with fewer than 2 rows

Take # function

Gathers rows of a matrix, in the order given.

function : Take(X:Float[,], rows:Int[]) ~ Float[,]

Parameters

NameTypeDescription
XFloatrows x features
rowsIntrow indexes to take

Return

TypeDescription
Floata new matrix of rows->Size() rows; raises LabError on a bad index

Take # function

Gathers labels, in the order given.

function : Take(y:Bool[], rows:Int[]) ~ Bool[]

Parameters

NameTypeDescription
yBoolone label per row
rowsIntrow indexes to take

Return

TypeDescription
Boola new array of rows->Size() labels; raises LabError on a bad index

Take # function

Gathers values, in the order given.

function : Take(y:Float[], rows:Int[]) ~ Float[]

Parameters

NameTypeDescription
yFloatone value per row
rowsIntrow indexes to take

Return

TypeDescription
Floata new array of rows->Size() values; raises LabError on a bad index

TrainTest # function

Splits features and binary labels, optionally stratified by the label.

function : TrainTest(X:Float[,], y:Bool[], test_fraction:Float, seed:Int, stratify:Bool) ~ DataSplit

Parameters

NameTypeDescription
XFloatrows x features
yBoolone label per row, true for the positive class
test_fractionFloatshare of each stratum that goes to the test half, in (0, 1)
seedIntseeds the shuffle; the same seed reproduces the same split
stratifyBooltrue keeps the positive rate equal in both halves

Return

TypeDescription
DataSplitthe split; raises LabError on bad input or a stratum with fewer than 2 rows

TrainTest # function

Splits features and binary labels, stratified by caller-supplied codes.

The codes can be anything the test half should keep in proportion: the attack family behind a binary label, a device model, a day of capture. One code per row; a stratum is every row sharing a code.

function : TrainTest(X:Float[,], y:Bool[], strata:Float[], test_fraction:Float, seed:Int) ~ DataSplit

Parameters

NameTypeDescription
XFloatrows x features
yBoolone label per row, true for the positive class
strataFloatone code per row
test_fractionFloatshare of each stratum that goes to the test half, in (0, 1)
seedIntseeds the shuffle

Return

TypeDescription
DataSplitthe split; raises LabError on bad input or a stratum with fewer than 2 rows