1

MATLAB で k-means を使用しています。これが私のコードです:

load cobat.txt;  % read the file

k=input('Enter a number: ');        % determine the number of cluster
isRand=0;   % 0 -> sequeantial initialization
            % 1 -> random initialization

[maxRow, maxCol]=size(cobat);
if maxRow<=k, 
    y=[m, 1:maxRow];
else
    % initial value of centroid
    if isRand,
        p = randperm(size(cobat,1));      % random initialization
        for i=1:k
            c(i,:)=cobat(p(i),:) ; 
        end
    else
        for i=1:k
           c(i,:)=cobat(i,:);        % sequential initialization
        end
    end

    temp=zeros(maxRow,1);   % initialize as zero vector
    u=0;
    while 1,
        d=DistMatrix3(cobat,c);   % calculate the distance 
        [z,g]=min(d,[],2);      % set the matrix g group

        if g==temp,             % if the iteration doesn't change anymore
            break;              % stop the iteration
        else
            temp=g;             % copy the matrix to the temporary variable
        end
        for i=1:k
            f=find(g==i);
            if f                % calculate the new centroid 
                c(i,:)=mean(cobat(find(g==i),:),1)
            end
        end
        c
        sort(c)
    end

    y=[cobat,g]

「cobat」は私のファイルです。ここに見えます:

65  80  55
45  75  78
36  67  66
65  78  88
79  80  72
77  85  65
76  77  79
65  67  88
85  76  88
56  76  65

「c」は、各クラスターごとの重心 (クラスターの中心) の変数です。「g」はクラスタ番号を示す変数です。問題は、重心 (c) に基づいてクラスター番号 (小さいものから大きいもの) を並べ替え/適合させたいことです。そこで、sort(c)してみますが、クラスタ番号(g)には影響しません。

sort(g) しようとすると、希望どおりにソートされません。クラスター番号が重心に基づいてソートされるようにします。例; k=3 でコードを実行すると、最終的な重心は次のようになります。

 73.0000   79.0000   70.6667 %C 1
 58.3333   73.3333   84.6667 %C 2
 36.0000   67.0000   66.0000 %C 3

ソートすると数字のクラスターも「ソート」され、

36.0000   67.0000   66.0000 %C 3
58.3333   73.3333   70.6667 %C 2
73.0000   79.0000   84.6667 %C 1

このように、数のクラスターが収まるようにします。

36.0000   67.0000   66.0000 %C 1
58.3333   73.3333   70.6667 %C 2
73.0000   79.0000   84.6667 %C 3

ソートではなくフィットなので、この行 'y=[cobat,g]' が実行されると、それも変更されます。

これは簡単に思えますが、難しいです。私は理解できませんでした。誰でもそれを解決する考えがありますか?

ありがとうございました。

4

1 に答える 1