0

你能帮我如何在 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);

在我的主要功能中,我调用了测试文件夹来测试数据。但在这种情况下,我想使用留一交叉验证来测试数据,所以我不再调用该目录。你能帮我解决这个问题吗?因此,我可以使用其中一种训练数据进行测试。

4

1 回答 1

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

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

于 2013-03-11T17:46:05.790 回答