-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysvkernel2.m
72 lines (63 loc) · 2.33 KB
/
mysvkernel2.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
function H = mysvkernel(ker,x1,x2,sigmay)
% ---------------------------------------------------------------------------------------------------
% SVKERNEL kernel for Support Vector Methods
%
% Usage: k = svkernel(ker,u,v)
%
% Parameters: ker - kernel type
% u,v - kernel arguments
%
% Values for ker: 'linear' -
% 'poly' - p1 is degree of polynomial
% 'rbf' - p1 is width of rbfs (sigma)
% 'sigmoid' - p1 is scale, p2 is offset
% 'spline' -
% 'bspline' - p1 is degree of bspline
% 'fourier' - p1 is degree
% 'erfb' - p1 is width of rbfs (sigma)
% 'anova' - p1 is max order of terms
%
% Author: Steve Gunn (srg@ecs.soton.ac.uk)
% ---------------------------------------------------------------------------------------------------
% ---------------------------------------------------------------------------------------------------
% Version for fast matrix form of RBF kernel
%
% Code modified by José L. Rojo-Álvarez & Gustavo Camps-Valls
% jlrojo@tsc.uc3m.es, gcamps@uv.es
% 2004(c)
%
% ---------------------------------------------------------------------------------------------------
if strcmp(ker,'rbf')
if(size(x1,2)==1)
N1=size(x1,1);
N2=size(x2,1);
H = zeros(N1,N2);
for i=1:N1
H(i,:) = (exp(-(1/2/(sigmay^2))*(x2-ones(N2,1)*x1(i,:))'.*(x2-ones(N2,1)*x1(i,:))'));
end
else
N1=size(x1,1);
N2=size(x2,1);
H = zeros(N1,N2);
for i=1:N1
H(i,:) = exp(-(1/2/(sigmay^2))*sum((x2-ones(N2,1)*x1(i,:))'.*(x2-ones(N2,1)*x1(i,:))'));
end
end
elseif strcmp(ker,'semirbf')
global p
N1=size(x1,1);
N2=size(x2,1);
% Componentes no parametricas
xm1 = x1(:,1:p);
xm2 = x2(:,1:p);
M = zeros(N1,N2);
for i=1:N1
M(i,:) = exp(-(1/2/(sigmay^2))*...
sum((xm2-ones(N2,1)*xm1(i,:))'.*(xm2-ones(N2,1)*xm1(i,:))'));
end
% Componentes parametricas
xc1 = x1(:,p+1:end);
xc2 = x2(:,p+1:end);
C = xc1 * xc2';
H = M + C;
end