-
Notifications
You must be signed in to change notification settings - Fork 42
/
svmeval.m
executable file
·86 lines (75 loc) · 2.49 KB
/
svmeval.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
% SVMEVAL - Evaluates a support vector machine at the given data points.
%
% Syntax: [f,K] = svmeval(X,a,b,ind,X_mer,y_mer,type,scale)
% (evaluates the given SVM at the data points contained in X)
%
% [f,K] = svmeval(X)
% (evaluates the SVM in memory at the data points contained in X)
%
% f: SVM output for the evaluation vectors
% K: kernel matrix containing dot products in feature space between
% the margin and error vectors (rows of K) and the column vectors in X
% (columns of K)
% X: matrix of evaluation vectors stored columnwise
% a: alpha coefficients
% b: bias
% ind: cell array containing indices of margin, error and reserve vectors
% ind{1}: indices of margin vectors
% ind{2}: indices of error vectors
% ind{3}: indices of reserve vectors
% X_mer: matrix of margin, error and reserve vectors stored columnwise
% y_mer: column vector of class labels (-1/+1) for margin, error and reserve vectors
% type: kernel type
% 1: linear kernel K(x,y) = x'*y
% 2-4: polynomial kernel K(x,y) = (scale*x'*y + 1)^type
% 5: Gaussian kernel with variance 1/(2*scale)
% scale: kernel scale
%
% Version 3.22e -- Comments to [email protected]
%
function [f,K] = svmeval(X_eval,varargin)
% flags for example state
MARGIN = 1;
ERROR = 2;
RESERVE = 3;
UNLEARNED = 4;
if (nargin == 8)
% define arguments
a = varargin{1};
b = varargin{2};
ind = varargin{3};
X = varargin{4};
y = varargin{5};
type = varargin{6};
scale = varargin{7};
else
% define global variables
global a;
global b;
global ind;
global scale;
global type;
global X;
global y;
end;
% evaluate the SVM
% find all of the nonzero coefficients
% (note: when performing kernel perturbation, ind{MARGIN} and ind{ERROR}
% do not necessarily identify all of the nonzero coefficients)
indu = find(a(ind{UNLEARNED}) > 0);
indu = ind{UNLEARNED}(indu);
indr = find(a(ind{RESERVE}) > 0);
indr = ind{RESERVE}(indr);
indme = [ind{MARGIN} ind{ERROR}];
K = [];
f = b;
if (length(indme) > 0)
K = kernel(X(:,indme),X_eval,type,scale);
f = f + K'*(y(indme).*a(indme));
end;
if (length(indu) > 0)
f = f + kernel(X(:,indu),X_eval,type,scale)'*(y(indu).*a(indu));
end;
if (length(indr) > 0)
f = f + kernel(X(:,indr),X_eval,type,scale)'*(y(indr).*a(indr));
end;