Query regarding k-means clustering in MATLAB

2019-01-20 20:58发布

问题:

I have a very large amount of data in the form of matrix.I have already clustered it using k-means clustering in MATLAB R2013a. I want the exact coordinates of the centroid of each cluster formed.. Is it possible using any formula or anything else?

I want to find out the centroid of each cluster so that whenever some new data arrives in matrix, i can compute its distance from each centroid so as to find out the cluster to which new data will belong

My data is heterogeneous in nature.So,its difficult to find out average of data of each cluster.So, i am trying to write some code for printing the centroid location automatically.

回答1:

In MATLAB, use

[idx,C] = kmeans(..) 

instead of

idx = kmeans(..) 

As per the documentation:

[idx,C] = kmeans(..) returns the k cluster centroid locations in the k-by-p matrix C.



回答2:

The centroid is simply evaluated as the average value of all the points' coordinates that are assigned to that cluster.

If you have the assignments {point;cluster} you can easily evaluate the centroid: let's say you have a given cluster with n points assigned to it and these points are a1,a2,...,an. You can evaluate the centroid for such cluster by using:

centroid=(a1+a2+...+an)/n

Obviously you can run this process in a loop, depending on how your data structure (i.e. the assignment point/centroid) is organized.