我正在MATLAB中使用K-means进行聚类。正如你可能知道的用法如下:在MATLAB中的Kmeans聚类中获取最接近数据点的中心的索引
[IDX,C] = kmeans(X,k)
其中IDX给出了在X每个数据点的簇号,和C给出了每个cluster.I的重心需要得到的索引(行号实际数据集X)与质心最近的数据点。有谁知道我该怎么做? 谢谢
我正在MATLAB中使用K-means进行聚类。正如你可能知道的用法如下:在MATLAB中的Kmeans聚类中获取最接近数据点的中心的索引
[IDX,C] = kmeans(X,k)
其中IDX给出了在X每个数据点的簇号,和C给出了每个cluster.I的重心需要得到的索引(行号实际数据集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 implementation。
事实上,如果我理解你的话,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
+1正是我要发布的内容。一个小编辑,我认为应该是:`C(iCluster,:)`而不是`C(iCluster)` – Amro 2010-12-09 16:06:45