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) ~ IndexSplitParameters
| Name | Type | Description |
|---|---|---|
| strata | Float | one code per row |
| test_fraction | Float | share of each stratum that goes to the test half, in (0, 1) |
| seed | Int | seeds the shuffle |
Return
| Type | Description |
|---|---|
| IndexSplit | the 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
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
| rows | Int | row indexes to take |
Return
| Type | Description |
|---|---|
| Float | a 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
| Name | Type | Description |
|---|---|---|
| y | Bool | one label per row |
| rows | Int | row indexes to take |
Return
| Type | Description |
|---|---|
| Bool | a 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
| Name | Type | Description |
|---|---|---|
| y | Float | one value per row |
| rows | Int | row indexes to take |
Return
| Type | Description |
|---|---|
| Float | a 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) ~ DataSplitParameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
| y | Bool | one label per row, true for the positive class |
| test_fraction | Float | share of each stratum that goes to the test half, in (0, 1) |
| seed | Int | seeds the shuffle; the same seed reproduces the same split |
| stratify | Bool | true keeps the positive rate equal in both halves |
Return
| Type | Description |
|---|---|
| DataSplit | the 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) ~ DataSplitParameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
| y | Bool | one label per row, true for the positive class |
| strata | Float | one code per row |
| test_fraction | Float | share of each stratum that goes to the test half, in (0, 1) |
| seed | Int | seeds the shuffle |
Return
| Type | Description |
|---|---|
| DataSplit | the split; raises LabError on bad input or a stratum with fewer than 2 rows |