%------------ Applying K-means Algortihm in Data Clustering --------------%
clear all;
close all;
clc;

%--------------- Initializing the dataset of flowers ---------------------%
iris = load('iris0.txt'); %irisdataset.txt 
DataSet=iris(:,1:4); %dataset;
%y = iris(:,5);
%%%%%%%%
% one feature 
scatter(1:150,iris(:,1));
close all;
scatter(1:50,iris(1:50,1),'k*');
hold on
scatter(1:50,iris(51:100,1),'*');
scatter(1:50,iris(101:150,1),'b*');
close all;

% two features
scatter(iris(:,1),iris(:,2));
close all;
scatter(iris(1:50,1),iris(1:50,2),'k*');
hold on
scatter(iris(51:100,1),iris(51:100,2),'*');
scatter(iris(101:150,1),iris(101:150,2),'b*');
close all;

%three features
scatter3(iris(:,1),iris(:,2),iris(:,3),'k*')
close all;

scatter3(iris(1:50,1),iris(1:50,2),iris(1:50,3),'r*')
hold on
scatter3(iris(51:100,1),iris(51:100,2),iris(51:100,3),'*')
scatter3(iris(101:150,1),iris(101:150,2),iris(101:150,3),'b*')
      


[l,C] = kmeans(iris,3);

scatter3(C(1,1),C(1,2),C(1,3),'bo');
scatter3(C(2,1),C(2,2),C(2,3),'ko');
scatter3(C(3,1),C(3,2),C(3,3),'ro');

clear r;






Dim=size(DataSet);
      
%----------------------- Selecting 3 random centres ----------------------% 
Selection=rand(1,3);
Selection=Selection*Dim(1,1);
Selection=ceil(Selection); %Selecting the row number.

%--------------------------- 3 random centres ----------------------------%
Centre1=DataSet(Selection(1),:);
Centre2=DataSet(Selection(2),:);
Centre3=DataSet(Selection(3),:);



n=input('No Of Iterations : ')
%----------------------- Partitional Algorithm ---------------------------%
%-------------------------- K means Algorithm ----------------------------%
for j=1:1:n
count1=0;
Mean1=zeros(1,4);
count2=0;
group1=[];
Mean2=zeros(1,4);
group2=[];
count3=0;
group3=[];
Mean3=zeros(1,4);

%Finding the minimum distance
for i=1:1:Dim(1,1)
    
Pattern1(i)=sqrt((Centre1(1,1)-DataSet(i,1))^2+(Centre1(1,2)-DataSet(i,2))^2+(Centre1(1,3)-DataSet(i,3))^2+(Centre1(1,4)-DataSet(i,4))^2);
Pattern2(i)=sqrt((Centre2(1,1)-DataSet(i,1))^2+(Centre2(1,2)-DataSet(i,2))^2+(Centre2(1,3)-DataSet(i,3))^2+(Centre1(1,4)-DataSet(i,4))^2);
Pattern3(i)=sqrt((Centre3(1,1)-DataSet(i,1))^2+(Centre3(1,2)-DataSet(i,2))^2+(Centre3(1,3)-DataSet(i,3))^2+(Centre1(1,4)-DataSet(i,4))^2);

LessDist=[Pattern1(i) Pattern2(i) Pattern3(i)];
Minimum=min(LessDist);

%Finding the new centre
if (Minimum==Pattern1(i))
    count1=count1+1;
    Mean1=Mean1+DataSet(i,:);
    group1=[group1 i];
else if (Minimum==Pattern2(i))
        count2=count2+1;
        Mean2=Mean2+DataSet(i,:);
        group2=[group2 i];
    else count3=count3+1;
        Mean3=Mean3+DataSet(i,:);
        group3=[group3 i];  
    end
end

end

%----------------------------- New Centres -------------------------------%
Centre1=Mean1/count1;
Centre2=Mean2/count2;
Centre3=Mean3/count3;
plot(j,count1,'r');
hold on
plot(j,count2,'g');
plot(j,count3,'b');

end

hold off
% specify the indexed color for each point
icolor = ceil((DataSet(:,4)/max(DataSet(:,4)))*256);

figure,
scatter3(DataSet(:,1),DataSet(:,2),DataSet(:,3),DataSet(:,4),icolor,'filled');
figure,

scatter3(DataSet(:,1),DataSet(:,2),DataSet(:,3),[],DataSet(:,4),'filled');


