필터 지우기
필터 지우기

kmeans 1 d data

조회 수: 13 (최근 30일)
tusu
tusu 2015년 4월 11일
댓글: Image Analyst 2020년 4월 17일
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