clc
clear all
%% tento skript slouží pro nehierarchické shlukování dat
%načtení příznaků
stredni = load('stredni_h.mat');
stredni = stredni.vykresleni_stred_h;
aktivita = load('aktivita.mat');
aktivita = aktivita.vykresleni_akt;
var = load('variacni_koeficient.mat');
var = var.vykresleni_var;
komplexita = load('komplexita.mat');
komplexita = komplexita.vykresleni_cplx;
mobilita = load('mobilita.mat');
mobilita = mobilita.vykresleni_mob;
spicatost = load('spicatost.mat');
spicatost = spicatost.vykresleni_stred_h;
rozptyl = load('rozptyl.mat');
rozptyl = rozptyl.vykresleni_rozptyl;
odchylka = load('odchylka.mat');
odchylka = odchylka.vykresleni_odch;
% změna velikostí matic - rozdělení na organismy
jl = stredni(1,1:800);
A = reshape(jl,[800,1]);
cb = stredni(2,1:800);
B = reshape(cb,[800,1]);
kp1 = stredni(3,1:800);
C = reshape(kp1,[800,1]);
kp2 = stredni(4,1:800);
D = reshape(kp2,[800,1]);
kp3 = stredni(5,1:800);
E = reshape(kp3,[800,1]);
kp4 = stredni(6,1:800);
F = reshape(kp4,[800,1]);
pa = stredni(7,1:800);
G = reshape(pa,[800,1]);
st = stredni(8,1:800);
H = reshape(st,[800,1]);
% uložení vektorů do matice (obsahuje všechny organismy) 
stredni = [A;B;C;D;E;F;G;H];

jl = aktivita(1,1:800);
A = reshape(jl,[800,1]);
cb = aktivita(2,1:800);
B = reshape(cb,[800,1]);
kp1 = aktivita(3,1:800);
C = reshape(kp1,[800,1]);
kp2 = aktivita(4,1:800);
D = reshape(kp2,[800,1]);
kp3 = aktivita(5,1:800);
E = reshape(kp3,[800,1]);
kp4 = aktivita(6,1:800);
F = reshape(kp4,[800,1]);
pa = aktivita(7,1:800);
G = reshape(pa,[800,1]);
st = aktivita(8,1:800);
H = reshape(st,[800,1]);

aktivita = [A;B;C;D;E;F;G;H];


jl = var(1,1:800);
A = reshape(jl,[800,1]);
cb = var(2,1:800);
B = reshape(cb,[800,1]);
kp1 = var(3,1:800);
C = reshape(kp1,[800,1]);
kp2 = var(4,1:800);
D = reshape(kp2,[800,1]);
kp3 = var(5,1:800);
E = reshape(kp3,[800,1]);
kp4 = var(6,1:800);
F = reshape(kp4,[800,1]);
pa = var(7,1:800);
G = reshape(pa,[800,1])
st = var(8,1:800);
H = reshape(st,[800,1]);

var = [A;B;C;D;E;F;G;H];


jl = komplexita(1,1:800);
A = reshape(jl,[800,1]);
cb = komplexita(2,1:800);
B = reshape(cb,[800,1]);
kp1 = komplexita(3,1:800);
C = reshape(kp1,[800,1]);
kp2 = komplexita(4,1:800);
D = reshape(kp2,[800,1]);
kp3 = komplexita(5,1:800);
E = reshape(kp3,[800,1]);
kp4 = komplexita(6,1:800);
F = reshape(kp4,[800,1]);
pa = komplexita(7,1:800);
G = reshape(pa,[800,1]);
st = komplexita(8,1:800);
H = reshape(st,[800,1]);

komplexita = [A;B;C;D;E;F;G;H];

jl = mobilita(1,1:800);
A = reshape(jl,[800,1]);
cb = mobilita(2,1:800);
B = reshape(cb,[800,1]);
kp1 = mobilita(3,1:800);
C = reshape(kp1,[800,1]);
kp2 = mobilita(4,1:800);
D = reshape(kp2,[800,1]);
kp3 = mobilita(5,1:800);
E = reshape(kp3,[800,1]);
kp4 = mobilita(6,1:800);
F = reshape(kp4,[800,1]);
pa = mobilita(7,1:800);
G = reshape(pa,[800,1]);
st = mobilita(8,1:800);
H = reshape(st,[800,1]);

