I'm trying to build implementation code for k-means algorithm by using matlab. I'm learning and new to use matlab here. Somehow I built the implementation code for k-means algorithm by googling watching youtube of matlab functions. I set up the initial 3 initial centroids and have iris datasets, and those three centroids goes to right direction to make 3 clusters when I checked it. However, I don't really understand and can't find the source from web that I want. Can anybody help me out how to draw 2D PCA scatter plots with each 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),'.');**
At the end of three lines in the code, it gave me an error to draw scatter in PCA. I'm trying to draw reduced 2D PCA scatter plots for each clusters with different color such as rgb color. What is my problem? and Can anybody help me to figure this out for me? This might be big help to understand and learn matlab for me.
Thanks..