kmeans 1 d data

7 次查看(过去 30 天)
tusu
tusu 2015-4-11
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 个评论
Geoff Hayes
Geoff Hayes 2015-4-11
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])
tusu
tusu 2015-4-12
No cluster pattern remains the same but some time say (1,1,2,3) belongs to cluster 1 some time its belongs cluster 2..I want kmeans should allocate (1,1,2,3) in cluster 1, (10,11) in cluster 2 and (13,13,17) in cluster 3 in fixed manner.

请先登录,再进行评论。

回答(2 个)

Image Analyst
Image Analyst 2015-4-12
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 个评论
Tom Lane
Tom Lane 2015-4-15
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
Image Analyst 2020-4-17
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

请先登录,再进行评论。


Javier Buldu
Javier Buldu 2020-4-17
data=data'
Then:
idx = kmeans(data, 3 );

标签

Community Treasure Hunt

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

Start Hunting!

Translated by