www.pudn.com > easy_svm.zip > svmSim.m, change:2009-01-04,size:1679b


function Yd = svmSim(svm,Xt) 
% 输入参数: 
% svm  支持向量机(结构体变量) 
% the following fields: 
%   type - 支持向量机类型 {'svc_c', 'svr_epsilon' } 
%   ker - 核参数 
%       type   - linear :  k(x,y) = x'*y 
%                poly   :  k(x,y) = (x'*y+c)^d 
%                gauss  :  k(x,y) = exp(-0.5*(norm(x-y)/s)^2) 
%                tanh   :  k(x,y) = tanh(g*x'*y+c) 
%       degree - Degree d of polynomial kernel (positive scalar). 
%       offset - Offset c of polynomial and tanh kernel (scalar, negative for tanh). 
%       width  - Width s of Gauss kernel (positive scalar). 
%       gamma  - Slope g of the tanh kernel (positive scalar). 
%   x - 训练样本 
%   y - 训练目标; 
%   a - 拉格朗日乘子 
% Xt  测试样本,n×d的矩阵,n为样本个数,d为样本维数 
% 输出参数: 
% Yd  测试输出,n×1的矩阵,n为样本个数,值为+1或-1 
% ------------------------------------------------------------% 
type = svm.type; 
ker = svm.ker; 
X = svm.x; 
Y = svm.y; 
a = svm.a; 
% ------------------------------------------------------------% 
% 测试输出 
epsilon=1e-6;                  % 如果小于此值则认为是0 
i_sv = find(abs(a)>epsilon);          % 支持向量下标 
switch type 
    case 'svc_c', 
        tmp = ((a(i_sv,:).*Y(i_sv,:))'*kernel(ker,X(i_sv,:),X(i_sv,:)))'; % 行向量 
        b = 1./Y(i_sv)-tmp; 
        b = mean(b); 
        tmp=((a.*Y)'*kernel(ker,X,Xt))'; 
        Yd = sign(tmp+b); 
    case 'svr_epsilon', 
        tmp = (a(i_sv)'*kernel(ker,X(i_sv,:),X(i_sv,:)))';   % 列向量 
        b = Y(i_sv)-tmp;  
        if length(b)~=0 
           b = mean(b);  
        else 
            b=0; 
        end 
        tmp=(a'*kernel(ker,X,Xt))'; 
        Yd=tmp+b;  
    otherwise, 
end