2016-02-21 55 views
0

目前,我正在使用此代码进行MATLAB R2015b支持向量机(SVM)10倍交叉验证。MATLAB支持向量机(SVM)交叉验证实现提高代码速度

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; 

我需要提取此二进制分类的敏感性和特异性。否则,我在循环中运行此代码。

这种交叉验证结构非常缓慢。任何其他实现更快的执行?

+0

我认为有一个名为'cvpartition'的函数,可能有帮助。 – Rashid

回答

1

一个简单的方法是使用crossval函数的多线程。

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 

使用混淆矩阵可以轻松获得灵敏度和特异性。对于记录来说,脚本和Matlab提供的crossval函数之间没有时间上的改进。

交叉点函数的替代方法是使用parfor 来拆分不同核心之间每次迭代的计算。对于任何并行计算,parpool必须先运行(或可能由某些函数运行),并且需要几秒钟才能设置它。