Leave one out crossvalind in Matlab

2019-08-05 18:10发布

问题:

I have extracted HOG features for male and female pictures, now, I'm trying to use the Leave-one-out-method to classify my data. Due the standard way to write it in Matlab is:

[Train, Test] = crossvalind('LeaveMOut', N, M);

What I should write instead of N and M? Also, should I write above code statement inside or outside a loop? this is my code, where I have training folder for Male (80 images) and female (80 images), and another one for testing (10 random images).

for i = 1:10  
 [Train, Test] = crossvalind('LeaveMOut', N, 1);
 SVMStruct = svmtrain(Training_Set (Train), train_label (Train));
 Gender = svmclassify(SVMStruct, Test_Set_MF (Test)); 
end 

Notes:

  • Training_Set: an array consist of HOG features of training folder images.
  • Test_Set_MF: an array consist of HOG features of test folder images.
  • N: total number of images in training folder.
  • SVM should detect which images are male and which are female.

回答1:

I will focus on how to use crossvalind for the leave-one-out-method.

I assume you want to select random sets inside a loop. N is the length of your data vector. M is the number of randomly selected observations in Test. Respectively M is the number of observations left out in Train. This means you have to set N to the length of your training-set. With M you can specify how many values you want in your Test-output, respectively you want to left out in your Train-output.

Here is an example, selecting M=2 observations out of the dataset.

dataset = [1 2 3 4 5 6 7 8 9 10];
N = length(dataset);
M = 2;

for i = 1:5
    [Train, Test] = crossvalind('LeaveMOut', N, M);
    % do whatever you want with Train and Test
    dataset(Test) % display the test-entries
end

This outputs: (this is generated randomly, so you won't have the same result)

ans =
     1     9
ans =
     6     8
ans =
     7    10
ans =
     4     5
ans =
     4     7

As you have it in your code according to this post, you need to adjust it for a matrix of features:

Training_Set = rand(10,3);     % 10 samples with 3 features each

N = size(Training_Set,1);
M = 2;

for i = 1:5
    [Train, Test] = crossvalind('LeaveMOut', N, 2);
    Training_Set(Train,:) % displays the data to train
end