MATLAB で K-means を使用してクラスタリングを行っています。ご存知かもしれませんが、使用法は次のとおりです。
[IDX,C] = kmeans(X,k)
ここで、IDX は X の各データ ポイントのクラスター番号を示し、C は各クラスターの重心を示します。重心に最も近いデータ ポイントのインデックス (実際のデータ セット X の行番号) を取得する必要があります。どうすればそれができるか知っている人はいますか?ありがとう
MATLAB で K-means を使用してクラスタリングを行っています。ご存知かもしれませんが、使用法は次のとおりです。
[IDX,C] = kmeans(X,k)
ここで、IDX は X の各データ ポイントのクラスター番号を示し、C は各クラスターの重心を示します。重心に最も近いデータ ポイントのインデックス (実際のデータ セット X の行番号) を取得する必要があります。どうすればそれができるか知っている人はいますか?ありがとう
@Dimaが述べた「ブルートフォースアプローチ」は次のようになります
%# loop through all clusters
for iCluster = 1:max(IDX)
%# find the points that are part of the current cluster
currentPointIdx = find(IDX==iCluster);
%# find the index (among points in the cluster)
%# of the point that has the smallest Euclidean distance from the centroid
%# bsxfun subtracts coordinates, then you sum the squares of
%# the distance vectors, then you take the minimum
[~,minIdx] = min(sum(bsxfun(@minus,X(currentPointIdx,:),C(iCluster,:)).^2,2));
%# store the index into X (among all the points)
closestIdx(iCluster) = currentPointIdx(minIdx);
end
クラスターの中心に最も近い点の座標を取得するには、次k
を使用します。
X(closestIdx(k),:)
強引なアプローチは、k-means を実行してから、クラスター内の各データ ポイントを重心と比較し、それに最も近いデータ ポイントを見つけることです。これは、matlab で簡単に実行できます。
一方、各クラスターの「中心」としてデータ ポイントを提供するk-medoidsクラスタリング アルゴリズムを試すこともできます。これがmatlabの実装です。
実際、私があなたを正しく理解していれば、kmeansはすでに答えを提供しています:
[IDX,C, ~, D] = kmeans(X,k); % D is the distance of each datapoint to each of the clusters
[minD, indMinD] = min(D); % indMinD(i) is the index (in X) of closest point to the i-th centroid