clear all % Remove items from workspace, freeing up system memory
clc       % Clear Command Window  
close all % close all the opened matlab figure windows

% Load the data set
path_data='C:\Users\m.bennasar\OneDrive - University of Wales Trinity Saint David\Documents\AI\Datasets\iris.csv';
T = readtable(path_data);
Data_set = table2array(T); % convert table to array 
[m,n]=size(Data_set);
figure;
plot(Data_set(:,1),Data_set(:,2),'.','MarkerSize',15,'LineWidth',3) ;
title 'IRIS Data set';
%% Features Normalisation 
Data_set_N=Data_set;
for i=1:n-1
    Data_set_N(:,i)=(Data_set(:,1)- min(Data_set(:,1)))/(max(Data_set(:,1))-min(Data_set(:,1)));
end
%% clustering using K-means 
Data_set_N=Data_set;
% Distance metric=  'sqeuclidean', 'cityblock', 'cosine', 'correlation', or 'hamming'.
K=3;
 opts = statset('Display','final');
[idx,C,D] = kmeans(Data_set_N(:,1:n-1),K,'Distance','sqeuclidean','Replicates',5,'Options',opts);

%% Find change the value of K 
Dis=zeros(15,1);
opts = statset('Display','final');
for K=1:15
    [idx,C,D] = kmeans(Data_set_N(:,1:n-1),K,'Distance','sqeuclidean','Replicates',5,'Options',opts);
    S = sum (D,'all');
    Dis(K)=S;
end

plot(1:15,Dis);
grid on 
title 'Effect of K value on the distance'

    

%% plot the clusters 
color={'r.','b.','g.','k.','y.','c.','m.'}; 
figure;
for i=1:K
    
    plot3(Data_set(idx==i,1),Data_set(idx==i,2),Data_set(idx==i,3),color{i},'MarkerSize',12)
    hold on
end

plot3(C(:,1),C(:,2),C(:,3),'kx',...
     'MarkerSize',15,'LineWidth',3) 
grid on 
title 'Cluster Assignments and Centroids'
hold off
%% Plot the actual clases 
IDX_1=find(Data_set(:,5)==1);
IDX_2=find(Data_set(:,5)==2);
IDX_3=find(Data_set(:,5)==3);

color={'r.','b.','g.','k.','y.','c.','m.'}; 
figure;

    
    plot3(Data_set(IDX_1,1),Data_set(IDX_1,2),Data_set(IDX_1,3),color{1},'MarkerSize',12)
    hold on
    plot3(Data_set(IDX_2,1),Data_set(IDX_2,2),Data_set(IDX_2,3),color{2},'MarkerSize',12)
    hold on
    plot3(Data_set(IDX_3,1),Data_set(IDX_3,2),Data_set(IDX_3,3),color{3},'MarkerSize',12)
    hold on
    grid on


