Performance MATLAB支持向量机(SVM)交叉验证实现,提高代码速度
目前我正在使用这段代码进行MatlabR2015B支持向量机(SVM)10倍交叉验证Performance MATLAB支持向量机(SVM)交叉验证实现,提高代码速度,performance,matlab,svm,Performance,Matlab,Svm,目前我正在使用这段代码进行MatlabR2015B支持向量机(SVM)10倍交叉验证 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'
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;
我需要提取这种二元分类的敏感性和特异性。否则,我将在循环中运行此代码
这种交叉验证的结构太慢了。任何其他更快执行的实现?一个简单的方法是使用函数中的多线程
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函数之间的时间没有任何改进
crossval函数的替代方法是使用
在不同的核心之间分割每次迭代的计算
Ps:对于任何并行计算,
parpool
必须在之前运行(或者可能由某些函数运行),并且需要几秒钟来设置它。我认为有一个名为cvpartition
的函数可能会有所帮助。