资源简介
提供了SVM算法用MATLAB来实现的源程序。
代码片段和文件信息
function [test_targets a_star] = SVM(train_patterns train_targets test_patterns params)
% Classify using (a very simple implementation of) the support vector machine algorithm
%
% Inputs:
% train_patterns - Train patterns
% train_targets - Train targets
% test_patterns - Test patterns
% params - [kernel kernel parameter solver type Slack]
% Kernel can be one of: Gauss RBF (Same as Gauss) Poly Sigmoid or Linear
% The kernel parameters are:
% RBF kernel - Gaussian width (One parameter)
% Poly kernel - Polynomial degree
% Sigmoid - The slope and constant of the sigmoid (in the format [1 2] with no separating commas)
% Linear - None needed
% Solver type can be one of: Perceptron Quadprog Lagrangian
%
% Outputs
% test_targets - Predicted targets
% a - SVM coeficients
%
% Note: The number of support vectors found will usually be larger than is actually
% needed because the two first solvers are approximate.
[Dim Nf] = size(train_patterns);
Dim = Dim + 1;
train_patterns(Dim:) = ones(1Nf);
test_patterns(Dim:) = ones(1 size(test_patterns2));
if (length(unique(train_targets)) == 2)
z = 2*(train_targets>0) - 1;
else
z = train_targets;
end
%Get kernel parameters
[kernel ker_param solver slack] = process_params(params);
%Transform the input patterns
y = zeros(Nf);
switch kernel
case {‘Gauss‘‘RBF‘}
for i = 1:Nf
y(:i) = exp(-sum((train_patterns-train_patterns(:i)*ones(1Nf)).^2)‘/(2*ker_param^2));
end
case {‘Poly‘ ‘Linear‘}
if strcmp(kernel ‘Linear‘)
ker_param = 1;
end
for i = 1:Nf
y(:i) = (train_patterns‘*train_patterns(:i) + 1).^ker_param;
end
case ‘Sigmoid‘
ker_param = str2num(ker_param);
if (length(ker_param) ~= 2)
error(‘This kernel needs two parameters to operate!‘)
end
for i = 1:Nf
y(:i) = tanh(train_patterns‘*train_patterns(:i)*ker_param(1)+ker_param(2));
end
otherwise
error(‘Unknown kernel. Can be Gauss Linear Poly or Sigmoid.‘)
end
%Find the SVM coefficients
switch solver
case ‘Quadprog‘
%Quadratic programming
alpha_star = quadprog(diag(z)*y‘*y*diag(z) -ones(1 Nf) zeros(1 Nf) 1 z 0 zeros(1 Nf) slack*ones(1 Nf))‘;
a_star = (alpha_star.*z)*y‘;
%Find the bias
sv_for_bias = find((alpha_star > 0.001*slack) & (alpha_star < slack - 0.001*slack));
if isempty(sv_for_bias)
bias = 0;
else
B = z(sv_for_bias) - a_star(sv_for_bias);
bias = mean(B);
end
sv = find(alpha_star > 0.001*slack);
case ‘Perceptron‘
max_iter = 1e5;
iter = 0;
rate = 0.0
- 上一篇:mssim图像平均相似性度量
- 下一篇:掌纹识别代码matlab
评论
共有 条评论