支持向量机多分类可以采用两种方式,1.一对多 one vs rest 2.一对一 one vs one
数据集可以下载从我的资源中:
.一对多 one vs rest
clc;
clear all;
[iris_label,iris_data] = libsvmread('iris.scale');%读取数据到matlab格式
% [~,~,labels] = unique(species); %# labels: 1/2/3
% data = zscore(meas); %# scale features
numInst = size(iris_data,1);%个数 150
numLabels = max(iris_label);%个数3
%# split training/testing
idx = randperm(numInst); %把150个数据进行随机打乱
numTrain = 100;%取前100个
numTest = numInst - numTrain;
trainData = iris_data(idx(1:numTrain),:);
testData = iris_data(idx(numTrain+1:end),:);
trainLabel = iris_label(idx(1:numTrain));
testLabel = iris_label(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
model = cell(numLabels,1); %模型的个数
for k=1:numLabels
model{k} = svmtrain(double(trainLabel==k), trainData, '-c 1 -g 0.2 -b 1');
*
optimization finished, #iter = 25
nu = 0.113197
obj = -4.959999, rho = -0.028122
nSV = 12, nBSV = 7
Total nSV = 12
*
optimization finished, #iter = 19
nu = 0.110327
obj = -4.867614, rho = -0.021455
nSV = 11, nBSV = 8
Total nSV = 11
%测试集的精度
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
Accuracy = 100% (50/50) (classification)
Accuracy = 98% (49/50) (classification)
Accuracy = 98% (49/50) (classification)
for k=1:numLabels
model{k} = svmtrain(double(trainLabel==k), trainData, '-c 1 -g 0.2 -b 1');
*
optimization finished, #iter = 25
nu = 0.113197
obj = -4.959999, rho = -0.028122
nSV = 12, nBSV = 7
Total nSV = 12
*
optimization finished, #iter = 19
nu = 0.110327
obj = -4.867614, rho = -0.021455
nSV = 11, nBSV = 8
Total nSV = 11
%测试集的精度
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
Accuracy = 100% (50/50) (classification)
Accuracy = 98% (49/50) (classification)
Accuracy = 98% (49/50) (classification)
clc;
clear all;
[iris_label,iris_data] = libsvmread('iris.scale');%读取数据到matlab格式
% [~,~,labels] = unique(species); %# labels: 1/2/3
% data = zscore(meas); %# scale features
numInst = size(iris_data,1);
numLabels = max(iris_label);
%# split training/testing
idx = randperm(numInst);
numTrain = 100;
numTest = numInst - numTrain;
trainData = iris_data(idx(1:numTrain),:);
testData = iris_data(idx(numTrain+1:end),:);
trainLabel = iris_label(idx(1:numTrain));
testLabel = iris_label(idx(numTrain+1:end));
model= svmtrain(trainLabel, trainData, '-c 1 -g 0.2 -b 1');
[predict_label, accuracy, prob] = svmpredict(testLabel,testData, model,'-b 1');
% fprintf('准确率为%d.....\n',accuracy);
*
optimization finished, #iter = 14
nu = 0.172654
obj = -4.707420, rho = 0.125191
nSV = 10, nBSV = 6
Total nSV = 10
*
optimization finished, #iter = 9
nu = 0.147329
obj = -5.304480, rho = 0.111620
nSV = 10, nBSV = 8
Total nSV = 50
Accuracy = 98% (49/50) (classification)
optimization finished, #iter = 14
nu = 0.172654
obj = -4.707420, rho = 0.125191
nSV = 10, nBSV = 6
Total nSV = 10
*
optimization finished, #iter = 9
nu = 0.147329
obj = -5.304480, rho = 0.111620
nSV = 10, nBSV = 8
Total nSV = 50
Accuracy = 98% (49/50) (classification)