This repository has been archived on 2021-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
NC/mp4/Project_4_Maggioni_Claudio/src/ClusterPoints.m

64 lines
1.6 KiB
Mathematica
Raw Normal View History

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
% Specify the number of clusters
K = 2;
%% 1a) Get coordinate list from pointclouds
% Coords used in this script
Pts = getPoints();
figure;
scatter(Pts(:,1),Pts(:,2))
title('Two Spirals')
n = size(Pts, 1);
% Create Gaussian similarity function
[S] = similarityfunc(Pts(:,1:2));
%% 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, S);
%% 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);
% \----------------------------/
% Your implementation
% \----------------------------/
% Cluster rows of eigenvector matrix of L corresponding to K smallest
% eigennalues. Use kmeans for that.
[D_spec,x_spec] = kmeans_mod(Pts,K,n);
%% 1f) Run K-means on input data
% \----------------------------/
% Your implementation
% \----------------------------/
[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,dummy_map)
title('TODO: Plot the spectral clusters')
subplot(1,2,2)
gplotmap(W,Pts,dummy_map)
title('TODO: Plot the K-means clusters')