Determination of data points in each cluster of K-means algorithm

32 views (last 30 days)
Hello,
How can I calculate the number of data points of each cluster of K-means ? I found the answer of counter in python, but donot know how to use such kind of commond in MATLAB. I am finding clusters using this code
clear workspace;
path = char('E:\final'); %pass to this variable your complet data set path
net=alexnet();
imds = imageDatastore(fullfile(path),'IncludeSubfolders',true, 'LabelSource', 'foldernames');
augImds=augmentedImageDatastore(net.Layers(1, 1).InputSize(1:2),imds);
idx=randperm(numel(imds.Files),30);
imgEx=readByIndex(augImds,idx);
figure;montage(imgEx.input);title('example of the dataset');
figure;
Labels=imds.Labels;
% count the number of images
numClass=numel(countcats(Labels));
% feature extraction with the pre-trained network
feature=squeeze(activations(net,augImds,'fc8'));
% conduct a principal component analysis for the dimension reduction
A=pca(feature,"Centered",true);
subplot(1,2,1);
gscatter(A(:,1),A(:,2),Labels);
subplot(1,2,2);
% perform t-sne for the dimension reduction
T=tsne(feature');
gscatter(T(:,1),T(:,2),Labels);
% perform k-means algorithm
% please note that as the result is dependent on the initial point in the algorithm, the
% result would not be same
C=kmedoids(feature',numClass,"Start","plus");
% confirm the number of images in the largest group
[~,Frequency] = mode(C);
sz=net.Layers(1, 1).InputSize(1:2);
% prepare a matrix to show the clustering result
I=zeros(sz(1)*numClass,sz(2)*Frequency,3,'uint8');
% loop over the class to display images assigned to the group
for i=1:numClass
% read the images assigned to the group
% use the function "find" to find out the index of the i-th group image
ithGroup=readByIndex(augImds,find(C==i));
% tile the images extracted above
I((i-1)*sz(1)+1:i*sz(1),1:sz(2)*numel(find(C==i)),:)=cat(2,ithGroup.input{ : });
end
figure;
imshow(I);
title('result of the image clustering using k-means after feature extraction with alexnet')
  3 Comments
Learner
Learner on 23 May 2021
Sir i want result in this format.The tabel in the below file is created manually by me.I want to do this by program itself.pls help
file attached below pls check

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 24 May 2021
Edited: Adam Danz on 24 May 2021
C = kmedoids(___)
T = groupcounts(C)
  4 Comments
Adam Danz
Adam Danz on 24 May 2021
> With this T = groupcounts(C) I got the count of datapoints that are in the cluster.
Doesn't that address your question, "How can I calculate the number of data points of each cluster of K-means" ?
It sounds like we've got an XY Problem. I'd have to look deeper into what you're doing and I don't have the time right now to do that. Hopefully ImageAnalyst's comment above can point you in the right direction.
Learner
Learner on 27 May 2021
T = groupcounts(C) worked for me..
Thanks a ton Adam Danz Sir and Imamge Analyst Sir for help and time

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 24 May 2021
classNumbers = kmedoids(X,k)
To find how many data points are in class 1 for example
numberInClass1 = sum(classNumbers == 1);

Community Treasure Hunt

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

Start Hunting!