首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在MATLAB中获取与Kmeans聚类中心最近的数据点的索引

在MATLAB中获取与Kmeans聚类中心最近的数据点的索引
EN

Stack Overflow用户
提问于 2010-12-09 23:43:29
回答 3查看 4.8K关注 0票数 5

我正在使用MATLAB中的K-means进行一些聚类。如你所知,它的用法如下:

代码语言:javascript
运行
复制
[IDX,C] = kmeans(X,k)

其中IDX给出了X中每个数据点的簇号,C给出了每个簇的质心。我需要获得最接近质心的数据点的索引(实际数据集X中的行号)。有人知道我是怎么做到的吗?谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-12-10 00:01:36

@Dima提到的“蛮力方法”如下所示

代码语言:javascript
运行
复制
%# 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最近的点的坐标,请使用

代码语言:javascript
运行
复制
X(closestIdx(k),:)
票数 5
EN

Stack Overflow用户

发布于 2010-12-09 23:48:03

暴力方法是运行k-means,然后将集群中的每个数据点与质心进行比较,并找到最接近它的数据点。在matlab中很容易做到这一点。

另一方面,您可能希望尝试k-medoids聚类算法,该算法提供一个数据点作为每个集群的“中心”。这是一个matlab implementation

票数 1
EN

Stack Overflow用户

发布于 2016-12-12 16:03:28

实际上,如果我没理解错的话,kmeans已经给了你答案:

代码语言:javascript
运行
复制
[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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4400070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档