gpt4 book ai didi

matlab - 多类 SVM(一个对所有)

转载 作者:太空宇宙 更新时间:2023-11-03 19:14:54 25 4
gpt4 key购买 nike

我知道 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 文件,但仍然不太确定。我需要一些帮助。

最佳答案

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

关于matlab - 多类 SVM(一个对所有),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8953326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com