how to create leave one out cross validation in matlab? [duplicate]

风格不统一 提交于 2019-12-01 10:11:21

问题


I am still confused with my code. I tried to implement leave one out cross validation in matlab for classification. so in here . I take out one data from training become testing data. I already make a code in matlab. but Iam not sure it's correct because the result is wrong. can someone help me to correct it?? thank you very much.

this is my code :

clc    
[C,F] = train('D:\fp\',...
    'D:\tp\');


for i=size(F,1)
testVal = i;      
trainingSet = setdiff(1:numel(C), testVal); % use the rest for training

Ctrain = C(trainingSet,:);
Ftrain = F(trainingSet,:);
test= F(testVal,:);
svmStruct = svmtrain(Ftrain,Ctrain,'showplot',true,'Kernel_Function','rbf');
result_class(i)= svmclassify(svmStruct,test,'showplot',true);
ax(i)=result_class;
i=i+1;
end

回答1:


This is what I usually use to create leave one out cross-validation.

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

Here, N will be the number of total samples you have in your training+testing set. M=1 in your case. You can put this in a for loop.

Also, you can use random number generation to perform leave-one out crossvalidation without using predefined function.



来源:https://stackoverflow.com/questions/15451301/how-to-create-leave-one-out-cross-validation-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!