-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathml_kFoldCV_Idxs.m
37 lines (33 loc) · 1.12 KB
/
ml_kFoldCV_Idxs.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
function idxss = ml_kFoldCV_Idxs(n, k, shldRandom)
% idxss = ml_kFoldCV_Idxs(n, k, shldRandom)
% Get the indexes of k-fold Cross Validation.
% Suppose your training data has n examples and you want to perform k-fold CV
% You need to divide the training data into k equal partitions (as equal as possible).
% This function provides indexes for the partitions.
% Inputs:
% n: the number of training examples.
% k: the number of folds of k-fold CVs.
% shldRandom: should we shuffle the indexes from 1:n randomly before dividing into partions?
% The default value is 1.
% Outputs:
% idxss: a cell(1,k) strucutre, idxss{i} is a list of indexes for test data of the i^th fold.
% By: Minh Hoai Nguyen (minhhoai@cmu.edu)
% Date: 11 July 07.
if ~exist('shldRandom', 'var') || isempty(shldRandom)
shldRandom = 1;
end;
if shldRandom
suffledIdxs = randperm(n);
else
suffledIdxs = 1:n;
end;
q = floor(n/k);
r = n - k*q;
idxss = cell(1,k);
for i=1:k
if i<=r
idxss{i} = suffledIdxs(1+(i-1)*(q+1):i*(q+1));
else
idxss{i} = suffledIdxs((1+r+(i-1)*q):(r+i*q));
end;
end;