My K-means program has malfunctionality; please help me to run it properly

2 vues (au cours des 30 derniers jours)
Hello everyone,
I wrote some scripts for K-means clustering, but sometimes it is not able to assign some points to its correct cluster. As it seems, there are some problems that I'm not to find them and revise them. I put my scripts here and need your idea to rearrange the program for running appropriately. In addition, the data of these codes were attached, and you can find it at the bottom of this post.
clc;
clear;
close all;
load fisheriris
ClustersNumber=2;
X=meas(:,3);
Y=meas(:,4);
Random=randi([1 numel(X)],ClustersNumber,1);
for i=1:ClustersNumber
Mean.X(i)=X(randi(Random(i)));
Mean.Y(i)=Y(randi(Random(i)));
end
iteration=0;
while 1
% The distance of each point from the mean of each cluster
if iteration>100
for j=1:numel(X)
for i=1:ClustersNumber
Cluster.Distance(i,j)=sqrt((X(j)-Mean.X(i))^2+(Y(j)-Mean.Y(i))^2);
Cluster.Distance(Cluster.Distance==0)=NaN;
end
Min(j)=min(Cluster.Distance(:,j));
end
%%Assigning the points to each nearest cluster respecting the means
for i=1:numel(X)
if Cluster.Distance(1,i)==Min(i)
Class(1).data(i)=Min(i);
Class(1).X(i)=X(i);
Class(1).Y(i)=Y(i);
else
Class(2).data(i)=Min(i);
Class(2).X(i)=X(i);
Class(2).Y(i)=Y(i);
end
end
% Updating the means
for i=1:ClustersNumber
Mean.X(i)=mean(Class(i).X(Class(i).X~=0));
Mean.Y(i)=mean(Class(i).Y(Class(i).Y~=0));
end
break;
end
iteration=iteration+1;
end
scatter(Class(1).X,Class(1).Y,'+'); hold on; scatter(Class(2).X,Class(2).Y,'s');
Thanks
  2 commentaires
Image Analyst
Image Analyst le 12 Avr 2015
All I get is this:
>> load fisheriris
Error using load
Unable to read file 'fisheriris'. No such file or directory.
How do you think we can solve that so we can help you? Please read this.
Federico Frics
Federico Frics le 13 Avr 2015
Modifié(e) : Federico Frics le 13 Avr 2015
Thanks for your response. The fisheriris is a data incorporated into matlab directory by default. However, if you did'nt manage to find the data, I attached the data to the first post for you.
Thanks

Connectez-vous pour commenter.

Réponse acceptée

Federico Frics
Federico Frics le 13 Avr 2015
Modifié(e) : Federico Frics le 13 Avr 2015
Hi there,
As no one was not able to help me, I found where the problem is, and revised it.Therefore, I place my final codes here to help other people.
clc; clear; close all; load fisheriris ClustersNumber=2; X=meas(:,3); Y=meas(:,4); Random=randi([1 numel(X)],ClustersNumber,1);
%%Generate the initial clusters randomly
Class(1).X=X(1:(randi([1 80],1)));
Class(1).Y=Y(1:numel(Class(1).X));
Class(2).X=X(numel(Class(1).X)+1:end);
Class(2).Y=Y(numel(Class(1).X)+1:end);
% Determine the mean of each initial cluster for k=1:ClustersNumber
Mean.X(k)=mean(Class(k).X);
Mean.Y(k)=mean(Class(k).Y);
end
%% Main loop iteration=0;
while 1
if iteration>100
% The distance of each point from the mean of the clusters
for j=1:numel(X)
for i=1:ClustersNumber
Cluster.Distance(i,j)=sqrt((X(j)-Mean.X(i))^2+(Y(j)-Mean.Y(i))^2);
Cluster.Distance(Cluster.Distance==0)=NaN;
end
% Compute the minimum distance of each point from the classes
Min(j)=min(Cluster.Distance(:,j));
end
%%Assign the points to each nearest cluster respecting the means
for i=1:numel(X)
if Cluster.Distance(1,i)==Min(i)
NewClass(1).data(i)=Min(i);
NewClass(1).X(i)=X(i);
NewClass(1).Y(i)=Y(i);
elseif Cluster.Distance(2,i)==Min(i)
NewClass(2).data(i)=Min(i);
NewClass(2).X(i)=X(i);
NewClass(2).Y(i)=Y(i);
end
end
% Update the means
for k=1:ClustersNumber
NewClass(k).X( NewClass(k).X==0)=[];
NewClass(k).Y( NewClass(k).Y==0)=[];
Mean.X(k)=mean(NewClass(k).X);
Mean.Y(k)=mean(NewClass(k).Y);
end
break;
end
iteration=iteration+1;
end
scatter(NewClass(1).X,NewClass(1).Y,'+'); hold on; scatter(NewClass(2).X,NewClass(2).Y,'s'); scatter(Mean.X(1),Mean.Y(1),'*'); scatter(Mean.X(2),Mean.Y(2));

Plus de réponses (1)

Federico Frics
Federico Frics le 13 Avr 2015
You can find the data in the first post. Thanks

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by