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
답변 (1개)
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
카테고리
도움말 센터 및 File Exchange에서 k-Means and k-Medoids Clustering에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!