forked from gchers/SVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVM.m
46 lines (39 loc) · 1.33 KB
/
SVM.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
function [pc, numVectors] = SVM(XTrain, YTrain, XTest, YTest, degree, C, threshold)
% Support Vector Machine
% This function trains a SVM on the training set. The SVM is then used to predict
% the labels of the test set. Also, the count of the alpha that passed the
% condition of threshold <= alpha_i <= C-threshold is returned.
%
% Copyright (C) 2013 GPLv3
% by joker__ <g.chers ::at:: gmail.com>
N = size(XTrain,1);
% Defining the Kernel Function
% This Kernel is the Vapnik's polynomial,
% it can although be changed, using the
% following anonymous function
kern = @(X,d) (1 + X).^d;
% Defining variables for quadprog
Dy = diag(YTrain);
K = kern(XTrain * XTrain', degree); % Kernel application to the dot prod of all the vectors
H = (Dy * K * Dy) / 2;
f = -ones(N,1);
A = [];
c = [];
Aeq = YTrain';
ceq = 0;
cl = zeros(N,1);
cu = C * ones(N,1);
alpha = quadprog(H,f,A,c,Aeq,ceq,cl,cu);
% indexes when alpha is subject to the constraints:
idx = find(alpha > threshold & alpha < C - threshold);
numVectors = length(idx);
d = 0;
for i=1:length(idx)
d = d + YTrain(i) - ((alpha.* YTrain)' * K(:,i));
end
d = d / length(idx); % Take the mean of the computed d
YPred = zeros(size(YTest));
for i=1:length(YTest)
YPred(i) = sign(((alpha.* YTrain)' * kern(XTrain*XTest(i,:)',degree) + d) / 2);
end
pc = sum(YPred == YTest) / length(YTest);