这两个问题之一可能与LIBSVM无关,这可能使您感到困惑。您应该尝试调整此答案,而忽略其他答案 。
您应该选择折痕,然后其余部分完全按照链接的问题进行处理。假设数据已加载到data
,标签已加载到labels
:
n = size(data,1);
ns = floor(n/10);
for fold=1:10,
if fold==1,
testindices= ((fold-1)*ns+1):fold*ns;
trainindices = fold*ns+1:n;
else
if fold==10,
testindices= ((fold-1)*ns+1):n;
trainindices = 1:(fold-1)*ns;
else
testindices= ((fold-1)*ns+1):fold*ns;
trainindices = [1:(fold-1)*ns,fold*ns+1:n];
end
end
% use testindices only for testing and train indices only for testing
trainLabel = label(trainindices);
trainData = data(trainindices,:);
testLabel = label(testindices);
testData = data(testindices,:)
%# train one-against-all models
model = cell(numLabels,1);
for k=1:numLabels
model{k} = svmtrain(double(trainLabel==k), trainData, '-c 1 -g 0.2 -b 1');
end
%# get probability estimates of test instances using each model
prob = zeros(size(testData,1),numLabels);
for k=1:numLabels
[~,~,p] = svmpredict(double(testLabel==k), testData, model{k}, '-b 1');
prob(:,k) = p(:,model{k}.Label==1); %# probability of class==k
end
%# predict the class with the highest probability
[~,pred] = max(prob,[],2);
acc = sum(pred == testLabel) ./ numel(testLabel) %# accuracy
C = confusionmat(testLabel, pred) %# confusion matrix
end
0
我想在MATLAB中针对所有 支持向量机的分类中进行10倍交叉验证。
我试图以某种方式混合这两个相关的答案:
但是,由于我是MATLAB及其语法的新手,所以直到现在我都没有设法使其工作。
另一方面,我在LibSVM README文件中仅看到以下几行关于交叉验证的信息,并且在那找不到任何相关示例:
谁能为我提供10倍交叉验证和反对所有分类的示例?