MATLAB Answers

Split a dataset into training set and test set using the cross-validation principle

21 views (last 30 days)
Patrice Monkam
Patrice Monkam on 19 Dec 2020
Answered: yanqi liu on 1 Feb 2021
Hello everyone,
I have a dataset set of about 50 images, and I would like to split the dataset into training and test sets. I would like to do it in the way as cross-validation. That is, I would like to split the data into 5 equivalent subsets. Then, four of the subsets would be used as training data and the remaining one subset for testing. Finally, I would have five sets of experimental data comprising each a training set and a test set. I can perform this task online while training the network using some built-in functions. However, in this scenario, I would like to split the data offline (before the training) for conducting some experiments. Given my poor programming skills, I am unable to implement. Please, how can I achieve this? Any suggestions and comments would be highly appreciated.

Answers (1)

yanqi liu
yanqi liu on 1 Feb 2021
please use crossvalind,reference resources
such as
clc; clear all; close all;
% total 50
% split to 5
indices = crossvalind('Kfold', 50, 5);
for i=1:5
fprintf('\ndemo %d\n', i);
ind1=find(indices==i);
ind2=find(indices~=i);
fprintf('\nuse for train\n');
disp(ind2(:)');
fprintf('\nuse for test\n');
disp(ind1(:)');
end
demo 1
use for train
1 10
1 2 3 4 5 6 7 8 9 10
11 20
11 15 17 18 19 21 22 23 24 25
21 30
26 28 29 31 32 33 36 37 38 39
31 40
40 41 42 43 44 45 46 47 48 49
use for test
12 13 14 16 20 27 30 34 35 50

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!