[matlab]支持向量机(SVM)用于分类的算法实现
function [D, a_star] = SVM(train_features, train_targets, params, region)
% Classify using (a very simple implementation of) the support vector machine algorithm
%
% Inputs:
% features- Train features
% targets - Train targets
% 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
% region - Decision region vector: [-x x -y y number_of_points]
%
% Outputs
% D - Decision sufrace
% a - SVM coeficients
%
% Note: The number of support vectors found will usually be larger than is actually
% needed because both solvers are approximate.
[Dim, Nf] = size(train_features);
Dim = Dim + 1;
train_features(Dim,:) = ones(1,Nf);
z = 2*(train_targets>0) - 1;
%Get kernel parameters
[kernel, ker_param, solver, slack] = process_params(params);
%Transform the input features
y = zeros(Nf);
switch kernel,
case {'Gauss','RBF'},
for i = 1:Nf,
y(:,i) = exp(-sum((train_features-train_features(:,i)*ones(1,Nf)).^2)'/(2*ker_param^2));
end
case {'Poly', 'Linear'}
if strcmp(kernel, 'Linear')