mobilita = [A;B;C;D;E;F;G;H];

jl = spicatost(1,1:800);
A = reshape(jl,[800,1]);
cb = spicatost(2,1:800);
B = reshape(cb,[800,1]);
kp1 = spicatost(3,1:800);
C = reshape(kp1,[800,1]);
kp2 = spicatost(4,1:800);
D = reshape(kp2,[800,1]);
kp3 = spicatost(5,1:800);
E = reshape(kp3,[800,1]);
kp4 = spicatost(6,1:800);
F = reshape(kp4,[800,1]);
pa = spicatost(7,1:800);
G = reshape(pa,[800,1]);
st = spicatost(8,1:800);
H = reshape(st,[800,1]);

spicatost = [A;B;C;D;E;F;G;H];


jl = rozptyl(1,1:800);
A = reshape(jl,[800,1]);
cb = rozptyl(2,1:800);
B = reshape(cb,[800,1]);
kp1 = rozptyl(3,1:800);
C = reshape(kp1,[800,1]);
kp2 = rozptyl(4,1:800);
D = reshape(kp2,[800,1]);
kp3 = rozptyl(5,1:800);
E = reshape(kp3,[800,1]);
kp4 = rozptyl(6,1:800);
F = reshape(kp4,[800,1]);
pa = rozptyl(7,1:800);
G = reshape(pa,[800,1]);
st = rozptyl(8,1:800);
H = reshape(st,[800,1]);

rozptyl = [A;B;C;D;E;F;G;H];

jl = odchylka(1,1:800);
A = reshape(jl,[800,1]);
cb = odchylka(2,1:800);
B = reshape(cb,[800,1]);
kp1 = odchylka(3,1:800);
C = reshape(kp1,[800,1]);
kp2 = odchylka(4,1:800);
D = reshape(kp2,[800,1]);
kp3 = odchylka(5,1:800);
E = reshape(kp3,[800,1]);
kp4 = odchylka(6,1:800);
F = reshape(kp4,[800,1]);
pa = odchylka(7,1:800);
G = reshape(pa,[800,1]);
st = odchylka(8,1:800);
H = reshape(st,[800,1]);

odchylka = [A;B;C;D;E;F;G;H];

%% nastavení počtu příznaků 
X = [mobilita,aktivita,stredni,var,rozptyl,komplexita,odchylka]; %vybrané příznaky
X = zscore(X); % normalizace dat pomocí z score
%% nastavení příslušnosti ke shlukům
% pokud shlukuji podle počtu vzorků, přiřadím čísla od 1 do 8.
idx_skutecne(1:800,1)=2;
idx_skutecne(801:1600,1)=1;
idx_skutecne(1601:2400,1)=2;
idx_skutecne(2401:3200,1)=2;
idx_skutecne(3201:4000,1)=1;
idx_skutecne(4001:4800,1)=1;
idx_skutecne(4801:5600,1)=1;
idx_skutecne(5601:6400,1)=1;
centroidy = load('centroidy.mat') %nastavení centroidů - podle nejlepšícch výsledků
C_centr = centroidy.C_centr
X = tsne(X) %tsne analýza
[idx,C_centr,~,D_centr] = kmeans(X,2,'Distance', 'Sqeuclidean','Start',C_centr ) % kmeans 





%  zařazení do shluků
figure
plot(X(idx==1,1),X(idx==1,2),'.')
hold on
plot(X(idx==2,1),X(idx==2,2),'.')
hold on
plot(C_centr(:,1),C_centr(:,2),'kx','LineWidth',2)
xlabel('t-SNE1')
ylabel('t-SNE2')
legend('LSK','RBK',' Centroid')
title 'Zařazení do shluků a centroidy'
hold off


%Příslušnost k organismům 
figure
plot(X(idx_skutecne==1,1),X(idx_skutecne==1,2),'.')
hold on
plot(X(idx_skutecne==2,1),X(idx_skutecne==2,2),'.')
hold on
plot(C_centr(:,1),C_centr(:,2),'kx','LineWidth',2)
hold on
legend('LSK','RBK',' Centroid')
xlabel('t-SNE1')
ylabel('t-SNE2')
title 'Příslušnost k organismům a centroidy shluků'
hold off


%výpočet konfuzní matice
[c]=confusionmat(idx, idx_skutecne);
confusionchart(c)

