循环中的重复分类精度始终相同

时间:2017-09-30 08:34:44

标签: matlab classification svm

我有非常简单的二进制分类代码(见下文)。当我在Matlab中重新运行它时(只需手动按下"运行"按钮),每次运行都会让我对14个主题中的每一个都有不同的准确度。但是,如果我循环遍历我的代码nrPermute次,循环的每次迭代都会给出相应主题的相同精度 - 为什么呢?因此,在第一个代码中,不同运行的平均值(准确度)是不同的,而在第二个代码中,对于不同的迭代,它总是相同的。以下两个代码

代码,每个主题只进行一次10倍交叉验证:

%% SVM-Classification
nrFolds = 10; %number of folds of crossvalidation, 10 is standard
kernel = 'linear'; % 'linear', 'rbf' or 'polynomial'
C = 1; 
solver = 'L1QP';

cvFolds = crossvalind('Kfold', labels, nrFolds);

for k = 1:14

for i = 1:nrFolds                            % iteratre through each fold
    testIdx = (cvFolds == i);                % indices of test instances
    trainIdx = ~testIdx;                     % indices training instances

    % train the SVM
    cl = fitcsvm(features(trainIdx,:), 
     labels(trainIdx),'KernelFunction',kernel,'Standardize',true,...
    'BoxConstraint',C,'ClassNames',[0,1],'Solver',solver);

    [label,scores] =  predict(cl, features(testIdx,:));
    eq = sum(label==labels(testIdx));
    accuracy(i) = eq/numel(labels(testIdx));

end

crossValAcc(k) = mean(accuracy);

end

代码,其中每个10倍交叉验证重复nrPermute次:

%% SVM-Classification
nrFolds = 10; %number of folds of crossvalidation, 10 is standard
kernel = 'linear'; % 'linear', 'rbf' or 'polynomial'
C = 1; 
solver = 'L1QP';

cvFolds = crossvalind('Kfold', labels, nrFolds);
nrPermute = 5;


for k = 1:14
for p = 1:nrPermute

for i = 1:nrFolds                            % iteratre through each fold
    testIdx = (cvFolds == i);                % indices of test instances
    trainIdx = ~testIdx;                     % indices training instances

    % train the SVM
    cl = fitcsvm(features(trainIdx,:), 
     labels(trainIdx),'KernelFunction',kernel,'Standardize',true,...
    'BoxConstraint',C,'ClassNames',[0,1],'Solver',solver);

    [label,scores] =  predict(cl, features(testIdx,:));
    eq = sum(label==labels(testIdx));
    accuracy(i) = eq/numel(labels(testIdx));

end

    accSubj(p) = mean(accuracy); % accuracy of each permutation

end

crossValAcc(k) = mean(accSubj);


end

1 个答案:

答案 0 :(得分:0)

如果对其他人也有用,我想出来:排列的循环应该在cvFolds = crossvalind之外(' Kfold',标签,nrFolds);这样折叠的分布就会被重新洗牌!