MATLAB中的支持向量机

11

你能举出一个使用支持向量机(SVM)在matlab中进行4类分类的例子吗?例如:

atribute_1  atribute_2 atribute_3 atribute_4 class
1           2          3           4             0
1           2          3           5             0
0           2          6           4             1
0           3          3           8             1
7           2          6           4             2
9           1          7           10            3
2个回答

26

SVM最初被设计用于二分类问题,后来扩展到处理多类问题。其思想是将问题分解为许多二元分类问题,然后将它们组合起来以获得预测结果。

一种方法称为“一对多”,建立与类别数目相同的二元分类器,每个分类器训练用于将一个类别与其他类别分离。要预测新实例,我们选择具有最大决策函数值的分类器。

另一种称为“一对一”(我认为在LibSVM中使用),则构建k(k-1)/2个二元分类器,训练用于将每对类别相互分离,并使用多数投票方案(最大胜利策略)来确定输出预测。

还有其他方法,例如使用纠错输出码(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

如果我在使用cvpartition中的k folds,那么取k folds的平均准确率是否正确? @Amro - sum2000
1
@sum2000:是的,您可以报告k折交叉验证的平均准确率,然后返回从整个训练数据中学习到的模型。 - Amro
但是,每次运行代码时准确性都不同,而使用cvpartition的内置函数计算误差时则不会出现这种情况。 - sum2000
1
@sum2000:我猜这是因为每次运行代码时数据的分区方式都不同。如果你想要重现结果,请考虑为随机数生成器设置一个种子(参见 rng 函数)。 - Amro

15
目前,MATLAB不支持多类SVM。您可以使用svmtrain(2类)来实现多类SVM,但是使用标准SVM软件包会更容易。
我使用过LIBSVM并确认它非常容易使用。
%%# Your data
D = [
1           2          3           4             0
1           2          3           5             0
0           2          6           4             1
0           3          3           8             1
7           2          6           4             2
9           1          7           10            3];
%%# For clarity
Attributes = D(:,1:4);
Classes = D(:,5);
train = [1 3 5 6];
test = [2 4];

%%# Train
model = svmtrain(Classes(train),Attributes(train,:),'-s 0 -t 2');

%%# Test
[predict_label, accuracy, prob_estimates] = svmpredict(Classes(test), Attributes(test,:), model);

有没有对上面的例子进行分类的示例? - edgarmtze
如果我执行 model = svmtrain(Classes(train),Attributes(train,:),'-s 0 -t 2'); 我会得到: ??? Error using ==> svmtrain at 172 Group must be a vector. - edgarmtze
@darkcminor:你是不是复制粘贴了我提供的所有代码?它在我的电脑上可以运行。 - Jacob
你是如何纠正“Group must be a vector”错误信息的?@cMinor - madCode
1
如果您仍在寻找解决方案:@madCode http://stackoverflow.com/questions/15619584/libsvm-error-group-must-be-a-vector - Framester

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接