SVM最初是为二进制分类而设计的。然后将它们扩展为处理多类问题。想法是将问题分解为许多二元类问题,然后将它们组合以获得预测。
一种名为“永无止境”的方法,可以构建与所有类一样多的二进制分类器,每个分类器都经过训练可以将一个类与其他类分开。为了预测新实例,我们选择决策函数值最大的分类器。
另一种称为“ 一对一”的方法(我相信该方法在LibSVM中使用),构建k(k-1)/2
二进制分类器,经过训练将每对类别彼此分开,并使用多数表决方案(max-获胜策略)来确定输出预测。
还有其他方法,例如使用纠错输出代码(ECOC)来构建许多冗余的二进制分类器,并使用此冗余来获得更鲁棒的分类(使用与汉明码相同的思想)。
示例(一对一):
%# load dataset
load fisheriris
[g gn] = grp2idx(species); %# nominal class to numeric
%# split training/testing sets
[trainIdx testIdx] = crossvalind('HoldOut', species, 1/3);
pairwise = nchoosek(1:length(gn),2); %# 1-vs-1 pairwise models
svmModel = cell(size(pairwise,1),1); %# store binary-classifers
predTest = zeros(sum(testIdx),numel(svmModel)); %# store binary predictions
%# classify using one-against-one approach, SVM with 3rd degree poly kernel
for k=1:numel(svmModel)
%# get only training instances belonging to this pair
idx = trainIdx & any( bsxfun(@eq, g, pairwise(k,:)) , 2 );
%# train
svmModel{k} = svmtrain(meas(idx,:), g(idx), ...
'BoxConstraint',2e-1, 'Kernel_Function','polynomial', 'Polyorder',3);
%# test
predTest(:,k) = svmclassify(svmModel{k}, meas(testIdx,:));
end
pred = mode(predTest,2); %# voting: clasify as the class receiving most votes
%# performance
cmat = confusionmat(g(testIdx),pred);
acc = 100*sum(diag(cmat))./sum(cmat(:));
fprintf('SVM (1-against-1):\naccuracy = %.2f%%\n', acc);
fprintf('Confusion Matrix:\n'), disp(cmat)
这是一个示例输出:
SVM (1-against-1):
accuracy = 93.75%
Confusion Matrix:
16 0 0
0 14 2
0 1 15
0
您能否在Matlab中使用支持向量机(SVM)给出4类分类的示例,例如: