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

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

Can you show me the result clustered image?
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

C = kmedoids(___)
T = groupcounts(C)

4 Comments

Hello Sir,
With this T = groupcounts(C) I got the count of datapoints that are in the cluster.
But I want result in this format as explained below
Suppose in my cluster of ka images I have 10 images of ka and 1 image of ra and 2image of ha.So I want results in that format that this cluster has images of ka,this num of images of ra and this number of images ha
For better understanding I am aataching the output result
PFA
If you're doing deep learning OCR/classification on those characters you have to put it into a loop where you pass each character into your network and then count up (make a histogram) of how many classes come back of each type. For sample pseudocode
myHistogram = zeros(1, numberOfClasses)
imds = imageDatastore(*.png');
numImages = length(imds.Files)
for k = 1 : numImages
thisFullFileName = imds.Files{k};
thisImage = imread(thisFullFileName);
imshow(thisImage);
caption = sprintf('Image #%d of %d', k, numImages);
drawnow;
thisClass = classify(thisImage);
myHistogram(thisClass) = myHistogram(thisClass) + 1;
end
> 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.
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)

classNumbers = kmedoids(X,k)
To find how many data points are in class 1 for example
numberInClass1 = sum(classNumbers == 1);

Asked:

on 23 May 2021

Commented:

on 27 May 2021

Community Treasure Hunt

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

Start Hunting!