-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_classificator.m
65 lines (62 loc) · 1.99 KB
/
get_classificator.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
function [classificator] = get_classificator(kernel_type, x_opt, ...
x_train, y_train, x_test, gamma, coef0, degree)
[n, m] = size(x_train);
n_test = length(x_test);
if strcmp(kernel_type, 'linear')
w = zeros(1, m);
for i = 1:n
w = w + x_opt(i)*y_train(i)*x_train(i, :);
end
for i = 1:n
if x_opt(i) ~= 0
d = w*x_train(i, :)' - 1/y_train(i);
break;
end
end
classificator = w*x_test' - d;
elseif strcmp(kernel_type, 'polynomial')
for i = 1:n
if x_opt(i) ~= 0
s = 0;
for j = 1:n
ki = K_poly(x_train(j, :), x_train(i, :), coef0, ...
gamma, degree);
s = s + x_opt(j)*y_train(j)*ki;
end
d = s - 1/y_train(i);
break;
end
end
s1 = zeros(1, n_test);
for i = 1:n
ki = K_poly(x_train(i, :), x_test, coef0, gamma, degree);
s1 = s1 + x_opt(i)*y_train(i)* ki;
end
classificator = s1 - d;
elseif strcmp(kernel_type, 'radial_basis')
for i = 1:n
if x_opt(i) ~= 0
s = 0;
for j = 1:n
ki = K_radial(x_train(j, :), x_train(i, :), gamma);
s = s + x_opt(j)*y_train(j)*ki;
end
d = s - 1/y_train(i);
break;
end
end
s1 = zeros(1, n_test);
for i = 1:n
ki = K_radial(repmat(x_train(i, :), n_test, 1), x_test, gamma);
s1 = s1 + x_opt(i)*y_train(i)* ki';
end
classificator = s1 - d;
end
end
function [kp] = K_poly(x, x1, coef0, gamma, degree)
kp = (coef0 + gamma * (x*x1')).^degree;
end
function [kr] = K_radial(x, x1, gamma)
aux = x-x1;
kr = exp(-gamma*sqrt(sum(aux.^2,2)));
end