-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdemo_KernelFunction.m
53 lines (42 loc) · 1.36 KB
/
demo_KernelFunction.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
%{
Demonstration of computing the kernel function matrix.
linear : k(x,y) = x'*y
polynomial : k(x,y) = (γ*x'*y+c)^d
gaussian : k(x,y) = exp(-γ*||x-y||^2)
sigmoid : k(x,y) = tanh(γ*x'*y+c)
laplacian : k(x,y) = exp(-γ*||x-y||)
%}
clc
close all
addpath(genpath(pwd))
% data
X = rand(30, 2);
Y = rand(20, 2);
% kernel setting
kernelObj = cell(5, 1);
kernelObj{1, 1} = BaseKernel('type', 'gaussian', ...
'gamma', 1);
kernelObj{2, 1} = BaseKernel('type', 'polynomial', ...
'degree', 2, ...
'gamma', 1, ...
'offset', 0);
kernelObj{3, 1} = BaseKernel('type', 'linear');
kernelObj{4, 1} = BaseKernel('type', 'sigmoid', ...
'gamma', 1, ...
'offset', 0);
kernelObj{5, 1} = BaseKernel('type', 'laplacian', ...
'gamma', 1);
% compute the kernel function matrix
kernelMatrix = cell(5, 1);
figure
set(gcf, 'position', [100, 100, 600, 400])
titleName = {'gaussian', 'polynomial', 'linear',...
'sigmoid', 'laplacian'};
for i = 1:5
kernelMatrix{i, 1} = kernelObj{i, 1}.computeMatrix(X, Y);
subplot(3, 2, i)
mesh(kernelMatrix{i, 1})
colorbar
colormap('parula')
title(titleName{1, i})
end