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.

Resample

Changes the class balance of a TRAINING set to a requested positive ratio.

The class-imbalance experiment trains at ratios from 0.001 to 0.5 and must measure every run on the same test set. These functions take only the training half, so the order is fixed by construction: Partition->TrainTest first, then resample what it returned as training data; the test half is never touched.

Example

split := Partition->TrainTest(X, y, 0.3, 42, true);
each(r : ratios) {
  train := Resample->ToPositiveRatio(split->GetTrainX(), split->GetTrainY(), ratios[r], 42);
  model->Fit(train->GetX(), train->GetY());
  preds := model->PredictClass(split->GetTestX());   # the same test set every time
};

Operations

CountPositives # function

Number of positive labels.

function : CountPositives(y:Bool[]) ~ Int

Parameters

NameTypeDescription
yBoollabels

Return

TypeDescription
Intcount of true values, 0 for Nil

PositiveRatio # function

Share of positive labels.

function : PositiveRatio(y:Bool[]) ~ Float

Parameters

NameTypeDescription
yBoollabels

Return

TypeDescription
Floatpositives over rows, 0.0 for an empty or Nil array

ToPositiveRatio # function

Down-samples the over-represented class until positives / rows is the requested ratio; the other class is kept whole.

If the current ratio is above the request, positives are cut to Round(ratio * negatives / (1 - ratio)); otherwise negatives are cut to Round(positives * (1 - ratio) / ratio). The rows kept are chosen by a seeded shuffle and returned in ascending source order. The achieved ratio is exact when the arithmetic allows and otherwise within one row; Sample->GetPositiveRatio reports it.

function : ToPositiveRatio(X:Float[,], y:Bool[], ratio:Float, seed:Int) ~ Sample

Parameters

NameTypeDescription
XFloattraining rows x features
yBoolone training label per row
ratioFloatrequested positives / rows, in (0, 1)
seedIntseeds the choice of rows

Return

TypeDescription
Samplethe resampled training set; raises LabError when a class is empty, the ratio is out of range, or the request would keep zero rows of a class

ToPositiveRatio # function

Reaches the requested ratio by down-sampling (oversample false, as above) or by duplicating the under-represented class (oversample true): rows of that class are drawn with replacement from a seeded generator and appended after the original rows, and nothing is dropped.

function : ToPositiveRatio(X:Float[,], y:Bool[], ratio:Float, seed:Int, oversample:Bool) ~ Sample

Parameters

NameTypeDescription
XFloattraining rows x features
yBoolone training label per row
ratioFloatrequested positives / rows, in (0, 1)
seedIntseeds the choice of rows
oversampleBooltrue duplicates the minority instead of dropping the majority

Return

TypeDescription
Samplethe resampled training set; raises LabError on bad input