Preprocessing splitters kfold - CyrilB1531/lodestar GitHub Wiki

Development build. This page describes main, not a released package. The latest published Lodestar.Preprocessing is 0.1.0 โ€” read its documentation.

Home โ€บ Preprocessing โ€บ Splitting

Splitters.KFold

Cuts the rows into contiguous folds, as KFold(n_splits=foldCount) does without shuffling.

public static IReadOnlyList<FoldSplit> KFold(int sampleCount, int foldCount)
public static IReadOnlyList<FoldSplit> KFold(int sampleCount, int foldCount, ReadOnlySpan<int> order)

Parameters โ€” sampleCount is how many rows there are. foldCount is how many folds to cut, at least two and at most sampleCount. order is a permutation of 0..sampleCountโˆ’1 the rows are read in; an empty span reads them in order, which is what the two-argument overload passes.

Returns โ€” one FoldSplit per fold, in order, each index list ascending.

Exceptions โ€” ArgumentOutOfRangeException when sampleCount is below two, or foldCount is below two or above sampleCount. ArgumentException when order is neither empty nor a permutation of the rows โ€” a repeated index, an index out of range, or a length that is not sampleCount.

Example โ€” ten rows into three folds, and the same ten read in a permuted order.

using Lodestar.Preprocessing;

IReadOnlyList<FoldSplit> folds = Splitters.KFold(sampleCount: 10, foldCount: 3);

// 10 = 3 + 3 + 3 + 1, and the leftover row goes to the first fold, not the last.
string first = string.Join(",", folds[0].TestIndices);   // => 0,1,2,3
string second = string.Join(",", folds[1].TestIndices);  // => 4,5,6
string third = string.Join(",", folds[2].TestIndices);   // => 7,8,9

// Reading the rows in another order cuts the same block sizes out of that order.
int[] order = [9, 4, 1, 7, 0, 3, 6, 8, 2, 5];
string shuffled = string.Join(",", Splitters.KFold(10, 3, order)[0].TestIndices);  // => 1,4,7,9

Remarks โ€” the first sampleCount % foldCount folds take one extra row. That is the reference's placement and it is worth stating, because the other plausible rule โ€” giving the remainder to the last folds โ€” agrees with it only when the division is exact.

Every row is held out by exactly one fold and fitted on by the other foldCount โˆ’ 1, permuted or not; that is asserted rather than assumed.

Applies to โ€” net10.0, netstandard2.0.

See also โ€” Splitters.StratifiedKFold, FoldSplit.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