资源简介
SVM(支持向量机)是模式识别和机器学习中的重要的数据分类的方法.本代码可以实现三分类.
代码片段和文件信息
clear;
irisData = dlmread(‘iris.data‘);
plotAxis = [min(irisData(: 3)) max(irisData(: 3)) min(irisData(: 4)) max(irisData(: 4))];
kernel = ‘rbf‘;
c = [1 10 100 1000 10000];
sigma = 1 : 0.5: 3;
[C Sigma] = meshgrid(c sigma);
C = C(:);
Sigma = Sigma(:);
Err = zeros(size(C));
n = length(Err);
Xtest = irisData(1 : 150 3 : 4);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% First train Isis-setosa(1) and Isis-versicolor(2) using grid search.
%
disp(‘.....................................................................‘)
X = irisData(1 : 100 3 : 4);
Y = irisData(1 : 100 5);
Y(find(Y==1)) = 1;
Y(find(Y==2)) = -1;
Xtrain = X;
disp(‘Training Isis-setosa(1) and Isis-versicolor(2) using grid search.‘)
disp(‘Please wait a while.‘);
for i = 1 : n
[alphaStar bStar SVIndex] = yxcSVMtrain(X Y C(i) kernel Sigma(i));
[YClassified Z Err(i)] = yxcSVMclassifer(Xtrain Xtrain Y alphaStar bStar kernel Sigma(i));
end
[mErr i] = min(Err);
%Compute the minimum one again using the optimal C and Sigma.
[alphaStar bStar SVIndex] = yxcSVMtrain(X Y C(i) kernel Sigma(i));
[YClassified Z Err(i)] = yxcSVMclassifer(Xtrain Xtrain Y alphaStar bStar kernel Sigma(i));
disp(sprintf(‘Error rate is %%%d with C = %d sigma = %d‘ mErr*100 C(i) Sigma(i)));
disp(sprintf(‘There are %d support vectors %d out of %d are wrongly classified‘ ...
length(SVIndex) mErr*length(Y) length(Y)));
yxcSVMplot(X Y SVIndex alphaStar bStar kernel Sigma(i) plotAxis);
title(sprintf(‘Isis-setosa(1) and Isis-versicolor(2) SV = %d Err = %%%d C = %d Sigma = %d‘ ...
length(SVIndex) mErr*100 C(i) Sigma(i)));
% Test all data on the Isis-setosa(1) and Isis-versicolor(2) data set.
[Y12 Z] = yxcSVMclassifer(Xtrain Xtest Y alphaStar bStar kernel Sigma(i));
Y12(find(Y12==1)) = 1;
Y12(find(Y12==-1)) = 2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Then train Isis-versicolor(2) and Isis-virginica(3) using grid search.
%
disp(‘.....................................................................‘)
X = irisData(51 : 150 3 : 4);
Y = irisData(51 : 150 5);
Y(find(Y==2)) = 1;
Y(find(Y==3)) = -1;
Xtrain = X;
disp(‘Training Isis-versicolor(2) and Isis-virginica(3) using grid search.‘)
disp(‘Please wait a while.‘);
for i = 1 : n
[alphaStar bStar SVIndex] = yxcSVMtrain(X Y C(i) kernel Sigma(i));
[YClassified Z Err(i)] = yxcSVMclassifer(Xtrain Xtrain Y alphaStar bStar kernel Sigma(i));
%disp(sprintf(‘Cross Validation: %d in %d‘ i n));
end
[mErr i] = min(Err);
%Compute the minimum one again using the optimal C and Sigma.
[alphaStar bStar SVIndex] = yxcSVMtrain(X Y C(i) kernel Sigma(i));
[YClassified Z Err(i)] = yxcSVMclassifer(Xtrain Xtrain Y alphaStar bStar kernel Sigma(i));
disp(sprintf(‘Error rate is %%%d with C = %d sigma = %d‘ mErr*100 C(i) Sigma(i
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2699 2007-04-04 10:23 三分类_svm_matlab\iris.data
文件 6004 2010-12-19 14:53 三分类_svm_matlab\three_SVM.m
文件 610 2010-12-19 14:53 三分类_svm_matlab\three_SVMclassifer.m
文件 575 2010-12-19 14:53 三分类_svm_matlab\three_SVMkernel.m
文件 1051 2010-12-19 14:54 三分类_svm_matlab\three_SVMplot.m
文件 1243 2010-12-19 14:54 三分类_svm_matlab\three_SVMtrain.m
目录 0 2010-12-19 14:56 三分类_svm_matlab
----------- --------- ---------- ----- ----
12182 7
- 上一篇:zbar支持中文dll
- 下一篇:基于LABVIEW的温度数据采集系统的软件设计
评论
共有 条评论