clear all ;
clc ;

load fisheriris ;
i = 1 ;
j = 0 ;
p = 0 ;
labels = {'Sepal lenght ', 'Sepal width' , 'Petal width', 'Petal length'} ;
species_labels = {'setosa', 'versicolor', 'virginica'} ;

gplotmatrix(meas,[],[],[],[],[],[],[],labels,labels) ;
figure() ;
%% statistiki analisi

for j = 0 : 2
    
    for i = 1 : 4
        
        
        u = 50*j + 1 ;
        p = u + 49 ;
        meas_vector = meas( u:p , i ) ;
        mes_tim = mean(meas_vector) ;
        typ_ap = std(meas_vector) ;
        disp('mesi timi');
        disp(mes_tim) ;
        disp('tipiki apoklisi');
        disp(typ_ap) ;
        
    end
    
end
%% ierarxiki sistadopoiisi
k = 3 ;
w = 1 ;

    for i = 1 : 2
        
        for j = 1 : 3
             
            if i==1 && j==1
        
                Z = linkage(meas,'single','euclidean') ; 
                c = cluster(Z,'maxclust',k) ; 
                silhouette(meas,c) ;
                meh=silhouette(meas,c);
                mean(meh);
                disp('mesos sinetelestis iearakis sistadipoiisis');
                disp(mean(meh));
                figure() ;
                savefig(sprintf('cluster_graph%d',w));
                D = pdist(meas,'euclidean') ;
                leaforder=optimalleaforder(Z,D) ;
                dendrogram(Z,'Reorder',leaforder,'ColorThreshold','default') ;
                figure() ;
                savefig(sprintf('dendrogram%d',w));
                [table,chi2,P,lab] =  crosstab(species,c) ;
                disp('Table');
                disp(table);
                disp('chi2') ;
                disp(chi2) ;
                disp('P') ;
                disp(P) ;
                disp('lab') ;
                disp(lab);
                w = w + 1 ;
                
            end
            if i==1 && j==2
        
                Z = linkage(meas,'average','euclidean') ; 
                c = cluster(Z,'maxclust',k) ; 
                silhouette(meas,c) ;
                figure() ;
                savefig(sprintf('cluster_graph%d',w));
                D = pdist(meas,'euclidean') ;
                leaforder=optimalleaforder(Z,D) ;
                dendrogram(Z,'Reorder',leaforder,'ColorThreshold','default') ;
                meh=silhouette(meas,c);
                mean(meh);
                disp('mesos sinetelestis iearakis sistadipoiisis');
                disp(mean(meh));
                figure() ;
                savefig(sprintf('dendrogram%d',w));
                [table,chi2,P,lab] =  crosstab(species,c) ;
                disp('Table');
                disp(table);
                disp('chi2') ;
                disp(chi2) ;
                disp('P') ;
                disp(P) ;
                disp('lab') ;
                disp(lab);
                w = w + 1 ;
                
            end
            if i==1 && j==3
        
                Z = linkage(meas,'complete','euclidean') ; 
                c = cluster(Z,'maxclust',k) ; 
                silhouette(meas,c) ;
                meh=silhouette(meas,c);
                mean(meh);
                disp('mesos sinetelestis iearakis sistadipoiisis');
                disp(mean(meh));
                figure() ;
                savefig(sprintf('cluster_graph%d',w));
                D = pdist(meas,'euclidean') ;
                leaforder=optimalleaforder(Z,D) ;
                dendrogram(Z,'Reorder',leaforder,'ColorThreshold','default') ;
                figure() ;
                savefig(sprintf('dendrogram%d',w));
                [table,chi2,P,lab] =  crosstab(species,c) ;
                disp('Table');
                disp(table);
                disp('chi2') ;
                disp(chi2) ;
                disp('P') ;
                disp(P) ;
                disp('lab') ;
                disp(lab);
                w = w + 1 ;
                
            end
            if i==2 && j==1
        
                Z = linkage(meas,'single','correlation') ; 
                c = cluster(Z,'maxclust',k) ; 
                silhouette(meas,c) ;
                meh=silhouette(meas,c);
                mean(meh);
                disp('mesos sinetelestis iearakis sistadipoiisis');
                disp(mean(meh));
                figure() ;
                savefig(sprintf('cluster_graph%d',w));
                D = pdist(meas,'correlation') ;
                leaforder=optimalleaforder(Z,D) ;
                dendrogram(Z,'Reorder',leaforder,'ColorThreshold','default') ;
                figure() ;
                savefig(sprintf('dendrogram%d',w));
                [table,chi2,P,lab] =  crosstab(species,c) ;
                disp('Table');
                disp(table);
                disp('chi2') ;
                disp(chi2) ;
                disp('P') ;
                disp(P) ;
                disp('lab') ;
                disp(lab);
                w = w + 1 ;
                
            end
            if i==2 && j==2
        
                Z = linkage(meas,'average','correlation') ; 
                c = cluster(Z,'maxclust',k) ; 
                silhouette(meas,c) ;
                meh=silhouette(meas,c);
                mean(meh);
                disp('mesos sinetelestis iearakis sistadipoiisis');
                disp(mean(meh));
                figure() ;
                savefig(sprintf('cluster_graph%d',w));
                D = pdist(meas,'correlation') ;
                leaforder=optimalleaforder(Z,D) ;
                dendrogram(Z,'Reorder',leaforder,'ColorThreshold','default') ;
                figure() ;
                savefig(sprintf('dendrogram%d',w));
                [table,chi2,P,lab] =  crosstab(species,c) ;
                disp('Table') ;
                disp(table) ;
                disp('chi2') ;
                disp(chi2) ;
                disp('P') ;
                disp(P) ;
                disp('lab') ;
                disp(lab);
                w = w + 1 ;
                
            end
            if i==2 && j==3
        
                Z = linkage(meas,'complete','correlation') ; 
                c = cluster(Z,'maxclust',k) ; 
                silhouette(meas,c) ;
                meh=silhouette(meas,c);
                mean(meh);
                disp('mesos sinetelestis iearakis sistadipoiisis');
                disp(mean(meh));
                figure() ;
                savefig(sprintf('cluster_graph%d',w));
                D = pdist(meas,'correlation') ;
                leaforder=optimalleaforder(Z,D) ;
                dendrogram(Z,'Reorder',leaforder,'ColorThreshold','default') ;
                figure() ;
                savefig(sprintf('dendrogram%d',w));
                [table,chi2,P,lab] =  crosstab(species,c) ;
                disp('Table');
                disp(table);
                disp('chi2') ;
                disp(chi2) ;
                disp('P') ;
                disp(P) ;
                disp('lab') ;
                disp(lab);
                
            end
        end
    end



