kmeans 1 d data
이전 댓글 표시
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
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
2015년 4월 12일
답변 (2개)
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
tusu
2015년 4월 12일
Image Analyst
2015년 4월 12일
If you don't know the boundaries, then how can you say what kmeans is doing is wrong? Give some examples of how kmeans is clustering them so we can see that it's just giving random classifications as you claimed.
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
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
2020년 4월 17일
0 개 추천
data=data'
Then:
idx = kmeans(data, 3 );
카테고리
도움말 센터 및 File Exchange에서 k-Means and k-Medoids Clustering에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!