Goal To implement clustering On the following tasks consider

Goal To implement clustering On the following tasks consider the data in the file \"Project4.xlsx\". file contains a large number (210,012) of length 3 vectors, each on one line. Each vector is actual the red, green, and blue intensity values of one of the pixels in an image. For your information, the image has 516 rows and 407 columns. The pixels in the file are listed row by row from top to bottom, and within each row from left to right. For example, the first pixel in the file is the uppermost left pixel in the image. The second line of the file contains the pixel to the right of that one, and so on. In this assignment, we will explore clustering methods, applying them in particular to the problem of dividing the pixels of the image into a small number of similar clusters. Consider the K-means clustering algorithm, as described in class. In particular, consider a version in which the inputs to the algorithm are: 1. The set of data to be clustered. (the vectors x1, x2, x3, ...O 2. The desired number of clusters, K. 3. Initial centroids for the K clusters. Then the algorithm proceeds by alternating: (l) assigning each instance to the class with the nearest centroid, and (2) recomputing the centroids of each class-until the assignments and centroids stop changing. Use your implementation to cluster the data in the file mentioned above, using K 8, and the initial centroids: 255 255 255 1255 0 0 0 128

Solution

clc;

%--------------- Initializing the dataset ---------------------%
filename=input(\'enter filename:\',\'s\');
% filename=project4.xlsx;
DataSet=xlsread(filename);
Dim=size(DataSet);
  

k=input(\'No Of clustres : \');
%--------------------------- 3 random centres ----------------------------%
Centre1=input(\'enter center1:\');
Centre2=input(\'enter center2:\');
Centre3=input(\'enter center3:\');

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

%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
% final centroids
fprintf(\'the centroid1:\ \');
disp(Centre1);
fprintf(\'the centroid2:\ \');
disp(Centre2);
fprintf(\'the centroid3:\ \');
disp(Centre3);
hold off
% specify the indexed color for each point
icolor = ceil((DataSet(:,k)/max(DataSet(:,k)))*256);

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

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

 Goal To implement clustering On the following tasks consider the data in the file \
 Goal To implement clustering On the following tasks consider the data in the file \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site