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} \documentclass[unicode,11pt,a4paper,oneside,numbers=endperiod,openany]{scrartcl}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{amsmath}
\input{assignment.sty} \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} \begin{document}
\setassignment \setassignment
\setduedate{Wednesday, 18 November 2020, 11:55 PM} \setduedate{Wednesday, 18 November 2020, 11:55 PM}
@ -26,5 +35,24 @@
\end{enumerate} \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} \end{document}

View file

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

View file

@ -9,20 +9,28 @@ warning OFF;
addpath ../datasets addpath ../datasets
addpath ../datasets/Meshes addpath ../datasets/Meshes
% Specify the number of clusters
K = 2;
%% 1a) Get coordinate list from pointclouds %% 1a) Get coordinate list from pointclouds
% Coords used in this script % Coords used in this script
Pts = getPoints(); [Pts_spirals,Pts_clusterin,Pts_corn,Pts_halfk,Pts_fullmoon,Pts_out] = getPoints();
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};
for i = 1:6
% Specify the number of clusters
Pts = RUNS{i};
K = KS{i};
disp(TITLES(i));
figure; figure;
scatter(Pts(:,1),Pts(:,2)) scatter(Pts(:,1),Pts(:,2))
title('Two Spirals') title(TITLES(i))
n = size(Pts, 1); n = size(Pts, 1);
% Create Gaussian similarity function % Create Gaussian similarity function
[S] = similarityfunc(Pts(:,1:2)); [S] = similarityfunc(Pts(:,1:2), 10 * log(n));
%% 1b) Find the minimal spanning tree of the full graph. Use the information %% 1b) Find the minimal spanning tree of the full graph. Use the information
% to determine a valid value for epsilon % to determine a valid value for epsilon
@ -30,35 +38,32 @@ H = minSpanTree(S);
epsilon = max(H(H > 0), [], 'all'); epsilon = max(H(H > 0), [], 'all');
%% 1c) Create the epsilon similarity graph %% 1c) Create the epsilon similarity graph
[G] = epsilonSimGraph(epsilon,Pts, S); [G] = epsilonSimGraph(epsilon,Pts);
%% 1d) Create the adjacency matrix for the epsilon case %% 1d) Create the adjacency matrix for the epsilon case
W = S .* G; W = S .* G;
figure; figure;
gplotg(W,Pts(:,1:2)) gplotg(W,Pts(:,1:2))
title('Adjacency matrix') title('Adjacency matrix')
%% 1e) Create the Laplacian matrix and implement spectral clustering %% 1e) Create the Laplacian matrix and implement spectral clustering
[L,Diag] = CreateLapl(W); [L,Diag] = CreateLapl(W);
[V,~] = eigs(L, K, 'SA');
% \----------------------------/
% Your implementation
% \----------------------------/
% Cluster rows of eigenvector matrix of L corresponding to K smallest % Cluster rows of eigenvector matrix of L corresponding to K smallest
% eigennalues. Use kmeans for that. % eigennalues. Use kmeans for that.
[D_spec,x_spec] = kmeans_mod(Pts,K,n); [D_spec,x_spec] = kmeans_mod(V,K,n);
%% 1f) Run K-means on input data %% 1f) Run K-means on input data
% \----------------------------/
% Your implementation
% \----------------------------/
[D_kmeans,x_kmeans] = kmeans_mod(Pts,K,n); [D_kmeans,x_kmeans] = kmeans_mod(Pts,K,n);
%% 1g) Visualize spectral and k-means clustering results %% 1g) Visualize spectral and k-means clustering results
figure; figure;
subplot(1,2,1) subplot(1,2,1)
gplotmap(W,Pts,dummy_map) gplotmap(W,Pts,x_spec)
title('TODO: Plot the spectral clusters') title(strcat(TITLES(i), ': Spectral clusters'))
subplot(1,2,2) subplot(1,2,2)
gplotmap(W,Pts,dummy_map) gplotmap(W,Pts,x_kmeans)
title('TODO: Plot the K-means clusters') 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 % Construct an epsilon similarity graph
% Input % Input
% epsilon: size of neighborhood (calculate from Prim's Algorithm) % epsilon: size of neighborhood (calculate from Prim's Algorithm)
@ -14,6 +14,15 @@ fprintf('----------------------------\n');
fprintf('epsilon similarity graph\n'); fprintf('epsilon similarity graph\n');
fprintf('----------------------------\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 end

View file

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