% 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 [Pts_spirals,Pts_clusterin,Pts_corn,Pts_halfk,Pts_fullmoon,Pts_out] = getPoints(); TITLES = ["Two Spirals", "Cluster in cluster", "Corners", "Half crescent", "Full crescent", "Outlier"]; RUNS = {Pts_spirals, Pts_clusterin, Pts_corn, Pts_halfk, Pts_fullmoon, Pts_out}; KS = {2, 2, 4, 2, 2, 4}; 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)) n = size(Pts, 1); % Create Gaussian similarity function [S] = similarityfunc(Pts(:,1:2), 2 * log(n)); %% 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'); %% 1c) Create the epsilon similarity graph [G] = epsilonSimGraph(epsilon,Pts); %% 1d) Create the adjacency matrix for the epsilon case W = S .* G; figure; gplotg(W,Pts(:,1:2)) title('Adjacency matrix') %% 1e) Create the Laplacian matrix and implement spectral clustering [L,Diag] = CreateLapl(W); [V,~] = eigs(L, K, 'SA'); % 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); %% 1f) Run K-means on input data [D_kmeans,x_kmeans] = kmeans_mod(Pts,K,n); %% 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')) matlab2tikz('showInfo', false, strcat('../../', TITLES{i}, '.tex')); end