My K-means program has malfunctionality; please help me to run it properly
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2015-4-12
All I get is this:
>> load fisheriris
Error using load
Unable to read file 'fisheriris'. No such file or directory.
采纳的回答
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!