Kmeans (Initialise centroids)

13 Ansichten (letzte 30 Tage)
Ioanna Thoma
Ioanna Thoma am 19 Sep. 2019
Kommentiert: Akira Agata am 6 Okt. 2019
I have a dataset (X) where i use k means and save the centroids in matrix ( C), then I want to use the same centroids to cluster a different dataset. Can you please tell me the syntax? I know that I have to use 'start' but I am not doing it in a correct way.

Antworten (1)

Akira Agata
Akira Agata am 20 Sep. 2019
Like this?
% Apply k-means clustering to data set X (e.g num of classes = 2), and obtain centroids C
numClass = 2;
[cluster,C] = kmeans(X,numClass);
% Calculate distance from each row of new data set X2 and C
d = pdist2(X2,C);
% Cluster the data set X2 based on the distance from the centroids C
[~,cluster2] = min(d,[],2);
  2 Kommentare
Ioanna Thoma
Ioanna Thoma am 23 Sep. 2019
I mean, by using idx=kmeans(X,k,Name,Value)
After appling k-means to the dataset I have:
[idx,centroids]=kmeans(X,5) %I need 5 clusters
so then what do I have to do?
idxnew=kmeans(X,5,'Start',centroids) is not working:/
Akira Agata
Akira Agata am 6 Okt. 2019
I think the same strategy should work. Here is an 5-cluster example :
% Sample data
X = rand(100,2); % dataset1
X2 = rand(100,2); % dataset2
% Apply k-means clustering to dataset1 (e.g num of classes = 5), and obtain centroids C
numClass = 5;
[cluster,C] = kmeans(X,numClass);
% Calculate distance from each row of new dataset (dataset2) against the centroids C
d = pdist2(X2,C);
% Clustering the dataset2 based on the centroids C
[~,cluster2] = min(d,[],2);
% Visualize the result
figure
gscatter(X2(:,1),X2(:,2),cluster2)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Statistics and Machine Learning Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by