matlab libsvm multilass one反对所有分类

时间:2015-05-11 18:09:04

标签: matlab classification libsvm

我是分类新手并尝试使用libsvm进行多类分类。我有歌曲功能,想要分为3个班级。代码有效,但输出(pred)只为所有测试数据提供一个标签。例如pred = [2; 2; 2; 2]; 我所做的任何改变都不会对输出产生影响。我没有得到我错的地方。任何人都可以帮助我。以下是代码

load 113gender

s=cellstr(types);
[~,~,labels] = unique(s); 
data = allfeature;
numInst = size(data,1);
numLabels = max(labels);
numTest = size(alltest,1);

trainData = data;
trainLabel = labels;
testData = alltest;
%testLabel = ones(22,1);
%testLabel = [1;1;1;1;1;1;2;2;2;2;2;2;2;2;3;3;3;3;3;3;3;3];
testLabel = [3;3;3;3;3;3;2;2;2;2;2;2;2;2;1;1;1;1;1;1;1;1];

%# 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)  
C = confusionmat(testLabel, pred)                  

0 个答案:

没有答案