【问题标题】:one vs one svm multiclass classification matlab code一对一支持向量机多类分类matlab代码
【发布时间】:2016-03-15 01:42:28
【问题描述】:

这是我的一对一代码。此代码不是由@amro 编写的。我不明白为什么会这样。当我学习代码时,一切看起来都很简单。请帮我修复它。我正在使用 matlab2014a。

代码:一对一

%# 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),'-s 0 -t 0');

    %# 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)

错误:

Reference to non-existent field 'SupportVectors'.

Error in svmclassify (line 60)
if size(sample,2)~=size(svmStruct.SupportVectors,2)

Error in test_onevsone (line 21)
    predTest(:,k) = svmclassify(svmModel{k}, meas(testIdx,:));

我也试试,svmModel{1}.SupportVectors。看起来 SupportVectors 不是按结构返回的。

请有人修复这个错误... 谢谢...

【问题讨论】:

    标签: matlab classification svm libsvm


    【解决方案1】:

    您的错误在第 18 行,svmModel{k} = svmtrain(meas(idx,:), g(idx),'-s 0 -t 0');。 svmtrain 函数接受 2 + 2n 输入。正确的语法是:svmtrain(Training, Group)svmtrain(Training, Group, Name, Value)。名称和值是对参数。 Name 是参数名称,Value 是对应的值。您使用 '-s 0 -t' 作为第三个,这不是 matlab 参数名称!修复它并使用您的代码。

    【讨论】:

      猜你喜欢
      • 2013-08-31
      • 2011-10-15
      • 2011-07-01
      • 2021-06-28
      • 2014-04-08
      • 2015-06-03
      • 2021-04-10
      • 2017-08-18
      • 2013-07-14
      相关资源
      最近更新 更多