Kmeans plot centroid error

2 次查看(过去 30 天)
Sophie Lis
Sophie Lis 2018-8-28
回答: KSSV 2018-8-28
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

回答(1 个)

KSSV
KSSV 2018-8-28
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

类别

Help CenterFile Exchange 中查找有关 Cluster Analysis and Anomaly Detection 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by