这是我的一对一的代码。此代码不是由@amro编写的。我无法理解为什么会这样。当我学习代码时,一切看起来都很简单。请帮我修理一下。我正在使用matlab2014a。
代码:One vs One
%# 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不是按结构返回的。
有人请修复此错误... 谢谢......
答案 0 :(得分:0)
您的错误位于第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参数名称!修复它并使用您的代码。