Kmeans plot centroid error

I am trying to cluster my two-dimensional data with k-means clustering, where my data is wv_prop (attached) and am plotting according to the example code provided by matlab. However,I am getting a strange result as seen in the attached figure where there are no points around the 2nd centroid. Anyone know why this might be? It looks like the idx variable contains only 1's, and no 2's.
[idx,C] = kmeans(wv_prop,2);
figure;
plot(wv_prop(idx==1,1),wv_prop(idx==1,2),'r.','MarkerSize',12)
hold on
plot(wv_prop(idx==2,1),wv_prop(idx==2,2),'b.','MarkerSize',12)
plot(C(:,1),C(:,2),'kx',...
'MarkerSize',15,'LineWidth',3)
legend('Cluster 1','Cluster 2','Centroids',...
'Location','NW')
title 'Cluster Assignments and Centroids'
hold off

Answers (1)

Try removing the outliers:
load('wv_prop.mat')
% Remove outliers
idx = abs(wv_prop(:,2) - nanmean(wv_prop(:,2))) > 3*nanstd(wv_prop(:,2));
wv_prop(idx,:) = [] ;
[idx,C] = kmeans(wv_prop,2);
figure;
plot(wv_prop(idx==1,1),wv_prop(idx==1,2),'r.','MarkerSize',12)
hold on
plot(wv_prop(idx==2,1),wv_prop(idx==2,2),'b.','MarkerSize',12)
plot(C(:,1),C(:,2),'kx',...
'MarkerSize',15,'LineWidth',3)
legend('Cluster 1','Cluster 2','Centroids',...
'Location','NW')
title 'Cluster Assignments and Centroids'
hold off

Tags

Asked:

on 28 Aug 2018

Answered:

on 28 Aug 2018

Community Treasure Hunt

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

Start Hunting!