mp4: done MATLAB

This commit is contained in:
Claudio Maggioni 2020-11-11 21:36:00 +01:00
parent 34b290c569
commit 4d9b7f859d
20 changed files with 1436993 additions and 98 deletions

27493
mp4/3elt_adj.tex Normal file

File diff suppressed because it is too large Load diff

327369
mp4/3elt_clu.tex Normal file

File diff suppressed because it is too large Load diff

48
mp4/3elt_hist.tex Normal file
View file

@ -0,0 +1,48 @@
% This file was created by matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=2.603in,
height=4.754in,
at={(1.011in,0.642in)},
scale only axis,
xmin=0.3,
xmax=4.7,
ymin=0,
ymax=1800,
axis background/.style={fill=white}
]
\addplot[ybar interval, fill=mycolor1, fill opacity=0.6, draw=black, area legend] table[row sep=crcr] {%
x y\\
0.5 1088\\
1.5 965\\
2.5 869\\
3.5 1798\\
4.5 1798\\
};
\end{axis}
\begin{axis}[%
width=2.603in,
height=4.754in,
at={(4.436in,0.642in)},
scale only axis,
xmin=0.3,
xmax=4.7,
ymin=0,
ymax=3500,
axis background/.style={fill=white}
]
\addplot[ybar interval, fill=mycolor1, fill opacity=0.6, draw=black, area legend] table[row sep=crcr] {%
x y\\
0.5 31\\
1.5 1560\\
2.5 3098\\
3.5 31\\
4.5 31\\
};
\end{axis}
\end{tikzpicture}%

Binary file not shown.

View file

@ -1,13 +1,22 @@
\documentclass[unicode,11pt,a4paper,oneside,numbers=endperiod,openany]{scrartcl}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{amsmath}
\input{assignment.sty}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\usetikzlibrary{arrows.meta}
\usepgfplotslibrary{patchplots}
\usepackage{grffile}
\usepackage{amsmath}
\usepackage{subcaption}
\usepgfplotslibrary{external}
\tikzexternalize
\begin{document}
\setassignment
\setduedate{Wednesday, 18 November 2020, 11:55 PM}
@ -26,5 +35,24 @@
\end{enumerate}
\begin{figure}
\centering\input{airfoil1_clu.tex}
\caption{Graphs for \textit{Airfoil1}}
\end{figure}
\begin{figure}
\centering\input{barth_clu.tex}
\caption{Graphs for \textit{Barth}}
\end{figure}
\begin{figure}
\centering\input{grid2_clu.tex}
\caption{Graphs for \textit{Grid2}}
\end{figure}
\begin{figure}
\centering\input{3elt_clu.tex}
\caption{Graphs for \textit{3elt}}
\end{figure}
\begin{figure}
\end{figure}
\end{document}

View file

