-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_test_split.m
44 lines (40 loc) · 1.21 KB
/
train_test_split.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
function [train, test, label_train, label_test] = train_test_split(data, label, per)
%TRAIN_TEST_SPLIT use to divide the dataset into trainset and testset and
% return its' label.
%
% Parameters include:
% 'data' the original dataset.
% 'label' the data's label.
% 'per' percentage of test data in all data.
%
% Example 1
% [au_markov, au_texture] = preproc('../Au', @markov, @texturebyglcm);
% [len, ~] = size(au_markov);
% label = ones(len, 1);
% per = 0.3;
% [train, test, label_train, label_test] = train_test_split(au_markov,
% label, per);
%
% $ Date: 2019-6-14 09:44:20 $
[a, gT] = size(data);
trainSize = round(a*(1-per));
testSize = a-trainSize;
randSeq = randperm(a);
train = zeros(trainSize, gT);
test = zeros(testSize, gT);
label_train = zeros(trainSize, 1);
label_test = zeros(testSize, 1);
idx = 1;
for i = 1:trainSize
pos = randSeq(idx);
train(i, :) = data(pos, :);
label_train(i, :) = label(pos, :);
idx = idx+1;
end
for i = 1:testSize
pos = randSeq(idx);
test(i, :) = data(pos, :);
label_test(i, :) = label(pos, :);
idx = idx+1;
end
end