MATLAB Answers

splitting dataset into training set and testing set

12 views (last 30 days)
Munshida P
Munshida P on 14 Jan 2020
Commented: Munshida P on 16 Jan 2020
I have 400 images in my dataset(images).I want to split the dataset into 80% for training and 20% for testing.the below attached code works but , test_idx is empty?why?
train_idx contains 320 images.test_idx is empty.
% Load Image dataset
faceDatabase = imageSet('facedatabaseatt','recursive');
%splitting into training and testing sets
N = 400; % number of images
idx = 1:N ;
PD = 0.80 ;
train_idx = idx(1:round(PD*N)); % training indices
test_idx = idx(round(PD*N)+1:end,:) ; % test indices


Sign in to comment.

Accepted Answer

Akira Agata
Akira Agata on 15 Jan 2020
You can split your dataset by using partition function, like:
[setTrain, setTest] = partition(faceDatabase, [0.8, 0.2], 'randomized');


Munshida P
Munshida P on 15 Jan 2020
I have to split the dataset manually, without using built-in functions.
Akira Agata
Akira Agata on 15 Jan 2020
OK. Then, how about the following?
N = imgSet.Count; % number of images
PD = 0.8;
train_idx = sort(randperm(N,round(N*PD)));
test_idx = setxor(train_idx,1:N);
setTrain = select(faceDatabase,train_idx);
setTest = select(faceDatabase,test_idx);
Munshida P
Munshida P on 16 Jan 2020
Thank you sir. The above code works if i copy all 400 images in a single folder.
But, the original dataset ORL Facedatabaseatt contains 40 folders s1,s2,.....,s40(10 images each of 40 persons).
like that 10x40=400 images.i have to choose 8 images from each person for testing and 2 images for testing.
the following error was occured .
Index exceeds the number of array elements (10).
Error in imageSet/selectProperties (line 526)
imgSet.ImageLocation = imgSet.ImageLocation(index);
Error in imageSet/select (line 394)
out(n) = selectProperties(this(end), index);
Error in split (line 12)
setTrain = select(faceDatabase,train_idx);

Sign in to comment.

More Answers (0)

Sign in to answer this question.