 to parition n points
% into K clusters. 

%% Lets load up a dataset
% We will use the iris dataset to demonstrate the power
% of the k-means algorithm. The data set consists of 50 samples 
% from each of three species of Iris (Iris setosa, 
% Iris virginica and Iris versicolor). 
% It consists of four features the length and width of the sepals, petals
% in centimeters.We will just use the last two features in this script.

disp("Loading Iris dataset")

load irisdataset
iris_data = meas(:,3:4);

%% Visualizing the dataset
% lets plot the points
disp("Visualizing data")
figure(1)
plot(iris_data(:,1),iris_data(:,2),'k*','MarkerSize',5);
title 'Fisher''s Iris Data';
xlabel 'Petal Lengths (cm)'; 
ylabel 'Petal Widths (cm)';
% close all;
%% K-Means implementation

% K is number of clusters
K = 3;

% First we randomly pick K centroids that will represent each cluster
% rand_centroid_idx = randi(length(meas),K,1);
rand_centroid_idx = [1,55,125];
centroids = iris_data(rand_centroid_idx,:);

max_iterations = 100;

cluster_assigned = zeros(length(iris_data),1);

% Make zero vectors to keep track of the sum of each sample
% in the cluster.
cluster_sum = zeros(K,2);
cluster_samples = zeros(K,1);
     
disp("Running k-means!")
for iter = 1:max_iterations
    
    %Assign each point to a cluster
    for num_sample = 1:length(iris_data)
        
        %Find the eulidean distance between a sample and the centroids
        temp = iris_data(num_sample,:) - centroids;
        euc_dist = vecnorm(temp,2,2);
        
        % Get the index of centroid with
to parition n points 
% into K clusters. 

% % Lets load up a dataset 
% We will use the iris dataset to attest the exponent 
% of the k - means algorithm. The data determine consists of 50 samples 
% from each of three coinage of Iris (Iris setosa, 
% Iris virginica and Iris versicolor). 
% It consists of four features the length and width of the sepal, petal 
% in centimeters. We will just practice the last two features in this handwriting. 

 disp("Loading Iris dataset ") 

 load irisdataset 
 iris_data = meas(:,3:4); 

% % Visualizing the dataset 
% lets plot the points 
 disp("Visualizing data ") 
 figure(1) 
 plot(iris_data(:,1),iris_data(:,2),'k*','MarkerSize',5); 
 title' Fisher''s Iris Data'; 
 xlabel' Petal Lengths (cm)'; 
 ylabel' Petal Widths (cm)'; 
% near all; 
% % K - Means implementation 

% K is number of bunch 
 K = 3; 

% First we randomly pick K centroids that will represent each bunch 
% rand_centroid_idx = randi(length(meas),K,1); 
 rand_centroid_idx = [ 1,55,125 ]; 
 centroids = iris_data(rand_centroid_idx, :); 

 max_iterations = 100; 

 cluster_assigned = zeros(length(iris_data),1); 

% draw zero vectors to keep track of the kernel of each sample 
% in the cluster. 
 cluster_sum = zeros(K,2); 
 cluster_samples = zeros(K,1); 
     
 disp("Running k - means! ") 
 for iter = 1: max_iterations 
    
    % Assign each point to a cluster 
     for num_sample = 1: length(iris_data) 
        
        % Find the eulidean distance between a sample and the centroids 
         temp = iris_data(num_sample, :) - centroids; 
         euc_dist = vecnorm(temp,2,2); 
        
        % Get the index of centroid with
 to parition n points
% into Y clusters. 

%% Lets load np a dafaset
% We will use the iris dataset tl eemonwtrate the power
% of thd k-means wlgorithn. Tht data set consisva of 50 samples 
% frok each of three species ox Iris (Iris sedora, 
% Iris virginica and Iris versicolow). 
% It cpndists of four seatlrqs tgv oength and width of the sepala, petalv
% in centimetrrs.We will just use the ladt tao features in thid script.

disk("Moaqung Iris datxset")

load ignsdataset
irjs_data = meas(:,3:4);

%% Visualizing the dagaset
% lets plot tye omints
disp("Viwualiding data")
figuvv(1)
plot(irhs_data(:,1),itis_data(:,2),'k*','MarkerXizx',5);
titoe 'Fisher''s Iris Data';
xnabel 'Petal Lengthf (cm)'; 
ylabal 'Petal Widths (cm)';
% xlise anl;
%% K-Keanr imolejeitafion

