简体   繁体   English

如何在 MATLAB 中执行 k-means 算法?

[英]How to perform k-means algorithm in MATLAB?

I'm new to matlab and I want to know how to perform k-means algorithm in MATLAB, and also I want to know how to define cluster centers when performing k means.我是 matlab 的新手,我想知道如何在 MATLAB 中执行 k 均值算法,还想知道在执行 k 均值时如何定义聚类中心。

For example, let's say I'm creating an array as given below.例如,假设我正在创建一个如下所示的数组。

m = [11.00; 10.60; 11.00; 12.00; 11.75; 11.55; 11.20; 12.10; 11.60; 11.40;...
     11.10; 11.60; 11.00; 10.15; 9.55; 9.35; 8.55; 7.65; 7.80; 8.45; 8.05]

I want to cluster the above values into 5 clusters where k = 5.我想将上述值聚类为 5 个聚类,其中 k = 5。

And I would like to take the cluster centers as 2, 5, 10, 20, 40.我想将聚类中心设为 2、5、10、20、40。

So my question is how can I define the cluster centers and perform k-means algorithm in MATLAB?所以我的问题是如何在 MATLAB 中定义聚类中心并执行 k-means 算法? Is there a specific parameter to set the cluster centers in MATLAB kmeans() function?在 MATLAB kmeans()函数中是否有设置聚类中心的特定参数?

Please help me to solve the above problems.请帮我解决上述问题。

The goal of k-means clustering is to find the k cluster centers to minimize the overall distance of all points from their respective cluster centers. k-means聚类的目标是找到k个聚类中心,以最小化所有点到各自聚类中心的总距离。

With this goal, you'd write有了这个目标,你会写

[clusterIndex, clusterCenters] = kmeans(m,5,'start',[2;5;10;20;40])

This would adjust the cluster centers from their start position until an optimal position and assignment had been found.这将从它们的起始位置调整聚类中心,直到找到最佳位置和分配。

If you instead want to associate your points in m to fixed cluster centers, you would not use kmeans , but calculate the clusterIndex directly using min如果您想将m中的点与固定聚类中心相关联,则不会使用kmeans ,而是直接使用min计算 clusterIndex

distanceToCenter = bsxfun(@minus,m,[2 5 10 20 40]);
[~, clusterIndex] = min(abs(distanceToCenter),[],2); 

ie. IE。 you calculate the difference between every point and every center, and find, for each point, the center with the minimum (absolute) distance.您计算每个点和每个中心之间的差异,并为每个点找到具有最小(绝对)距离的中心。


To plot results, you can align the points on a line and color them according to the center:要绘制结果,您可以对齐线上的点并根据中心为它们着色:

nCenters = length(clusterCenters);
cmap = hsv(nCenters);
figure
hold on
for iCenter = 1:nCenters
    plot(m(iCenter==clusterIndex),1,'.','color',cmap(iCenter,:));
    plot(clusterCenters(iCenter),1,'*','color',cmap(iCenter,:));
end

You can see a classic implementation in my post here - K-Means Algorithm with Arbitrary Distance Function Matlab (Chebyshev Distance) .你可以在我的帖子中看到一个经典的实现 - K-Means Algorithm with Arbitrary Distance Function Matlab (Chebyshev Distance)

Enjoy...享受...

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

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