在matlab中保留一个交叉验证

时间:2013-03-11 17:18:26

标签: 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(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);

在我的main函数中,我调用了测试文件夹来测试数据。但是在这种情况下,我想使用Leave one out cross validation来测试数据,所以我不会再次调用该目录。你能帮帮我解决这个问题吗?所以,我可以使用其中一个训练数据进行测试。

1 个答案:

答案 0 :(得分:1)

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

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);

我希望你能从这里拿走它......