-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdemo.m
More file actions
43 lines (31 loc) · 1.28 KB
/
demo.m
File metadata and controls
43 lines (31 loc) · 1.28 KB
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
%
% demo1: gaussian kernel pca using KernelPca.m
%
% load sample data and plot------------------------------------------------
load('data.mat')
figure
hold on
gscatter(X(:, 1), X(:, 2), Y)
plot(Xtest(:, 1), Xtest(:, 2), 'LineStyle', 'none', 'Marker', '>')
legend(["X (class1)", "X (class2)", "X (class3)", "X (class4)", "Xtest"])
title('original data')
% gaussian kernel pca------------------------------------------------------
% fit pca model and get the coefficient for projection with dataset 'X'
% setting 'AutoScale' true is reccomended (default:false)
kpca = KernelPca(X, 'gaussian', 'gamma', 2.5, 'AutoScale', true);
% set the subspace dimention number M of projected data
% (M <= D, where D is the dimention of the original data)
M = 2;
% project the train data 'X' into the subspace by using the coefficient
projected_X = project(kpca, X, M);
% project the test data 'Xtest' as well
projected_Xtest = project(kpca, Xtest, M);
% plot
figure
hold on
gscatter(projected_X(:, 1), projected_X(:, 2), Y)
plot(projected_Xtest(:, 1), projected_Xtest(:, 2), 'LineStyle', 'none', 'Marker', '>')
title('pca with gaussian kernel')
xlabel('principal dim')
ylabel('second dim')
legend(["projected X (class1)", "projected X (class2)", "projected X (class3)", "projected X (class4)", "projected Xtest"])