2020-11-04 13:59:14 +00:00
|
|
|
% Cluster 2D pointclouds with spectral clustering and compare with k-means
|
|
|
|
% USI, ICS, Lugano
|
|
|
|
% Numerical Computing
|
|
|
|
|
|
|
|
clear variables;
|
|
|
|
close all;
|
|
|
|
warning OFF;
|
|
|
|
|
|
|
|
addpath ../datasets
|
|
|
|
addpath ../datasets/Meshes
|
|
|
|
|
|
|
|
%% 1a) Get coordinate list from pointclouds
|
|
|
|
% Coords used in this script
|
2020-11-11 20:36:00 +00:00
|
|
|
[Pts_spirals,Pts_clusterin,Pts_corn,Pts_halfk,Pts_fullmoon,Pts_out] = getPoints();
|
|
|
|
|
2020-11-17 13:12:36 +00:00
|
|
|
TITLES = ["Two Spirals", "Cluster in cluster", "Corners", "Half crescent", "Full crescent", "Outlier"];
|
2020-11-11 20:36:00 +00:00
|
|
|
RUNS = {Pts_spirals, Pts_clusterin, Pts_corn, Pts_halfk, Pts_fullmoon, Pts_out};
|
|
|
|
KS = {2, 2, 4, 2, 2, 4};
|
2020-11-04 13:59:14 +00:00
|
|
|
|
2020-11-11 20:36:00 +00:00
|
|
|
for i = 1:6
|
|
|
|
% Specify the number of clusters
|
|
|
|
Pts = RUNS{i};
|
|
|
|
K = KS{i};
|
|
|
|
disp(TITLES(i));
|
|
|
|
|
|
|
|
figure;
|
|
|
|
scatter(Pts(:,1),Pts(:,2))
|
|
|
|
title(TITLES(i))
|
2020-11-04 13:59:14 +00:00
|
|
|
|
2020-11-11 20:36:00 +00:00
|
|
|
n = size(Pts, 1);
|
|
|
|
|
|
|
|
% Create Gaussian similarity function
|
2020-11-17 13:12:36 +00:00
|
|
|
[S] = similarityfunc(Pts(:,1:2), 2 * log(n));
|
2020-11-04 13:59:14 +00:00
|
|
|
|
2020-11-11 20:36:00 +00:00
|
|
|
%% 1b) Find the minimal spanning tree of the full graph. Use the information
|
|
|
|
% to determine a valid value for epsilon
|
|
|
|
H = minSpanTree(S);
|
|
|
|
epsilon = max(H(H > 0), [], 'all');
|
2020-11-04 13:59:14 +00:00
|
|
|
|
2020-11-11 20:36:00 +00:00
|
|
|
%% 1c) Create the epsilon similarity graph
|
|
|
|
[G] = epsilonSimGraph(epsilon,Pts);
|
2020-11-04 13:59:14 +00:00
|
|
|
|
2020-11-11 20:36:00 +00:00
|
|
|
%% 1d) Create the adjacency matrix for the epsilon case
|
|
|
|
W = S .* G;
|
|
|
|
figure;
|
|
|
|
gplotg(W,Pts(:,1:2))
|
|
|
|
title('Adjacency matrix')
|
2020-11-04 13:59:14 +00:00
|
|
|
|
2020-11-11 20:36:00 +00:00
|
|
|
%% 1e) Create the Laplacian matrix and implement spectral clustering
|
|
|
|
[L,Diag] = CreateLapl(W);
|
|
|
|
[V,~] = eigs(L, K, 'SA');
|
2020-11-04 13:59:14 +00:00
|
|
|
|
2020-11-11 20:36:00 +00:00
|
|
|
% Cluster rows of eigenvector matrix of L corresponding to K smallest
|
|
|
|
% eigennalues. Use kmeans for that.
|
|
|
|
[D_spec,x_spec] = kmeans_mod(V,K,n);
|
2020-11-04 13:59:14 +00:00
|
|
|
|
2020-11-11 20:36:00 +00:00
|
|
|
%% 1f) Run K-means on input data
|
|
|
|
[D_kmeans,x_kmeans] = kmeans_mod(Pts,K,n);
|
2020-11-04 13:59:14 +00:00
|
|
|
|
2020-11-11 20:36:00 +00:00
|
|
|
%% 1g) Visualize spectral and k-means clustering results
|
|
|
|
figure;
|
|
|
|
subplot(1,2,1)
|
|
|
|
gplotmap(W,Pts,x_spec)
|
|
|
|
title(strcat(TITLES(i), ': Spectral clusters'))
|
|
|
|
subplot(1,2,2)
|
|
|
|
gplotmap(W,Pts,x_kmeans)
|
|
|
|
title(strcat(TITLES(i), ': K-means clusters'))
|
2020-11-17 13:12:36 +00:00
|
|
|
matlab2tikz('showInfo', false, strcat('../../', TITLES{i}, '.tex'));
|
|
|
|
|
2020-11-11 20:36:00 +00:00
|
|
|
end
|
|
|
|
|