我知道LIBSVM在多类SVM方面只允许一对一分类。但是,我想稍微调整它以执行一对一的分类。我试图在下面进行一对一的比赛。这是正确的方法吗?
代码:
TrainLabel;TrainVec;TestVec;TestLaBel;
u=unique(TrainLabel);
N=length(u);
if(N>2)
itr=1;
classes=0;
while((classes~=1)&&(itr<=length(u)))
c1=(TrainLabel==u(itr));
newClass=c1;
model = svmtrain(TrainLabel, TrainVec, '-c 1 -g 0.00154');
[predict_label, accuracy, dec_values] = svmpredict(TestLabel, TestVec, model);
itr=itr+1;
end
itr=itr-1;
end
我可能犯过一些错误。我想听听一些反馈。感谢。
第二部分: 正如葡萄藤所说: 我需要做Sum-pooling(或投票作为简化解决方案)来得出最终答案。我不知道该怎么做。我需要一些帮助;我看到了python文件,但仍然不太确定。我需要一些帮助。
答案 0 :(得分:10)
%# Fisher Iris dataset
load fisheriris
[~,~,labels] = unique(species); %# labels: 1/2/3
data = zscore(meas); %# scale features
numInst = size(data,1);
numLabels = max(labels);
%# split training/testing
idx = randperm(numInst);
numTrain = 100; numTest = numInst - numTrain;
trainData = data(idx(1:numTrain),:); testData = data(idx(numTrain+1:end),:);
trainLabel = labels(idx(1:numTrain)); testLabel = labels(idx(numTrain+1:end));
%# train one-against-all models
model = cell(numLabels,1);
for k=1:numLabels
model{k} = svmtrain(double(trainLabel==k), trainData, '-c 1 -g 0.2 -b 1');
end
%# get probability estimates of test instances using each model
prob = zeros(numTest,numLabels);
for k=1:numLabels
[~,~,p] = svmpredict(double(testLabel==k), testData, model{k}, '-b 1');
prob(:,k) = p(:,model{k}.Label==1); %# probability of class==k
end
%# predict the class with the highest probability
[~,pred] = max(prob,[],2);
acc = sum(pred == testLabel) ./ numel(testLabel) %# accuracy
C = confusionmat(testLabel, pred) %# confusion matrix
答案 1 :(得分:4)
从代码我可以看到你试图首先将标签转换为“某个类”与“不是这个类”,然后调用LibSVM进行训练和测试。一些问题和建议:
TrainingLabel
进行培训?在我看来,它应该是model = svmtrain(newClass, TrainVec, '-c 1 -g 0.00154');
吗?-b
开关来启用概率输出也将提高准确性。答案 2 :(得分:1)
您也可以使用以下决策值
,而不是概率估算[~,~,d] = svmpredict(double(testLabel==k), testData, model{k});
prob(:,k) = d * (2 * model{i}.Label(1) - 1);
达到同样的目的。