在matlab中省略一个交叉验证

在matlab中省略一个交叉验证,matlab,svm,Matlab,Svm,你能帮助我如何在Matlab中使用留一交叉验证吗?因为我使用的数据很小。我用10个数据训练真阳性,10个数据训练假阳性。我尝试了在这里找到的代码。这是我的培训职能: Function [ C,F ] = classification_test_samesize() dn='D:\thesis\mthesis\code program\FalsePositive\' db=dir(strcat(dn,'*.dcm')); F=[]; C=[]; for(

你能帮助我如何在Matlab中使用留一交叉验证吗?因为我使用的数据很小。我用10个数据训练真阳性,10个数据训练假阳性。我尝试了在这里找到的代码。这是我的培训职能:

Function [ C,F ] = classification_test_samesize()

    dn='D:\thesis\mthesis\code program\FalsePositive\'
    db=dir(strcat(dn,'*.dcm'));
    F=[];
    C=[];

    for(i=1:1:length(db))
        name=db(i).name;
        name=strcat(dn,name);
        im1=dicomread(name);
        [out] = func_feature(im1);
        f = [out];
        F = [F;f];
        C=[C;-1];

    end

    % false positive is -1 th class


    dn='D:\thesis\mthesis\code program\TruePositive\'
    db=dir(strcat(dn,'*.dcm'));

    for(i=1:1:length(db))
        name=db(i).name;
        name=strcat(dn,name);
        im1=dicomread(name);
        [out] = func_feature(im1);
        f = [out];
        F = [F;f];
        C=[C;1];
        % true positive is 1 th class
    end
end
这是我的主要功能

clc

direktori= uigetdir;
slash = '\';
direktori=strcat(direktori,slash);
dn=direktori;
db=dir(strcat(dn,'*.dcm'));
ftest=[];

for(i=1:1:length(db))
    name=db(i).name;
    name=strcat(dn,name);

    im1=dicomread(name);
    [out] = func_feature(im1);
    f = [out];
    ftest = [ftest;f];
end

[C,F] = classification_test_samesize();
svmStruct = svmtrain(F,C,'showplot',true,'Kernel_Function','rbf');
result_class = svmclassify(svmStruct,ftest,'showplot',true);

在我的主函数中,我调用testing文件夹来测试数据。但是在这种情况下,我想使用leave-one-out交叉验证来测试数据,所以我不再被称为目录。你能帮我解决这个问题吗?因此,我可以使用其中一个训练数据进行测试。

由于我的第一个示例让您感到困惑,我再次查看了您的完整代码,我认为我有一种更简单的方法来实现您想要的。首先,这里是分类函数:它看起来很像原始函数,但我不想取出任何数据-相反,我们将在计算后选择一些数据点:

Function [ C,F ] = classification_test_samesize(fpDir, tpDir)
% compute the C and F for files in the two directories given
db=dir(fullfile(fpDir,'*.dcm')); % preferred to strcat
F=[];
C=zeros(numel(db), 1);
%
% don't use 'i' as variable name - it is built in, and means "sqrt(-1)"
for ii= 1:numel(db)
    name=fullfile(fpDir, db(ii).name); % the correct way to concatenate directory and file name
    im1=dicomread(name);
    % I would like to think you could pre-allocate size for F
    % and use F(ii)=func_feature(im1); directly?
    F = [F; func_feature(im1)];
    C(ii) = -1;   
    fprintf(1, 'file name: %s; class -1\n', db(ii).name); 
end

% false positive is -1 th class

db=dir(fullpath(tpDir,'*.dcm'));
%
for ii = 1:numel(db)
    im1=dicomread(fullfile(tpDir, db(ii).name);
    F = [F; func_feature(im1)];
    C = [C;1];
    fprintf(1, 'file name: %s; class 1\n', db(ii).name);
% true positive is 1 th class
end
end
您使用与真负目录和真正目录对应的两个参数调用此函数,此时您完成了所有计算。现在,您可以选择这些数据中的哪些用于培训,哪些用于测试

clc    
% pick the number of the file we are using for test instead of training:

[C,F] = classification_test_samesize('D:\thesis\mthesis\code program\FalsePositive\', ...
    'D:\thesis\mthesis\code program\TruePositive\');

% now we are going to pick some of these for training, and others for verification
numTest = 5; % whatever number you want to set aside for verification
% >>>> changed the next three lines <<<<
randomIndx = randperm(1:numel(C));
testVal = randomIndx(1:numTest);
trainingSet = randomIndex(numTest+1:end);

% do the training: this uses those elements from F and C which are chosen for training:
Ctrain = C(trainingSet);
Ftrain = F(trainingSet);

disp('The following values now exist in "C":')
display(unique(Ctrain)); % this is to confirm there are exactly two classes...

svmStruct = svmtrain(Ftrain,Ctrain,'showplot',true,'Kernel_Function','rbf');

% finally, classify the other values:
result_class = svmclassify(svmStruct,F(testVal),'showplot',true);
clc
%选择我们用于测试而非培训的文件编号:
[C,F]=分类测试样本('D:\thesis\mthesis\code program\false positive\”。。。
“D:\thesis\mthesis\code program\TruePositive\”);
%现在我们将挑选其中一些用于培训,另一些用于验证
numTest=5;%不管你想留什么号码来核实

%>>>>更改了接下来的三行,非常感谢您的回答。但是我对matlab非常熟悉。我仍然不知道如何调用我的主函数,该函数已被您更正。因为我必须输入两个参数,即leaveOutPP和leaveOutTP。我希望你能帮我解决这个问题。。谢谢,当我运行它时出现了一些错误“使用svmtrain的错误(第335行)Y必须正好包含方法“SMO”的两组。main(第15行)中的错误svmStruct=svmtrain(Ftrain,Ctrain,'showplot',true,'Kernel_Function','rbf');你有什么办法解决它吗?非常感谢你的帮助。请检查
Ctrain
的大小,以及其中的值。我希望它是一个只包含
1
-1
的列向量-如果不是这样,我们需要找出原因。顺便说一下,我在计算机中发现了一个可能的问题修改
testVal
-如编写的那样,您可能会得到重复的值。我现在将更新该代码。我尝试运行您的代码,但再次出现一些错误:>在main2中10个未定义的函数或变量“numVal”。main2中的错误(第11行)testVal=randomIndex(1:numVal);如何修复它?打字错误-应该是
numTest
。抱歉!