kmeans 1 d data
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have data = [1, 1, 2, 3, 10, 11, 13, 13, 17]
I want to to cluster them in 3 clusters..like (1,1,2,3) in cluster 1 (10,11) in cluster 2 and (13,13,17) in cluster 3.
but when I apply idx = kmeans( data, 3 ); kmeans clusters them with random cluster index..
Is there any way to fix that?
How could I plot my clustered data ? scatter doesn't work here
2 commentaires
Geoff Hayes
le 11 Avr 2015
Tusu - please clarify what you mean by kmeans clusters them with random cluster index. Do you mean that sometimes the algorithm returns the clusters that you have defined above and other times the algorithm returns some other cluster pattern? You could try to specify the centroid starting locations of the clusters. Something like
kmeans(data,3,'Start',[2 10 13])
Réponses (2)
Image Analyst
le 12 Avr 2015
If you know the boundaries that you want in advance , then there is no reason to use kmeans . Just classify them yourself
for k = 1 length(data)
if data(k) <= 3
theClasses(k) = 1;
elseif data(k) < 13
theClasses(k) = 2;
else
theClasses(k) = 3;
end
end
4 commentaires
Tom Lane
le 15 Avr 2015
For one-dimensional data you might just sort the cluster means, and re-label the cluster numbers you got to match the sort order. The cluster numbers are just names, and aren't intended to have any real meaning.
Image Analyst
le 17 Avr 2020
Tusu:
If you want to relabel the cluster labels according to something, like for example the magnitude of the cluster centroid, see my attached demo. So,if your clusters are initially labeled
[1, 1, 2, 3, 10, 11, 13, 13, 17] % Data
[2, 2, 2, 2, 3, 3, 1, 1, 1] % Initial labels caused by randomness in kmeans() alrogithm
you can use my attached code to relabel them like
[1, 1, 2, 3, 10, 11, 13, 13, 17] % Data
[1, 1, 1, 1, 2, 2, 3, 3, 3] % Final labels based on the mean of each cluster
Demo code (you might need to run it several times to get the initial order to be NOT sequential):
data = [1, 1, 2, 3, 10, 11, 13, 13, 17]
[classLabels, centroids] = kmeans(data', 3);
classLabels' % Show in command window.
centroids' % Show in command window.
% Sort the centroids by distance from 0
[sortedCentroid, sortOrder] = sort(centroids, 'ascend')
% Instantiate new labels
newClassLabels = zeros(size(classLabels));
% Now relabel each point with the class label that we WANT it to be.
for k = 1 : length(classLabels)
originalLabel = classLabels(k);
newClassLabels(k) = find(sortOrder == originalLabel);
fprintf('For point #%d (= %2d), an original class label of %d will now be %d\n', k, data(k), originalLabel,newClassLabels(k));
end
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!