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

6 ビュー (過去 30 日間)
Learner
Learner 2021 年 5 月 23 日
コメント済み: Learner 2021 年 5 月 27 日
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 件のコメント
Learner
Learner 2021 年 5 月 23 日
Sure sir,
Input Image
PFA
Learner
Learner 2021 年 5 月 23 日
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

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2021 年 5 月 24 日
編集済み: Adam Danz 2021 年 5 月 24 日
C = kmedoids(___)
T = groupcounts(C)
  4 件のコメント
Adam Danz
Adam Danz 2021 年 5 月 24 日
> 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 2021 年 5 月 27 日
T = groupcounts(C) worked for me..
Thanks a ton Adam Danz Sir and Imamge Analyst Sir for help and time

サインインしてコメントする。

その他の回答 (1 件)

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

カテゴリ

Help Center および File ExchangeParallel and Cloud についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by