-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrossValidation.m
69 lines (62 loc) · 1.78 KB
/
crossValidation.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
% n-fold cross validation
function [trainFeatures,trainLabels,testFeatures,testLabels,vaildFeatures,vaildLabels] = crossValidation(features,labels,n,isVaild,isRng)
if (~exist('isRng','var'))
isRng = true;
end
foldData = cell(1,n);
trainData = cell(1,n);
testData = cell(1,n);
vaildData = cell(1,n);
testFeatures = cell(1,n);
testLabels = cell(1,n);
trainFeatures = cell(1,n);
trainLabels = cell(1,n);
vaildFeatures = cell(1,n);
vaildLabels = cell(1,n);
const = 1.0/n;
numOfData = length(features(:,1));
% randperm
if isRng == true
rng(0,'twister');
end
randNum =(randperm(length(features(:,1))))';
data = sortrows([randNum,features,labels]);
for i=1:n
foldData{i}=data(int16((i-1)*const*numOfData)+1:int16(i*const*numOfData),:);
end
for i=1:n
testData{i} = foldData{i};
if isVaild
if i~=n
vaildData{i} = foldData{i+1};
else
vaildData{i} = foldData{1};
end
end
trainData{i} = [];
for j = 1:n
if isVaild
if i~=n && j~=i && j~=i+1
trainData{i} = [trainData{i};foldData{j}];
end
if i==n && j~=i && j~=1
trainData{i} = [trainData{i};foldData{j}];
end
else
if j~=i
trainData{i} = [trainData{i};foldData{j}];
end
end
end
end
for i=1:n
testFeatures{i} = testData{i}(:,2:(length(features(1,:))+1));
trainFeatures{i} = trainData{i}(:,2:(length(features(1,:))+1));
testLabels{i} = testData{i}(:,(length(features(1,:))+2):end);
trainLabels{i} = trainData{i}(:,(length(features(1,:))+2):end);
if isVaild
vaildFeatures{i} = vaildData{i}(:,2:(length(features(1,:))+1));
vaildLabels{i} = vaildData{i}(:,(length(features(1,:))+2):end);
end
end
end