简体   繁体   English

获取最接近数据的索引指向MATLAB中Kmeans聚类中的中心点

[英]Getting the index of closest data point to the centriods in Kmeans clustering in MATLAB

I am doing some clustering using K-means in MATLAB. 我在MATLAB中使用K-means进行一些聚类。 As you might know the usage is as below: 您可能知道用法如下:

[IDX,C] = kmeans(X,k)

where IDX gives the cluster number for each data point in X, and C gives the centroids for each cluster.I need to get the index(row number in the actual data set X) of the closest datapoint to the centroid. 其中IDX给出X中每个数据点的簇号,C给出每个簇的质心。我需要得到距离质心最近的数据点的索引(实际数据集X中的行号)。 Does anyone know how I can do that? 有谁知道我怎么做到这一点? Thanks 谢谢

The "brute-force approach", as mentioned by @Dima would go as follows @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

To get the coordinates of the point that is closest to the cluster center k , use 要获得最接近群集中心k的点的坐标,请使用

X(closestIdx(k),:)

The brute force approach would be to run k-means, and then compare each data point in the cluster to the centroid, and find the one closest to it. 蛮力方法是运行k-means,然后将群集中的每个数据点与质心进行比较,找到最接近它的那个。 This is easy to do in matlab. 这在matlab中很容易做到。

On the other hand, you may want to try the k-medoids clustering algorithm, which gives you a data point as the "center" of each cluster. 另一方面,您可能想要尝试k-medoids聚类算法,该算法为您提供数据点作为每个聚类的“中心”。 Here is a matlab implementation . 这是一个matlab实现

Actually, kmeans already gives you the answer, if I understand you right: 实际上,如果我理解你,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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM