【问题标题】:support vector machines in matlabmatlab中的支持向量机
【发布时间】:2011-06-25 23:32:37
【问题描述】:

您能否举一个在 matlab 中使用支持向量机 (SVM) 对 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

【问题讨论】:

    标签: matlab artificial-intelligence machine-learning classification svm


    【解决方案1】:

    MATLAB 目前不支持多类 SVM。您可以使用svmtrain(2-classes)来实现这一点,但使用标准 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);
    

    【讨论】:

    • 上面的例子有什么分类的例子吗?
    • 如果我执行 model = svmtrain(Classes(train),Attributes(train,:),'-s 0 -t 2');我得到:???在 172 Group 处使用 ==> svmtrain 时出错,必须是向量。
    • @darkcminor:你复制粘贴了我提供的所有代码吗?它对我有用。
    • 你是如何纠正Group must be a vector错误信息的? @cMinor
    • @madCode 如果您仍在寻找解决方案:stackoverflow.com/questions/15619584/…
    【解决方案2】:

    SVM 最初是为二进制分类而设计的。然后它们被扩展为处理多类问题。其思想是将问题分解为许多二元类问题,然后将它们组合起来得到预测。

    一种称为one-against-all的方法,构建与类一样多的二元分类器,每个分类器都经过训练以将一个类与其他类分开。为了预测一个新的实例,我们选择具有最大决策函数值的分类器。

    另一种称为 one-against-one 的方法(我相信它在 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
    

    【讨论】:

    • @Amro 如果我在 cvpartition 中使用 k 折,取 k 折的平均准确度是否正确?
    • @sum2000:是的,您报告了 k 折的平均准确率,然后返回从整个训练数据中学习到的模型
    • 但是,每次运行代码的精度都不一样,当我使用 cvpartition 的内置函数计算错误时,情况并非如此。
    • @sum2000:我猜这是因为每次运行代码时数据的分区方式都不同。如果要重现结果,请考虑为随机数生成器设置种子(请参阅rng 函数)
    猜你喜欢
    • 2017-09-19
    • 2012-04-20
    • 2014-05-12
    • 2015-09-08
    • 2016-09-25
    • 2012-11-20
    • 2012-01-31
    相关资源
    最近更新 更多