Determination of data points in each cluster of K-means algorithm
Show older comments
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
KALYAN ACHARJYA
on 23 May 2021
Edited: KALYAN ACHARJYA
on 23 May 2021
Can you show me the result clustered image?
Learner
on 23 May 2021
Learner
on 23 May 2021
Accepted Answer
More Answers (1)
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);
Categories
Find more on k-Means and k-Medoids Clustering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!