-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainingPartitions.m
50 lines (38 loc) · 1.31 KB
/
trainingPartitions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function varargout = trainingPartitions(numObservations,splits)
%TRAININGPARTITONS Random indices for splitting training data
% [idx1,...,idxN] = trainingPartitions(numObservations,splits) returns
% random vectors of indices to help split a data set with the specified
% number of observations, where SPLITS is a vector of length N of
% partition sizes that sum to one.
%
% Example: Get indices for 50%-50% training-test split of 500
% observations.
% [idxTrain,idxTest] = trainingPartitions(500,[0.5 0.5])
%
% Example: Get indices for 80%-10%-10% training, validation, test split
% of 500 observations.
% [idxTrain,idxValidation,idxTest] = trainingPartitions(500,[0.8 0.1 0.1])
arguments
numObservations (1,1) {mustBePositive}
splits {mustBeVector,mustBeInRange(splits,0,1,"exclusive"),mustSumToOne}
end
numPartitions = numel(splits);
varargout = cell(1,numPartitions);
idx = randperm(numObservations)
%idx = 1:numObservations
%a = length(idx)
idxEnd = 0;
for i = 1:numPartitions-1
idxStart = idxEnd + 1;
idxEnd = idxStart + floor(splits(i)*numObservations) - 1;
varargout{i} = idx(idxStart:idxEnd);
end
% Last partition.
varargout{end} = idx(idxEnd+1:end);
end
function mustSumToOne(v)
% Validate that value sums to one.
if sum(v,"all") ~= 1
error("Value must sum to one.")
end
end