@ -2,64 +2,74 @@
% USI, ICS, Lugano
% Numerical Computing
clear all;close all;
clear variables;
close all;
warning OFF;
addpath ../datasets
addpath ../datasets/Meshes
load airfoil1.mat
% load barth.mat
% load grid2.mat
% load 3elt.mat
MATS = {'airfoil1', 'barth', 'grid2', '3elt'};
% Specify the number of clusters
K = 4;
% Read graph
W = Problem.A;
Pts = Problem.aux.coord;
n = size(Pts,1);
% dummy var
dummy_map = ones(n,1);
for i = 1:length(MATS)
load(strcat(MATS{i}, '.mat'));
figure;
spy(W)
title('Adjacency matrix')
%% 2a) Create the Laplacian matrix and plot the graph using the 2nd and 3rd eigenvectors
% \----------------------------/
% Your implementation
% \----------------------------/
% Specify the number of clusters
K = 4;
% Read graph
W = Problem.A;
Pts = Problem.aux.coord;
n = size(Pts,1);
% dummy var
dummy_map = ones(n,1);
% Eigen-decomposition
% \----------------------------/
% Your implementation
% \----------------------------/
figure;
spy(W)
xlabel(strcat(MATS{i}, ': adjacency matrix'))
%matlab2tikz('showInfo', false, strcat('../../', MATS{i}, '_adj.tex'))
%% 2a) Create the Laplacian matrix and plot the graph using the 2nd and 3rd eigenvectors
[L,~] = CreateLapl(W);
[V,~] = eigs(L, 4, 'smallestabs');
% Plot and compare
figure;
subplot(1,2,1);
gplot(W,Pts)
xlabel('Nodal coordinates')
subplot(1,2,2);
gplot(W,Pts)
xlabel('TODO: Plot using Eigenvector coordinates')
% Plot and compare
figure;
subplot(2,2,1);
gplot(W,Pts)
title(strcat(MATS{i}, ': nodal coordinates'))
subplot(2,2,2);
gplot(W,V(:, 2:3))
title(strcat(MATS{i}, ': eigenvector coordinates'))
%% 2b) Cluster each graph in K = 4 clusters with the spectral and the
% k-means method, and compare yourresults visually for each case.
%% 2b) Cluster each graph in K = 4 clusters with the spectral and the
% k-means method, and compare yourresults visually for each case.
% \----------------------------/
% Your implementation
% \----------------------------/
[D_spec,x_spec] = kmeans_mod(V, K, n);
[D_kmeans,x_kmeans] = kmeans_mod(Pts, K, n);
% Compare and visualize
subplot(2,2,3);
gplotmap(W,Pts,x_spec)
title(strcat(MATS{i}, ': spectral clusters'))
subplot(2,2,4);
gplotmap(W,Pts,x_kmeans)
title(strcat(MATS{i}, ': k-means clusters'))
matlab2tikz('showInfo', false, strcat('../../', MATS{i}, '_clu.tex'))
% Compare and visualize
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')
%% 2c) Calculate the number of nodes per cluster
[Spec_nodes,Kmeans_nodes] = USI_ClusterMetrics(K,dummy_map,dummy_map);
cx = sum(x_spec == 1:4);
ck = sum(x_kmeans == 1:4);
fprintf('%10s spectral: %4d %4d %4d %4d k-means: %4d %4d %4d %4d\n', ...
MATS{i}, cx(1), cx(2), cx(3), cx(4), ck(1), ck(2), ck(3), ck(4));
figure;
subplot(1,2,1);
title(strcat(MATS{i}, ': spectral histogram'));
histogram(x_spec, [0:4] + 0.5);
subplot(1,2,2);
title(strcat(MATS{i}, ': k-means histogram'));
histogram(x_kmeans, [0:4] + 0.5);
matlab2tikz('showInfo', false, strcat('../../', MATS{i}, '_hist.tex'));
%% 2c) Calculate the number of nodes per cluster
[Spec_nodes,Kmeans_nodes] = ClusterMetrics(K,dummy_map,dummy_map);
end

View file

@ -9,56 +9,61 @@ 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')
[Pts_spirals,Pts_clusterin,Pts_corn,Pts_halfk,Pts_fullmoon,Pts_out] = getPoints();
n = size(Pts, 1);
TITLES = ["Two Spirals", "Cluster in", "Corn", "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};
% Create Gaussian similarity function
[S] = similarityfunc(Pts(:,1:2));
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))
%% 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');
n = size(Pts, 1);
% Create Gaussian similarity function
[S] = similarityfunc(Pts(:,1:2), 10 * log(n));
%% 1c) Create the epsilon similarity graph
[G] = epsilonSimGraph(epsilon,Pts, S);
%% 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');
%% 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);
%% 1c) Create the epsilon similarity graph
[G] = epsilonSimGraph(epsilon,Pts);
% \----------------------------/
% Your implementation
% \----------------------------/
%% 1d) Create the adjacency matrix for the epsilon case
W = S .* G;
figure;
gplotg(W,Pts(:,1:2))
title('Adjacency matrix')
% 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);
%% 1e) Create the Laplacian matrix and implement spectral clustering
[L,Diag] = CreateLapl(W);
[V,~] = eigs(L, K, 'SA');
%% 1f) Run K-means on input data
% \----------------------------/
% Your implementation
% \----------------------------/
[D_kmeans,x_kmeans] = kmeans_mod(Pts,K,n);
% 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);
%% 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')
%% 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'))
end

View file