%% kmeans clustering
k = 3 ;
p = 1 ;

for i = 1 : 3
    
    if i == 1
        
        [idx,C] = kmeans(meas,k,'Distance','sqeuclidean') ;
        figure() ;
        silhouette(meas,idx) ;
        s=silhouette(meas,idx);
        mean(s);
        hold on ;
        disp('mesos sinetelestis kmeans sistadopoiisis');
        disp(mean(s));
        [table,chi2,P,lab] =  crosstab(species,idx) ;
        disp('Table');
        disp(table);
        disp('chi2') ;
        disp(chi2) ;
        disp('P') ;
        disp(P) ;
        disp('lab') ;
        disp(lab);
        var = 1 ;
        figure() ;
        ax=zeros(6,1) ;
            for p= 1 : 4
                for j = p : 4
                    if p ~= j
                        ax(var) = subplot(3,2,var) ;
                        scatter(ax(var),meas(:,p),meas(:,j),10,idx,'f') ;
                        xlabel(labels(p)) ;
                        ylabel(labels(j)) ;
                        var = var + 1 ;
                    end
                end
           end 
     end
    if i == 2
        
        [idx,C] = kmeans(meas,k,'Distance','correlation') ;
        figure() ;
        silhouette(meas,idx) ;
        s = silhouette(meas,idx);
        mean(s);
        disp('mesos sinetelestis kmeans sistadopoiisis');
        disp(mean(s));
        [table,chi2,P,lab] =  crosstab(species,idx) ;
        disp('Table');
        disp(table);
        disp('chi2') ;
        disp(chi2) ;
        disp('P') ;
        disp(P) ;
        disp('lab') ;
        disp(lab);
        hold on ;
        var = 1 ;
        figure() ;
        ax=zeros(6,1) ;
            for p= 1 : 4
                for j = p : 4
                    if p ~= j
                        ax(var) = subplot(3,2,var) ;
                        scatter(ax(var),meas(:,p),meas(:,j),10,idx,'f') ;
                        xlabel(labels(p)) ;
                        ylabel(labels(j)) ;
                        var = var + 1 ;
                    end
                end
           end 
     end
     if i == 3
        
        [idx,C] = kmeans(meas,k,'Distance','cosine') ;
        figure() ;
        silhouette(meas,idx) ;
        s = silhouette(meas,idx);
        mean(s);
        disp('mesos sinetelestis kmeans sistadopoiisis');
        disp(mean(s));
        [table,chi2,P,lab] =  crosstab(species,idx) ;
        disp('Table');
        disp(table);
        disp('chi2') ;
        disp(chi2) ;
        disp('P') ;
        disp(P) ;
        disp('lab') ;
        disp(lab);
        hold on ;
        var = 1 ;
        figure() ;
        ax=zeros(6,1) ;
            for p = 1 : 4
                for j = p : 4
                    if p ~= j
                        ax(var) = subplot(3,2,var) ;
                        scatter(ax(var),meas(:,p),meas(:,j),10,idx,'f') ;
                        xlabel(labels(p)) ;
                        ylabel(labels(j)) ;
                        var = var + 1 ;
                    end
                end
           end 
     end
end

