Anonymous
Anonymous

Reputation: 239

Clustering 3d scatter data using k-means clustering in matlab

I have a 3d scatter plot organized in an array. When I plot my data as a 3d scatter plot, I obtain 2 clear clusters - one smaller one on the left and one large one on the right. enter image description here

I've tried k-means clustering but I obtain these 2 clusters instead of the two that I wanted: enter image description here

Here's my code:

opts = statset('Display','final');
[idx,C] = kmeans(data,2,'Distance','cityblock',...'Replicates',5,'Options',opts);
figure;
plot3(data(idx==1,1),data(idx==1,2),data(idx==1,3),'r.','MarkerSize',12)
plot3(data(idx==2,1),data(idx==2,2),data(idx==2,3),'b.','MarkerSize',12)
plot3(C(:,1),C(:,2),C(:,3),'cx',...'MarkerSize',15,'LineWidth',3)
legend('Cluster 1','Cluster 2','Centroids',...'Location','NW')
title 'Cluster Assignments and Centroids'
hold off

How can alter my code so that I obtain the 2 clusters (one small one on the left, one large one on the right), instead of the up-down cluster that I have right now? Thanks so much!

Upvotes: 0

Views: 6004

Answers (2)

user10592680
user10592680

Reputation: 11

Transform the left-right data *10 to add weight to the variable, this should result in the cluster split you intended

Upvotes: 1

Mendi Barel
Mendi Barel

Reputation: 3677

Change to plot3 and add the C(:,3), This worked for me:

data=rand(100,3);
data=[data;2+rand(50,3)];

opts = statset('Display','final');
[idx,C] = kmeans(data,2,'Distance','cityblock','Replicates',5,'Options',opts);
figure(1);cla;gca;hold on;
plot3(data(idx==1,1),data(idx==1,2),data(idx==1,3),'r.','MarkerSize',12)
plot3(data(idx==2,1),data(idx==2,2),data(idx==2,3),'b.','MarkerSize',12)
plot3(C(:,1),C(:,2),C(:,3),'cx','MarkerSize',15,'LineWidth',3)
legend('Cluster 1','Cluster 2','Centroids','Location','NW')
title('Cluster Assignments and Centroids');
hold off

Upvotes: 0

Related Questions