如何在Matlab中创建遗忘的交叉验证? [英] how to create leave one out cross validation in matlab?
问题描述
我仍然对我的代码感到困惑.我试图在matlab中实现留一法交叉验证,以进行分类.所以在这里.我从训练中取出一个数据变成测试数据.我已经在matlab中编写了代码.但我不确定是否正确,因为结果不正确.有人可以帮我改正它吗?非常感谢.
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.
这是我的代码:
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
推荐答案
这是我通常用于创建留出交叉验证的内容.
This is what I usually use to create leave one out cross-validation.
[Train, Test] = crossvalind('LeaveMOut', N, M)
在这里,N
将是您在训练和测试集中拥有的样本总数. M=1
在您的情况下.您可以将其放入for循环中.
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.
这篇关于如何在Matlab中创建遗忘的交叉验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!