-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.m
132 lines (97 loc) · 2.91 KB
/
demo.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
%% create a clean environment
clc, clear all, warning off
%% set path and add the main code repo to path
repo_path = pwd;
addpath(genpath(repo_path))
clear repo_path
%% generate the noise-free data first (and keep this base X0 fixed)
P = 20;
q = 5;
Nk = 100;
K = 3;
N = Nk*K;
rng(1)
% if we want to generate data from linear subspaces
[X0, Truth] = GenSubDat(P, q, Nk, K, 0, 'linear');
% if we want to generate data from affine subspaces
%[X0, Truth] = GenSubDat(P, q, Nk, K, 0, 'affine');
% add some noise to the data
noi = 0.1;
X = X0 + normrnd(0, noi, size(X0));
%% plot the data if the data are 2D
for k = 1:K
scatter(X(Truth==k,1), X(Truth==k,2))
% grid off % comment this out if grid is not wanted
hold on
end
hold off
%% plot the data if the data are 3D
for k = 1:K
scatter3(X(Truth==k,1), X(Truth==k,2), X(Truth==k,3), 25, 'filled')
% grid off % comment this out if grid is not wanted
hold on
end
hold off
%% parameter settings
knn = 10;
rho = 0.1;
weight = 1; % whether to use the weight matrix or not
normalize = 1; % whether to normalise the data to have unit length
stretch = 1; % whether to stretch the points to reach the unit sphere
ss = 10;
MaxIter = 100;
thr = 1e-5;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Linear Subspace %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% WSSR_PGD_cos (use absolute cosine similarity)
tic;
[W, obj, obj_mat] = WSSR_PGD_cos(X, knn, rho, normalize, ss, MaxIter, stretch, thr);
time = toc
A = (abs(W) + abs(W'))./2;
grps = SpectralClustering(A, K);
cluster_performance(grps, Truth)
%% WSSR_QP_cos (use absolute cosine similarity)
tic;
[W, obj] = WSSR_QP_cos(X, knn, rho, normalize, stretch, weight);
time = toc
A = (abs(W) + abs(W'))./2;
grps = SpectralClustering(A, K);
cluster_performance(grps, Truth)
%% WSSR_le_cos (use absolute cosine similarity)
tic;
W = WSSR_le_cos(X, knn, rho, normalize, stretch, weight);
time = toc
A = (abs(W) + abs(W'))./2;
grps = SpectralClustering(A, K);
cluster_performance(grps, Truth)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Affine Subspace %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% WSSR_PGD_euclid (use Euclidean distance)
% The Euclidean version of PGD requires a much larger step size than the
% absolute cosine similarity version.
tic;
W = WSSR_PGD_euclid(X, knn, rho, normalize, ss, MaxIter, thr);
time = toc
A = (abs(W) + abs(W'))./2;
grps = SpectralClustering(A, K);
cluster_performance(grps, Truth)
%% WSSR_QP_euclid (use Euclidean distance)
tic;
W = WSSR_QP_euclid(X, knn, rho, normalize, weight);
time = toc
A = (abs(W) + abs(W'))./2;
grps = SpectralClustering(A, K);
cluster_performance(grps, Truth)
%% WSSR_le_euclid (use Euclidean distance)
tic;
W = WSSR_le_euclid(X, knn, rho, normalize, weight);
time = toc
A = (abs(W) + abs(W'))./2;
grps = SpectralClustering(A, K);
cluster_performance(grps, Truth)
%% visualise the similarity matrix
imshow(A*100)
%% visualise the clustering result
hold on
for k = 1:K
scatter(X(grps == k,1), X(grps == k,2))
end
hold off