<BR>
% 支持向量机C-SVC分类算法<BR>
clear all<BR>
% ------------------------------------------------------------%<BR>
% 构造两类训练数据集<BR>
fprintf('Support Vector Classification C_SVC \n')<BR>
fprintf('_____________________________________\n')<BR>
fprintf('Constructing ...\n');<BR>
C = 100; % 拉格朗日乘子上界<BR>
ker = struct('type','linear');%选择核函数<BR>
% linear : k(x,y) = x'*y<BR>
n = 50;<BR>
randn('state',2);%选择正态分布数据<BR>
x1 = randn(n,2);<BR>
y1 = ones(n,1);<BR>
x2 = 4+randn(n,2);<BR>
y2 = -ones(n,1);<BR>
figure;<BR>
plot(x1(:,1),x1(:,2),'bx',x2(:,1),x2(:,2),'k.');<BR>
hold on;<BR>
X = [x1;x2]; % 训练样本,n×d的矩阵,n为样本个数,d为样本维数<BR>
Y = [y1;y2]; % 训练目标,n×1的矩阵,n为样本个数,值为+1或-1<BR>
% ------------------------------------------------------------%<BR>
% train %<BR>
fprintf('training ...\n');<BR>
tic<BR>
% ------------------------------------------------------------%<BR>
% train%<BR>
% ------------------------------------------------------------%<BR>
% 解二次优化方城<BR>
n = length(Y);<BR>
H = (Y*Y').*Calckernel(ker,X,X);<BR>
f = -ones(n,1);<BR>
A = [];<BR>
b = [];<BR>
Aeq = Y';<BR>
beq = 0;<BR>
lb = zeros(n,1);<BR>
ub = C*ones(n,1);<BR>
a0 = zeros(n,1);<BR>
options = optimset;<BR>
options.LargeScale = 'off';<BR>
options.Display = 'off';<BR>
[a,fval,eXitflag,output,lambda] = quadprog(H,f,A,b,Aeq,beq,lb,ub,a0,options);<BR>
eXitflag<BR>
% 输出 svm<BR>
svm.ker = ker;<BR>
svm.x = X;<BR>
svm.y = Y;<BR>
svm.a = a;<BR>
% ------------------------------------------------------------%<BR>
% ------------------------------------------------------------%<BR>
fprintf('training time is t_train:\n');<BR>
t_train = toc<BR>
% ------------------------------------------------------------%<BR>
% 寻找支持向量<BR>
a = svm.a;<BR>
epsilon = 1e-8; % 如果小于此值则认为是0<BR>
i_sv = find(a>epsilon); % 支持向量下标<BR>
fprintf('Support Vectors :')<BR>
i_sv<BR>
plot(X(i_sv,1),X(i_sv,2),'ro');<BR>
% ------------------------------------------------------------%%<BR>
% 求 b<BR>
epsilon = 1e-8; % 如果小于此值则认为是0<BR>
i_sv = find(a>epsilon); % 支持向量下标<BR>
tmp = (Y.*a)'*Calckernel(ker,X,X(i_sv,:)); % 行向量<BR>
b = 1./Y(i_sv)-tmp';<BR>
b = mean(b);<BR>
fprintf('Construct function Y = sign(tmp+b):')<BR>
tmp<BR>
b<BR>
% ------------------------------------------------------------%<BR>
% 测试输出<BR>
fprintf('Predicting ...\n');<BR>
[x1,x2] = meshgrid(-2:0.05:7,-2:0.05:7);<BR>
[rows,cols] = size(x1);<BR>
nt = rows*cols; % 测试样本数<BR>
Xt = [reshape(x1,nt,1),reshape(x2,nt,1)];<BR>
tic<BR>
% ------------------------------------------------------------%<BR>
% ------------------------------------------------------------%<BR>
ker = svm.ker;<BR>
X = svm.x;<BR>
Y = svm.y;<BR>
a = svm.a;<BR>
% ------------------------------------------------------------%<BR>
% ------------------------------------------------------------%<BR>
% 测试输出<BR>
nt = size(Xt,1); % 测试样本数<BR>
tmp = (Y.*a)'*Calckernel(ker,X,Xt);<BR>
Yd = sign(tmp+b)'; <BR>
% ------------------------------------------------------------%<BR>
% ------------------------------------------------------------%<BR>
fprintf('Predict time is t_sim:\n');<BR>
t_sim = toc<BR>
Yd = reshape(Yd,rows,cols);<BR>
contour(x1,x2,Yd,[0 0],'m'); % 得出分类面<BR>
hold off;<BR>