I am trying to build implementation code for the k-mean algorithm using matlab. I am learning and new to using Matlab. Somehow I built implementation code for the k-mean algorithm by searching matlab functions on googling youtube. I set the initial 3 initial centroids and had aperture diaphragms, and these three centroids go in the right direction to make 3 clusters when I checked it. However, I really do not understand and cannot find the source from the Internet that I want. Can someone help me how to draw a PCA 2D scatterplot with different colors of three clusters?
This is my code implementation for k-mean,
clear; clc; close all;
load iris.xls
DataSet = iris;
Dim = size(DataSet);
load Iris_Initial_Centroids.xls
Centroid = Iris_Initial_Centroids;
Dim_Cen = size(Centroid);
Centroid1 = Centroid(1,:);
Centroid2 = Centroid(2,:);
Centroid3 = Centroid(3,:);
n = input('Enter the number of Iteration : ');
for i=1:1:n
count1 = 0;
Mean1 = zeros(1,4);
count2 = 0;
Mean2 = zeros(1,4);
count3 = 0;
Mean3 = zeros(1,4);
for j=1:1:Dim(1,1)
Pattern1(j)=sqrt((Centroid1(1,1)-DataSet(j,1))^2+(Centroid1(1,2)-DataSet(j,2))^2+(Centroid1(1,3)-DataSet(j,3))^2+(Centroid1(1,4)-DataSet(j,4))^2);
Pattern2(j)=sqrt((Centroid2(1,1)-DataSet(j,1))^2+(Centroid2(1,2)-DataSet(j,2))^2+(Centroid2(1,3)-DataSet(j,3))^2+(Centroid1(1,4)-DataSet(j,4))^2);
Pattern3(j)=sqrt((Centroid3(1,1)-DataSet(j,1))^2+(Centroid3(1,2)-DataSet(j,2))^2+(Centroid3(1,3)-DataSet(j,3))^2+(Centroid1(1,4)-DataSet(j,4))^2);
closestDistance = [Pattern1(j) Pattern2(j) Pattern3(j)];
minimum = min(closestDistance);
if (minimum == Pattern1(j))
count1 = count1+1;
Mean1 = Mean1 + DataSet(j,:);
else if (minimum == Pattern2(j))
count2 = count2 + 1;
Mean2 = Mean2 + DataSet(j,:);
else
count3 = count3+1;
Mean3 = Mean3 + DataSet(j,:);
end
end
end
Centroid1 = Mean1/count1;
Centroid2 = Mean2/count2;
Centroid3 = Mean3/count3;
%plot(i, Centroid1, '.');
%plot(i, Centroid2, '.');
%plot(i, Centroid3, '.');
end
**[coeff.score.latent] = pca(DataSet);
newDataSet = score(:,1:2);
plot(newDataSet(:,1),newDataSet(:,2),'.');**
, PCA. 2D PCA , , rgb. ? - ? Matlab .
..