필터 지우기
필터 지우기

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