@ -1,4 +1,4 @@
function [G] = epsilonSimGraph(epsilon, Pts, S)
function [G] = epsilonSimGraph(epsilon, Pts)
% Construct an epsilon similarity graph
% Input
% epsilon: size of neighborhood (calculate from Prim's Algorithm)
@ -14,6 +14,15 @@ fprintf('----------------------------\n');
fprintf('epsilon similarity graph\n');
fprintf('----------------------------\n');
G = S <= epsilon;
n = size(Pts, 1);
G = zeros(n, n);
for i = 1:n
for j = 1:n
dist = norm((Pts(i, :) - Pts(j, :)), 2);
if dist < epsilon
G(i, j) = 1;
G(j, i) = 1;
end
end
end
end

View file

@ -16,7 +16,7 @@ D = c;
x = zeros(size(Y,1),1);
D_old = inf*ones(size(D));
count = 1;
while norm(D - D_old) > 0.00001 & count < 500
while norm(D - D_old) > 0.00001 && count < 500
D_old = D;
% Assign points to clusters
for i = 1:n

24627
mp4/airfoil1_adj.tex Normal file

File diff suppressed because it is too large Load diff

293007
mp4/airfoil1_clu.tex Normal file

File diff suppressed because it is too large Load diff

48
mp4/airfoil1_hist.tex Normal file
View file

@ -0,0 +1,48 @@
% This file was created by matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=2.603in,
height=4.754in,
at={(1.011in,0.642in)},
scale only axis,
xmin=0.3,
xmax=4.7,
ymin=0,
ymax=1200,
axis background/.style={fill=white}
]
\addplot[ybar interval, fill=mycolor1, fill opacity=0.6, draw=black, area legend] table[row sep=crcr] {%
x y\\
0.5 971\\
1.5 1050\\
2.5 1150\\
3.5 1082\\
4.5 1082\\
};
\end{axis}
\begin{axis}[%
width=2.603in,
height=4.754in,
at={(4.436in,0.642in)},
scale only axis,
xmin=0.3,
xmax=4.7,
ymin=0,
ymax=2000,
axis background/.style={fill=white}
]
\addplot[ybar interval, fill=mycolor1, fill opacity=0.6, draw=black, area legend] table[row sep=crcr] {%
x y\\
0.5 344\\
1.5 739\\
2.5 1869\\
3.5 1301\\
4.5 1301\\
};
\end{axis}
\end{tikzpicture}%

46256
mp4/barth_adj.tex Normal file

File diff suppressed because it is too large Load diff

551415
mp4/barth_clu.tex Normal file

File diff suppressed because it is too large Load diff

48
mp4/barth_hist.tex Normal file
View file

@ -0,0 +1,48 @@
% This file was created by matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=2.603in,
height=4.754in,
at={(1.011in,0.642in)},
scale only axis,
xmin=0.3,
xmax=4.7,
ymin=0,
ymax=2500,
axis background/.style={fill=white}
]
\addplot[ybar interval, fill=mycolor1, fill opacity=0.6, draw=black, area legend] table[row sep=crcr] {%
x y\\
0.5 1588\\
1.5 1490\\
2.5 2206\\
3.5 1407\\
4.5 1407\\
};
\end{axis}
\begin{axis}[%
width=2.603in,
height=4.754in,
at={(4.436in,0.642in)},
scale only axis,
xmin=0.3,
xmax=4.7,
ymin=0,
ymax=4000,
axis background/.style={fill=white}
]
\addplot[ybar interval, fill=mycolor1, fill opacity=0.6, draw=black, area legend] table[row sep=crcr] {%
x y\\
0.5 71\\
1.5 3617\\
2.5 71\\
3.5 2932\\
4.5 2932\\
};
\end{axis}
\end{tikzpicture}%

12901
mp4/grid2_adj.tex Normal file

File diff suppressed because it is too large Load diff

153583
mp4/grid2_clu.tex Normal file

File diff suppressed because it is too large Load diff

48
mp4/grid2_hist.tex Normal file
View file

@ -0,0 +1,48 @@
% This file was created by matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=2.603in,
height=4.754in,
at={(1.011in,0.642in)},
scale only axis,
xmin=0.3,
xmax=4.7,
ymin=0,
ymax=1400,
axis background/.style={fill=white}
]
\addplot[ybar interval, fill=mycolor1, fill opacity=0.6, draw=black, area legend] table[row sep=crcr] {%
x y\\
0.5 785\\
1.5 1305\\
2.5 827\\
3.5 379\\
4.5 379\\
};
\end{axis}
\begin{axis}[%
width=2.603in,
height=4.754in,
at={(4.436in,0.642in)},
scale only axis,
xmin=0.3,
xmax=4.7,
ymin=0,
ymax=1400,
axis background/.style={fill=white}
]
\addplot[ybar interval, fill=mycolor1, fill opacity=0.6, draw=black, area legend] table[row sep=crcr] {%
x y\\
0.5 1271\\
1.5 1183\\
2.5 604\\
3.5 238\\
4.5 238\\
};
\end{axis}
\end{tikzpicture}%

BIN
mp4/usi_inf-.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 KiB