1
votes

Currently I'm using this code for MATLAB R2015b support vector machine(SVM) 10-fold cross validation.

indices = crossvalind('Kfold',output,10);
cp = classperf(binary_output);

for i = 1:10
    test = (indices == i); train = ~test;

    SVMModel = fitcsvm(INPUT(train,:), output(train,:),'KernelFunction','RBF',...
        'KernelScale','auto');
    class = predict(SVMModel, INPUT(test,:));
    classperf(cp,class,test);
end

z = cp.ErrorRate;
sensitivity = cp.Sensitivity;
specificity = cp.Specificity;

I need to extract sensitivity and specificity of this binary classification. Otherwise I'm running this code in a loop.

This structure of cross validation is so slow. Any other implementation for faster execution?

1
I think there is a function named cvpartition, which might help.Rashid

1 Answers

1
votes

An easy way is to use the multi-threading from the crossval function.

tic

%data partition
order = unique(y); % Order of the group labels
cp = cvpartition(y,'k',10); %10-folds

%prediction function
f = @(xtr,ytr,xte,yte)confusionmat(yte,...
predict(fitcsvm(xtr, ytr,'KernelFunction','RBF',...
    'KernelScale','auto'),xte),'order',order);

% missclassification error 
cfMat = crossval(f,INPUT,output,'partition',cp);
cfMat = reshape(sum(cfMat),2,2)
toc

With the confusion matrix you can get the sensitivity and specificity easily. For the record there is no improvement of time between your script and the crossval function provided by Matlab.

An alternative to the crossval function is to use parfor to split the computation of each iteration between different cores.

Ps: for any parallel computation, parpool has to be run before (or that might be run by some of the functions) and it takes few seconds to set it.