How to K-means Cluster?

Hi, I have a 732x29 matrix and I would like to cluster the data using kmeasn clustering. The matrix has two clusters (2 classes) that are either 1 or 2 and the 1 or 2 is in the 27th column f the matrix. Here is what I have so far:
fid=fopen('all_classes_withns_dmjd.txt');
myimport=textscan(fid,'%s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f','Headerlines',1);
fclose(fid);
X=cell2mat(myimport(:,2:29));
This works and it imports the code. What do I do to cluster it? I selected the classes like so:
mydata(1:1016,:);
X= mydata(1:1016,1);
Y= mydata(1:1016,2);
scatter(X,Y)
But I think there is a better way, because this way, MATLAB will not automatically make the data 2 clusters.
I tried the code in the website like so, but it does not work! Why?
fid=fopen('all_classes_withns_dmjd.txt');
myimport=textscan(fid,'%s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f','Headerlines',1);
fclose(fid);
X=cell2mat(myimport(:,2:29));
opts = statset('Display','final');
[idx,C] = kmeans(X,2,...
'Distance','city',...
'Replicates',5,...
'Options',opts);
plot(X(idx==1,1),X(idx==1,2),'r.','MarkerSize',12)
hold on
plot(X(idx==2,1),X(idx==2,2),'b.','MarkerSize',12)
plot(C(:,1),C(:,2),'kx',...
'MarkerSize',12,'LineWidth',2)
plot(C(:,1),C(:,2),'ko',...
'MarkerSize',12,'LineWidth',2)
legend('Cluster 1','Cluster 2','Centroids',...
'Location','NW')
Thanks!

6 Comments

Z
Z on 24 Jun 2013
Edited: Z on 24 Jun 2013
I also tried removing the centers like so:
IDX = kmeans(X,2);
plot(X(idx==1,1),X(idx==1,2),'r.','MarkerSize',12)
hold on
plot(X(idx==2,1),X(idx==2,2),'b.','MarkerSize',12)
legend('Cluster 1','Cluster 2','Centroids',...
'Location','NW')
And there is an error with the first line
What happens instead of it working?
I get this instead:
kmeans
Attempt to execute SCRIPT kmeans as a function:
C:\Users\Ziba\Documents\MATLAB\caltech\kmeans.m
Error in kmeanscentroid (line 8)
[idx,ctrs] = kmeans(X,2,...
That file (C:\Users\Ziba\Documents\MATLAB\caltech\kmeans.m) is on your matlab path and it is getting called instead of the matlab "kmeans" function. Rename the kmeans.m file in the folder to something else.
Z
Z on 24 Jun 2013
Thanks for telling me! But I get the same error... This particular line of code:
IDX= kmeans (X,2) OR idx kmeans (X,2)
are always giving me errors no matter how much I play around with them.
At the MATLAB command prompt use the command
which kmeans
If it gives you a result which is not under a MATLAB toolbox directory then the kmeans that it finds is interfering with the toolbox kmeans and you must rename the kmeans that it does find.
Note: kmeans is part of the Statistics toolbox so you must have that installed and licensed.

Sign in to comment.

 Accepted Answer

Z
Z on 1 Jul 2013

0 votes

The reason why the code was not working was the way I plotted it. Come on guys.

1 Comment

how did you plot it so you could get the answer ?

Sign in to comment.

More Answers (0)

Products

Asked:

Z
Z
on 24 Jun 2013

Commented:

on 5 Jun 2018

Community Treasure Hunt

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

Start Hunting!