Commit 8ad2abd3 authored by Lacoin Achille's avatar Lacoin Achille
Browse files

copie du travail de Maxime

parent dedfa3e1
X = load("george.dat");
figure, plot(X(:,1), X(:,2), 'o', 'markersize', 8, 'markerfacecolor', 'r')
hold on
title('George','fontsize', 14)
ltlX = mydownsampling(X, 10);
M = distance(ltlX);
%%
method='complete';
level_max = aggclust(M, method);
figure
dendro(level_max);
title('Clustering avec distance max', 'fontsize', 24)
K=2; % nombre de clusters voulus
% recuperation des indices des points de chaque cluster
N = size(ltlX, 1);
clusters=level_max(N-K+1).cluster;
% trace des clusters obtenus
plotclusters(clusters, X, K);
title('Clustering avec distance max', 'fontsize', 24)
\ No newline at end of file
function [C,clusters,JWiter] = kmoyennes(X,K,Co)
C = Co;
MaxIter = 15;
JWiter = zeros(MaxIter,1);
iter = 1;
while (iter <= MaxIter)
[clusters, Jw] = affectation(X,C,K);
JWiter(iter) = Jw;
C = nouveaux_centres(X,clusters);
end
end
function [C, clusters, JwIter] = Kmoyennes(X, K, C0) % JwIter est le cout
Jw_old=0;
Jw_new=10;
JwIter=[];
i=1;
epsilon=0.01;
imax=500;
while ((abs(Jw_new-Jw_old)>epsilon) && (i<imax))
Jw_old=Jw_new;
[clusters, Jw_new] = affectation(X, C0, K);
[C0] = nouveaux_centres(X, clusters, K);
JwIter=[JwIter; Jw_new];
i=i+1;
end
C=C0;
end
\ No newline at end of file
function [clusters, Jw] = affectation(X, C, K)
N=size(X,1);
Jw=0;
%K=size(C,1); % Damien
for i=1:N
for k=1:K
distance(k)=norm(X(i,:)-C(k,:),2)^2;
end
[val, indice]=min(distance);
clusters(i)=indice;
% i est le numero du point en question et indice est l'indice du cluster
% le plus proche de ce point
Jw=Jw+val;
end
end
\ No newline at end of file
function [clusters, Jw] = affectation(X, C, K)
n = length(X);
Jw=0;
clusters = zeros(n, 1);
tailleC=size(C)
for i=1:n
distance = zeros(K, 1);
for k=1:K
distance(k) = norm(X(i,:)-C(k,:),2)^2;
end;
[val indice]=min(distance);
clusters(i)=indice;
Jw=Jw+val;
end
\ No newline at end of file
......@@ -15,7 +15,7 @@ function level = aggclust(distance, method)
% of level i
%
% Type "aggclust" to see a demo of a hierarchical clustering
% cluster(single-linkage) of 50 random patterns of dimensionality 2.
% (single-linkage) of 50 random patterns of dimensionality 2.
%
% See also DENDRO, LINKCLU.
......
function [C, clusters, JwIter] = Kmoyennes(X, K, C0)
[clusters, J] = affectation(X, C0, K);
C = nouveauxCentres(X, clusters);
JwIter = (J);
difJ = 1;
while difJ > 0
size(C)
[clusters, J] = affectation(X, C, K);
C = nouveauxCentres(X, clusters);
difJ = JwIter(length(JwIter)) - J;
JwIter = [JwIter J];
end
\ No newline at end of file
clear all;
close all;
clc;
%% 1) aggclust gégnère un dendogramme en fonction d'une matrice de distance
% et de la fonction de distance utilisée
%% 2) Le script génère 2 dendogrammes en utilisant les distances max et min
% La distance max donne le résultat de la vérité vraie alors que la
% distance min en est très loin
%% 3)
load('ds2.dat'); % Load ds2
load('george.dat'); % Load ds2
X = mydownsampling(george, 50);
M = distance(X);
K = 6; % Nombre de clusters
method='complete';
level_max = aggclust(M, method);
% Affichage du dendogramme
figure
dendro(level_max);
title('Clustering avec distance max', 'fontsize', 24)
% recuperation des indices des points de chaque cluster
N = size(X, 1);
clusters=level_max(N-K+1).cluster;
% trace des clusters obtenus
plotclusters(clusters, X, K);
title('Clustering avec distance max', 'fontsize', 24)
%% 2) 4)
close all; clc; clear all;
X = gendata(50);
K = 2;
% C0 = rand(K, size(X, 2));
%
% [C, clusters, JwIter] = Kmoyennes(X, K, C0);
%
% % trace des clusters obtenus
% plotclusters(clusters, X, K);
% title('Clustering avec distance max', 'fontsize', 24)
data = load('../data/ds2.dat');
georgy = load('../data/george.dat');
% 2.2)
K=6;
C02 = rand(K, size(georgy, 2));
[C2, clusters2, JwIter2] = Kmoyennes(georgy, K, C02);
plotclusters(clusters2, georgy, K);
title('Clustering avec distance max de Georges')
K=2;
C03 = rand(K, size(data, 2));
[C3, clusters3, JwIter3] = Kmoyennes(data, K, C03);
plotclusters(clusters3, data, K);
title('Clustering avec distance max de ds2')
function [clusters, Jw] = affectation(X, C, K)
n = length(X);
Jw = 0;
clusters = zeros(n, 1);
size(C)
for i=1:n
distance = zeros(K, 1);
for k=1:K
distance(k) = norm(X(i, :) - C(k, :))^2;
end
[val, indice] = min(distance);
clusters(i) = indice;
Jw = Jw + val;
end
\ No newline at end of file
function level = aggclust(distance, method)
% AGGCLUST Hierarchical (agglomerative) clustering
% Usage: level = aggclust(distance, method)
%
% distance: 2D distance matrix of data points, with diagonal elements
% of "INF"
% method: "single" for single-linkage
% "complete" for complete-linkage
% level: data structure for a hierarchical clustering result
% level(i).distance: distance matrix at level i
% level(i).height: the minimum distance measure to form level i
% level(i).merged: the two clusters (of level i-1) being merged to form
% level i
% level(i).cluster{j}: a vector denotes the data points in j-th cluster
% of level i
%
% Type "aggclust" to see a demo of a hierarchical clustering
% (single-linkage) of 50 random patterns of dimensionality 2.
%
% See also DENDRO, LINKCLU.
% Roger Jang, 981027
if nargin == 0, selfdemo; return; end
if nargin < 2, method = 'complete'; end
data_n = size(distance, 1);
level(1).distance = distance;
level(1).height = 0;
level(1).merged = [];
for i = 1:data_n,
level(1).cluster{i} = [i];
end
for i = 2:data_n,
level(i) = merge(level(i-1), method);
end
% ====== Merge clusters
function level_out = merge(level, method)
% MERGE Merge a level of n clusters into n-1 clusters
cluster_n = length(level.cluster);
[min_i, min_j, min_value] = minxy(level.distance);
% Reorder to have min_i < min_j
if min_i > min_j,
temp = min_i;
min_i = min_j;
min_j = temp;
end
level_out = level;
% Update height
level_out.height = min_value;
% Update merged cluster
level_out.merged = [min_i min_j];
% Update cluster
level_out.cluster{min_i} = [level_out.cluster{min_i} level_out.cluster{min_j}];
level_out.cluster(min_j) = []; % delete cluster{min_j}
%keyboard
% New distance matrix
distance2 = level.distance;
% "min" for single-linkage; "max" for complete-linkage
if strcmp(method, 'single'),
distance2(:, min_i) = min(distance2(:, min_i), distance2(:, min_j));
distance2(min_i, :) = min(distance2(min_i, :), distance2(min_j, :));
elseif strcmp(method, 'complete'),
distance2(:, min_i) = max(distance2(:, min_i), distance2(:, min_j));
distance2(min_i, :) = max(distance2(min_i, :), distance2(min_j, :));
else
error('Unsupported method in AGGCLUST!');
end
distance2(min_j, :) = [];
distance2(:, min_j) = [];
distance2(min_i, min_i) = inf;
level_out.distance = distance2;
% ====== Find the minimum value in a matrix
function [i, j, min_value] = minxy(A)
[value_row, index_row] = min(A);
[min_value, j] = min(value_row);
i = index_row(j);
% ====== Self demo ======
function selfdemo
data_n = 50;
dimension = 2;
points = rand(data_n, dimension);
for i = 1:data_n,
for j = 1:data_n,
distance(i, j) = norm(points(i,:)-points(j,:));
end
end
% Diagonal elements should always be inf.
for i = 1:data_n, distance(i, i) = inf; end
level = aggclust(distance);
% Plot heights w.r.t. levels
figure;
plot([level.height], 'r:o');
xlabel('Level');
ylabel('Height');
title('Height vs. level');
% Plot the dendrogram
figure;
dendro(level);
% View the formation of clusters
figure;
linkclu(points, distance, level);
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment