Take the data out after using Kmeans Clusering

2 views (last 30 days)
Huy Cao
Huy Cao on 28 Nov 2021
Commented: Image Analyst on 28 Nov 2021
Hi, i tried to used Kmeans Clustering from an Excel files and after Clustering I dont know how to take the data of each Cluster. and i use 5 cluster. the plot show 5 cluster but only 3 colors. Please help me.Thanks
clear all
close all
clc
[num,txt,raw] = xlsread('ClusteringData.xlsx')
figure;
X=cell2mat(raw);
plot(X(:,2),X(:,1),'.');
[idx,C]= kmeans(X,5);
figure
title 'Kmeans Clustering';
xlabel 'Wing speed';
ylabel 'Power';
gscatter(X(:,2),X(:,1),idx,'bgm')
hold on
plot(C(:,2),C(:,1),'kx')
legend('Cluster 1','Cluster 2','Cluster 3','Cluster 4','Cluster 5','Cluster Centroid')

Accepted Answer

Image Analyst
Image Analyst on 28 Nov 2021
Looks right off the top of my head, though you forgot to attach 'ClusteringData.xlsx' so we can't test it with your actual data.
However I don't see gscatter having an option with exactly 4 inputs in the documentation. What is 'bgm'? Colors? Shouldn't you be specifying 5 colors, not three, like 'bgmcr' or something? If you only specify 3 it will use b for class 1 and 4, and g for class 2 and 5 making it look like there are only 3 classes, not 5. Or just leave out your 4th argument altogether.
  5 Comments
Image Analyst
Image Analyst on 28 Nov 2021
You're welcome. Thanks for Accepting the answer.
As you know the ordering of classes in kmeans is random. If you need to, or want to, reorder class numbers according to some criteria (like your x centroid value), see the attached example. Let me know if you can't figure it out.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by