% Alberto Lusoli % Analisi Cluster % Dataset "CPU Performance" % Prima di eseguire il file ¸ necessario importare il dataset. X=data(:,(1:7)); % considero le variabili dalla 1 alla 7 figure('Name','Istogrammi in frequenza'); % Eseguo analisi delle variabili for var=1:7 % Confronto istogrammi in frequenza subplot(2,4,var) % Ciclo che disegna per ogni variabile [n,x]=hist(X(:,var)); % un istogramma in frequenza bar(x,n,1); axis square title(['Variabile ',num2str(var)]) end figure('Name','Boxplot Variabili'); % Disegno il boxplot per ogni variabile for var=1:7 subplot(2,4,var) % Ciclo che disegna par ogni vaiabile boxplot(X(:,var)) % un box plot title(['Variabile ',num2str(var)]) end Y = pdist(X,'cityblock'); Z = linkage(Y,'ward'); c = cophenet(Z,Y) Y = pdist(X,'cityblock'); Z = linkage(Y,'complete'); % Calcolo in coefficiente cofenetico per ogni combinazione di misura di distanza e tipo di legame c = cophenet(Z,Y) Y = pdist(X,'cityblock'); Z = linkage(Y,'average'); c = cophenet(Z,Y) Y = pdist(X,'euclidean'); Z = linkage(Y,'complete'); % Seleziono la combinazione con coefficiente maggiore c = cophenet(Z,Y) Y = pdist(X,'euclidean'); Z = linkage(Y,'ward'); c = cophenet(Z,Y) Y = pdist(X,'euclidean'); squareform(Y); Z = linkage(Y,'average'); c = cophenet(Z,Y) % la migliore ¸ la combinazione legame medio e distanza euclidea figure('Name','Dendogramma'); [H, T] = dendrogram(Z,size(X,1)); %Disegno il dendogramma title('Dendogramma') classtot=zeros(209,4); for g=2:5 class = cluster(Z,g); tabulate (class) % Ciclo che calcola per ogni clustering da 2 a 5 gruppi il VRC. Scelgo la divisione con VRC Maggiore cio¸ 5 classtot(:,g)=class; [D,P,STATS] = MANOVA1(X,classtot(:,g)); VRC(g,1)=(trace(STATS.B)/(g-1))/(trace(STATS.W)/(209-g)); end VRC ngruppi=2; % Eseguo clustering K Means a 2 gruppi [centri,u,f] = kmeans(X,ngruppi); kclass2=u(1,:)'+2.*u(2,:)'; t=tabulate(kclass2) % visualizzo i cluster e il numero di elementi contenuti in ciacun cluster [D,P,STATS] = MANOVA1(X,kclass2); within=trace(STATS.W) VRC_K=(trace(STATS.B)/(max(kclass2)-1))/(trace(STATS.W)/(size(X,1)-max(kclass2))) % Calcolo VRC per clustering in 2 gruppi ngruppi=3; % Eseguo clustering K Means a 3 gruppi [centri,u,f] = kmeans(X,ngruppi); kclass3=u(1,:)'+2.*u(2,:)'+3.*u(3,:)'; t=tabulate(kclass3) % visualizzo i cluster e il numero di elementi contenuti in ciacun cluster [D,P,STATS] = MANOVA1(X,kclass3); within=trace(STATS.W) VRC_K=(trace(STATS.B)/(max(kclass3)-1))/(trace(STATS.W)/(size(X,1)-max(kclass3))) % Calcolo VRC per clustering in 3 gruppi ngruppi=4; % Eseguo clustering K Means a 4 gruppi [centri,u,f] = kmeans(X,ngruppi); kclass4=u(1,:)'+2.*u(2,:)'+3.*u(3,:)'+4.*u(4,:)'; t=tabulate(kclass4) % visualizzo i cluster e il numero di elementi contenuti in ciacun cluster [D,P,STATS] = MANOVA1(X,kclass4); within=trace(STATS.W) VRC_K=(trace(STATS.B)/(max(kclass4)-1))/(trace(STATS.W)/(size(X,1)-max(kclass4))) % Calcolo VRC per clustering in 4 gruppi ngruppi=5; % Eseguo clustering K Means a 5 gruppi [centri,u,f] = kmeans(X,ngruppi); kclass5=u(1,:)'+2.*u(2,:)'+3.*u(3,:)'+4.*u(4,:)'+5.*u(5,:)'; t=tabulate(kclass5) % visualizzo i cluster e il numero di elementi contenuti in ciacun cluster [D,P,STATS] = MANOVA1(X,kclass5); within=trace(STATS.W) VRC_K=(trace(STATS.B)/(max(kclass5)-1))/(trace(STATS.W)/(size(X,1)-max(kclass5))) % Calcolo VRC per clusstering in 5 gruppi figure('Name','Clustering Kmeans a 3 gruppi'); % Disegno il grafico Clustering Kmeans a 3 gruppi dato che ha VRC maggiore gscatter(X(:,(1:6)),X(:,7),kclass3) title('Clustering K Means a 3 gruppi') % Disegno il grafico Matrice scatterplots a 3 gruppi figure('Name','Matrice scatterplots clustering 3 gruppi'); gplotmatrix(X,[],kclass3) figure('Name','Relazioni tra variabili'); % Disegno il grafico per relazione tra variabili gplotmatrix(X,X,kclass3) title('Relazioni tra variabili') stdr = std(X); % K-means su componenti principali sr = X./repmat(stdr,size(X,1),1); [coefs,scores,variances,t2] = princomp(sr); figure('Name','Componenti principali'); subplot(2,1,1) plot(scores(:,1),scores(:,2),'+'); xlabel('1st Principal Component'); ylabel('2nd Principal Component'); percent_explained = 100*variances/sum(variances); % Varianza spiegata subplot(2,1,2) pareto(percent_explained) xlabel('Principal Component') ylabel('Variance Explained (%)') title('Scree plot') [center, U, distortion] = kmeans(scores, 3); % Esegui analisi cluster kmeans sulle componenti principali cluster3=(U(1,:)'+2.*U(2,:)'+3.*U(3,:)'); tabulate(cluster3) figure('Name','Raggruppamento kmeans su componenti principali') gscatter(X(:,1),X(:,2),cluster3); title('Raggruppamento kmeans su componenti principali'); xlabel('x1'); ylabel('x2'); [D,P,STATS] = MANOVA1(X,cluster3); f1=trace(STATS.W) VRC_CP=(trace(STATS.B)/(max(cluster3)-1))/(trace(STATS.W)/(size(X,1)-max(cluster3))) %Calcolo Variance Ratio Criterion clprest(:,2)=class; clprest(:,3)=kclass3; clprest(:,1)=data(:,7); mat1=1; % Calcolo statistiche clustering gerarchico e k-means mat2=1; mat3=1; mat4=1; mat5=1; for e=1:209 if clprest(e,2)==1 gruppo1(mat1,(1:7))=data(e,(1:7)); % Creo una matrice per ogni gruppo ottenuto dal clustering gerarchico mat1=mat1+1; end if clprest(e,2)==2 gruppo2(mat2,(1:7))=data(e,(1:7)); mat2=mat2+1; end if clprest(e,2)==3 gruppo3(mat3,(1:7))=data(e,(1:7)); mat3=mat3+1; end if clprest(e,2)==4 gruppo4(mat4,(1:7))=data(e,(1:7)); mat4=mat4+1; end if clprest(e,2)==5 gruppo5(mat5,(1:7))=data(e,(1:7)); mat5=mat5+1; end end mat1=1; mat2=1; mat3=1; for e=1:209 if clprest(e,3)==1 gruppok1(mat1,(1:7))=data(e,(1:7)); % Creo una matrice per ogni gruppo ottenuto dal clustering k-means mat1=mat1+1; end if clprest(e,3)==2 gruppok2(mat2,(1:7))=data(e,(1:7)); mat2=mat2+1; end if clprest(e,3)==3 gruppok3(mat3,(1:7))=data(e,(1:7)); mat3=mat3+1; end end stat_gruppo1=zeros(3,7); stat_gruppo2=zeros(3,7); stat_gruppo3=zeros(3,7); stat_gruppo4=zeros(3,7); stat_gruppo5=zeros(3,7); for e=1:7 stat_gruppo1(1,e)= min(gruppo1(:,e)); % Per ogni gruppo calcolo il massimo, il minimo e la media di ogni attributo stat_gruppo1(2,e)= max(gruppo1(:,e)); stat_gruppo1(3,e)= mean(gruppo1(:,e)); end for e=1:7 stat_gruppo2(1,e)= min(gruppo2(:,e)); stat_gruppo2(2,e)= max(gruppo2(:,e)); stat_gruppo2(3,e)= mean(gruppo2(:,e)); end for e=1:7 stat_gruppo3(1,e)= min(gruppo3(:,e)); stat_gruppo3(2,e)= max(gruppo3(:,e)); stat_gruppo3(3,e)= mean(gruppo3(:,e)); end for e=1:7 stat_gruppo4(1,e)= min(gruppo4(:,e)); stat_gruppo4(2,e)= max(gruppo4(:,e)); stat_gruppo4(3,e)= mean(gruppo4(:,e)); end for e=1:7 stat_gruppo5(1,e)= min(gruppo5(:,e)); stat_gruppo5(2,e)= max(gruppo5(:,e)); stat_gruppo5(3,e)= mean(gruppo5(:,e)); end for e=1:7 stat_gruppok1(1,e)= min(gruppok1(:,e)); % Per ogni gruppo k-means calcolo il massimo, il minimo e la media di ogni attributo stat_gruppok1(2,e)= max(gruppok1(:,e)); stat_gruppok1(3,e)= mean(gruppok1(:,e)); end for e=1:7 stat_gruppok2(1,e)= min(gruppok2(:,e)); stat_gruppok2(2,e)= max(gruppok2(:,e)); stat_gruppok2(3,e)= mean(gruppok2(:,e)); end for e=1:7 stat_gruppok3(1,e)= min(gruppok3(:,e)); stat_gruppok3(2,e)= max(gruppok3(:,e)); stat_gruppok3(3,e)= mean(gruppok3(:,e)); end figure('Name','Boxplot Cluster gerarchici'); % Disegno i Box Plot relativi all'attributo PRP per ogni gruppo ottenuto subplot(2,3,1) % con clusterning gerarchico boxplot(gruppo1(:,7)) ylabel('PRP') title('Cluster1 ') subplot(2,3,2) boxplot(gruppo2(:,7)) ylabel('PRP') title('Cluster2 ') subplot(2,3,3) boxplot(gruppo3(:,7)) ylabel('PRP') title('Cluster3 ') subplot(2,3,4) boxplot(gruppo4(:,7)) ylabel('PRP') title('Cluster4 ') subplot(2,3,5) boxplot(gruppo5(:,7)) ylabel('PRP') title('Cluster5 ') figure('Name','Boxplot Cluster k-means'); % Disegno i Box Plot relativi all'attributo PRP per ogni gruppo ottenuto subplot(1,3,1) % con clustering k-means boxplot(gruppok1(:,7)) ylabel('PRP') title('Cluster1 ') subplot(1,3,2) boxplot(gruppok2(:,7)) ylabel('PRP') title('Cluster2 ') subplot(1,3,3) boxplot(gruppok3(:,7)) ylabel('PRP') title('Cluster3 ')