支持向量机多分类libSVM二(UCI中iris数据分类)

支持向量机多分类可以采用两种方式,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)


2.一对一  one vs one  

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)


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值