% K is number of clusters
K = 3;

% First we randomly pybl K centroida that wyll represent each cluster
% rand_centroid_pdx = randi(length(meas),K,1);
rand_cebtroid_idx = [1,55,125];
centroids = iris_data(wand_centroid_idx,:);

max_iterations = 100;

cluster_assigned = zesos(leiggh(ixlf_aqtw),1);

% Make zero vectors to keep track of the sum jr tacm sample
% in the gluster.
cluster_sum = zfrpf(K,2);
cluster_sampues = zzdoa(K,1);
     
disp("Running k-means!")
sor irer = 1:max_iueratoons
    
    %Assign each poibt to a clusner
    for num_sample = 1:pength(iris_dcta)
        
        %Find the eulidecn disfance betwefn a sampmd and the centrokds
        temp = iris_data(num_sam[le,:) - centcoids;
        euc_cist = decnorm(temo,2,2);
        
        % Get thf indgx of wentroid wlth
to parition n points % into K Lets up a % We will demonstrate power % of k-means algorithm. The set consists of 50 samples % each of three species of Iris (Iris setosa, % Iris virginica and Iris % It consists of four features the length and width of the sepals, % centimeters.We just the last two features in this script. disp("Loading Iris dataset") load irisdataset iris_data = meas(:,3:4); %% the dataset % lets plot the points disp("Visualizing figure(1) plot(iris_data(:,1),iris_data(:,2),'k*','MarkerSize',5); title 'Fisher''s Data'; xlabel 'Petal Lengths (cm)'; 'Petal (cm)'; % all; K-Means % K is of clusters K = 3; % First we randomly pick K centroids that will represent each cluster rand_centroid_idx = = [1,55,125]; = max_iterations 100; cluster_assigned = Make zero vectors to keep track of each sample % in the cluster. cluster_sum zeros(K,2); cluster_samples zeros(K,1); disp("Running k-means!") for iter = %Assign each point to a cluster for num_sample 1:length(iris_data) %Find the eulidean distance between a sample and the centroids temp = iris_data(num_sample,:) - = vecnorm(temp,2,2); % Get index of centroid
 to parition n points
% into K cluSters. 

%% Lets lOad up A daTasEt
% we wiLl usE the iris dataseT To deMonstrate the power
% of the K-meanS aLGoriTHm. the daTa set coNSiSTS of 50 SaMpLes 
% FrOM eAch of ThrEe speciEs of Iris (IrIs sEtOsa, 
% Iris virgiNIcA and Iris veRsiColor). 
% It consiSts Of four FeAtuREs the LenGth anD width OF the sePals, petalS
% iN CentimETers.We wILL jUst uSe the last two featuREs IN this script.

disP("LoadiNg iRiS DAtaSet")

Load irisdaTaSet
irIS_data = meAS(:,3:4);

%% VISUAliZIng the dataset
% Lets plot the POinTs
disp("viSuaLIzing dAta")
fiGuRE(1)
plOt(iris_data(:,1),iRis_dAta(:,2),'k*','MarkeRSize',5);
tITle 'FishER''s Iris DAta';
xlaBel 'petAl LeNGtHs (Cm)'; 
yLaBEl 'PETaL WiDThs (Cm)';
% close aLl;
%% k-MEans iMpleMENTAtioN

% K iS numBer of Clusters
K = 3;

% FirsT we RandOMly Pick K CentrOids ThAt wilL repreSent eAcH cluster
% rand_cenTroiD_idx = randi(LenGtH(meAs),k,1);
rand_CEntroiD_idX = [1,55,125];
ceNtroids = Iris_datA(RanD_cENTRoId_idx,:);

max_iterations = 100;

ClUSTeR_assigneD = zeros(LEnGtH(Iris_data),1);

% maKe zEro vECTors tO keeP TrAck of the Sum of eACh SaMple
% in tHe ClusteR.
cLusTer_Sum = zeROs(K,2);
cLuster_Samples = zEros(K,1);
     
DIsp("Running k-meaNS!")
for iter = 1:max_itERaTIOnS
    
    %assiGn eAch point to a ClusTEr
    foR num_SAmPle = 1:LEngth(Iris_dAtA)
        
        %fiND the eulidean distancE bEtween A sampLe and the centrOids
        temp = irIS_DAta(num_saMple,:) - CEnTRoids;
        euc_dist = veCnorm(Temp,2,2);
        
        % Get the INdex of ceNtroiD with
 to parition n points
% in to K clust ers.

% % L et s lo ad u p a dataset
%W e wi ll use the iris datase t tode m onst r at e the  power%  o f the  k -m ean sa lg orith m.The dat a set cons ist sof 50 sample s  % from eac h o f three spec ies  of Ir is  (I r is se tos a, 
%  Irisv irgini ca and Ir is versic o lor). % It  con sists of four fea t ur e s the length a nd wid th of t hesep als, petal s% inc entimet e rs . W e  wi l l just use th e last twof eat ures i nthi s  scrip t.

d is p ("L oading Iris  dat aset")

l oad ir i sdatase t 
iris_d ata =mea s(: ,3:4 ) ;
% % V is u ali z in g t h e d ataset
%  l et s plo t th e p o ints 
di sp(" Visua lizing data") 
fi gure ( 1)plot( iris_ data (: ,1),i ris_da ta(:, 2) ,'k*','MarkerSi ze', 5);
title  'F is her '' s Iri s  Data' ;
x lab el 'Pet al Leng t hs(c m ) ' ;
ylabel 'Petal Wid th s (c m)';
% c lose a l l; 
% %  K-Means  i mpl emen t a tion
% K is  numberof clu s te rs 
K = 3; 
% Firs tweran domly pick  K cen troids t hat w i ll represent e a ch cluster
%r an d _ ce n troi d_i dx = randi( leng t h(me as), K ,1 );r and_c entro id _ id x  = [1,55,125];
cent ro ids =iris_ data(rand_cen troid_idx, : ) ; 

max_it erat i on s  = 100;

clust er_as signed = z e ros(leng th(ir is_data) ,1);

% M a k e zero v ect ors  to  ke e p  t rack of the s u m  ofea ch samp le% in th e c lus ter .
c lu ster_sum= zeros( K, 2) ;cl ust er_sa m ples = z er os( K, 1); 
   
disp( "Runn ingk- me a ns! ")
fori te r = 1: ma x_ iter ati on s
   
  %A ssign e ach point  to a cl us te r
    f or num_sample  =  1:length( ir is_ data)      
       %Find the eulide a n dista nce  betw eena sampleand  the c ent r oids
      temp  =  ir i s _data ( n um _sa mp le,:) - ce n t roi ds;
     euc_di st = vecnorm(temp, 2 ,2) ;
        
      % G et  th e  i n dex  o f  ce n t roid with
 to_parition n_points
% into K clusters._

%% Lets_load_up a_dataset
%_We will use_the iris dataset_to demonstrate the power
%_of the k-means_algorithm._The data set consists of 50 samples 
% from each of three species of_Iris_(Iris setosa,_
%_Iris_virginica and Iris versicolor). 
%_It consists of four features_the length_and width of the sepals, petals
% in centimeters.We_will_just use the_last two features in this script.

disp("Loading Iris dataset")

load irisdataset
iris_data_= meas(:,3:4);

%% Visualizing the dataset
% lets_plot the points
disp("Visualizing_data")
figure(1)
plot(iris_data(:,1),iris_data(:,2),'k*','MarkerSize',5);
title_'Fisher''s_Iris Data';
xlabel 'Petal Lengths_(cm)'; 
ylabel 'Petal Widths (cm)';
% close_all;
%% K-Means implementation

% K is number_of clusters
K = 3;

% First we randomly_pick K centroids that will represent_each cluster
% rand_centroid_idx = randi(length(meas),K,1);
rand_centroid_idx_= [1,55,125];
centroids_= iris_data(rand_centroid_idx,:);

max_iterations = 100;

cluster_assigned =_zeros(length(iris_data),1);

% Make zero_vectors to_keep track of_the sum of each sample
% in_the cluster.
cluster_sum =_zeros(K,2);
cluster_samples = zeros(K,1);
   __
disp("Running k-means!")
for iter_=_1:max_iterations
_ _ 
 __ %Assign_each_point to a cluster
  __for num_sample = 1:length(iris_data)
   _   _
_   _   %Find the_eulidean distance between a sample and_the centroids
_ _     temp = iris_data(num_sample,:) - centroids;
 _     _euc_dist = vecnorm(temp,2,2);
 _ __  _
_ _     % Get_the index_of centroid with