using plot in k-means

18 views (last 30 days)
yoga z
yoga z on 5 Jun 2013
Commented: Kawther on 30 Nov 2014
i try to applly k-means using data like this:
x=[ 3.4600 3.8700 3.6100 1.0000 1.0000 2.0000 ]
[ 3.0900 3.3400 3.2200 2.0000 0 1.0000]
[ 3.3300 3.0300 3.1900 1.0000 1.0000 1.0000]
[ 3.5600 3.3400 3.4900 2.0000 2.0000 1.0000]
[ 3.2600 3.1500 3.2000 1.0000 0 0]
[ 3.1900 3.3600 3.3700 1.0000 0 1.0000]
[ 3.1600 3.2100 3.1400 1.0000 1.0000 1.0000]
[ 3.7600 3.6200 3.5800 0 1.0000 1.0000]
[ 3.0900 3.1500 2.9300 1.0000 1.0000 1.0000]
[ 3.6300 3.6900 3.5600 0 0 1.0000]
[ 3.0700 3.0800 2.8600 0 1.0000 1.0000]
and i write this code in matlab
clear;
clc;
x=xlsread('test.xlsx',1,'C3:H159');
k=3;
p=200;
opts = statset('Display','final');
%[idx,ctrs,sumd] = kmeans(x,k,'Distance','city','Replicates',p,'Options',opts,'start','uniform','emptyaction','drop');
[idx,ctrs,sumd] = kmeans(x,k,'Distance','city','Replicates',p,'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(x(idx==3,1),x(idx==3,2),'g.','MarkerSize',12)
plot(ctrs(:,1),ctrs(:,2),'kx','MarkerSize',12,'LineWidth',2)
plot(ctrs(:,1),ctrs(:,2),'ko','MarkerSize',12,'LineWidth',2)
legend('Cluster 1','Cluster 2','Cluster 3','Centroids','Location','NW');
but results are illustrated using plot function is irregular, in the sense that the object is not clustered with either. any one can help me to improve this code, thanx b4
  2 Comments
Jing
Jing on 5 Jun 2013
What are you trying to do with the PLOT? Show the distribution of your data? If so, you need to first determine which dimensions you want to show the distribution in, because you have 6 dimensions in your data, it's impossible to show them all in one plot without reduce the dimensions.
yoga z
yoga z on 8 Jun 2013
thank you for your answer Jing, i just want to show the 3 cluster with their centroid, what i should do?

Sign in to comment.

Answers (3)

Jing
Jing on 9 Jun 2013
Like I said above, first of all, you need to decide which dimensions you want to show your clusters. You can't show the clusters in 6 dimension. Say, you want to show in the first two dimension, then your code is right for that. The reason you find 'the plot is irregular', may be the first two dimension is far from enough to determine the centroid. To show more dimension in a figure, you may also use plot3 to include 3 dimensions:
dm=[1,2,3];% draw first 3 dimension
plot3(x(idx==1,dm(1)),x(idx==1,dm(2)),x(idx==1,dm(3)),'r.','MarkerSize',12);
hold on;
plot3(x(idx==2,dm(1)),x(idx==2,dm(2)),x(idx==2,dm(3)),'b.','MarkerSize',12);
plot3(x(idx==3,dm(1)),x(idx==3,dm(2)),x(idx==3,dm(3)),'g.','MarkerSize',12);
plot3(ctrs(:,dm(1)),ctrs(:,dm(2)),ctrs(:,dm(3)),'ko','MarkerSize',12,'LineWidth',2);
legend('Cluster 1','Cluster 2','Cluster 3','Centroids');
hold off;
  1 Comment
Tom Lane
Tom Lane on 9 Jun 2013
You may also want to consider the gscatter function for plotting in two dimensions. It just simplifies some code, but it does not deal with the issue that Jing points out.

Sign in to comment.


Kawther
Kawther on 30 Nov 2014
What if i wanted to plot 4 clusters. should i plot in 4 dimensions? if yes, how can i plot?

Kawther
Kawther on 30 Nov 2014
Edited: Kawther on 30 Nov 2014
Dear All.
Considering that the clusters represent decision region for each sent symbol, how can i determine the decision region, and how can i find the bet error rate for the clusters resulting from the Kmean?
Thank you Kawther Hamad,
  1 Comment
Kawther
Kawther on 30 Nov 2014
Can i consider the originally sent data as a training data and resend data again and consider it and a test data and use them to find the bet error rate?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!