【问题标题】:Repeated classification accuracies in a loop always the same循环中的重复分类精度始终相同
【发布时间】:2018-03-12 01:56:13
【问题描述】:

我有非常简单的二进制分类代码(见下文)。当我在 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

【问题讨论】:

    标签: matlab classification svm


    【解决方案1】:

    如果这对其他人也有用,我想办法:置换循环应该在 cvFolds = crossvalind('Kfold', labels, nrFolds); 之外。这样折叠的分布被重新洗牌!

    【讨论】:

      猜你喜欢
      • 2016-07-14
      • 2016-07-12
      • 1970-01-01
      • 2013-07-20
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多