开发者

OpenCV KMeans (K-Means) python number of output clusters issue

I'm using OpenCV's python interface to do K-Means clustering of multidimensional data (usually dimension of 7). I'm getting strange results for the clusters. When requesting n-clusters (index 0 to n) some clusters don't have points assigned to them - which results in less clusters than expected. Has someone successfully used the python K-Means implementation of OpenCV? Some user experience or advice would be most helpful.

Here is a code snippet of my python implementation:

points = cv.CreateMat(dim1, dim2, cv.CV_32FC2)
clusters = cv.CreateMat(dim1, 1, cv.CV_32SC1)
for a in range(0,dim0):
   for b in range(0,dim1):
       for c in range(0,dim2):
           #print float(list[a*dim1*dim2 + b*dim2 + c])
           cv.Set2D( points, b, c, float(list[a*dim1*dim2 + b*dim2 + c]) )
cv.KMeans2(points, numClusters, clusters, (cv.CV_TERMCRIT_EPS + cv.CV_TERMCRIT_ITER, 100000, 0.00000001), 50)

for d in range(0,dim1):
    f.wr开发者_高级运维ite(str(int(clusters[d,0])))
    f.write(' ')
    f.write('\n')

Regards,

Stefan


This can be a desirable property, and it varies from implementation to implementation.

How this happens: When initialized randomly or when using Lloyd iterations, it may well happen that a cluster loses all its objects. In MacQueen k-means it should always keep at least one object. Assume that in 1d, there are (among others) objects at 1 and 2, assigned to cluster c1. The cluster c1 has a mean of 1.5. Now if there are two other clusters whose mean move to 0.6 and 2.4, then these two objects will be reassigned, and the cluster c1 will suddenly become empty.

Why this may be desirable: assuming you don't know the best value for k beforehand, you might just decide to choose a too large k and see if some of the clusters degenerate.

Most likely this does however indicate that your data set just does not work with k-means. K-means is actually pretty picky, it is surprising how often it still works satisfactory enough. In general, k-means does not like clusters that vary in size but a close to each other. Because k-means will always split in the middle! Plus, in your particular case, k is probably way too high.

Here's a one dimensional illustration of a situation which k-means does not like: (A and B are object of their clusters; the second line indicates the true means and the middle split between the two means. k-means would then reassign and split even further to the left.

AAAAAAAAAAAAA BBBBB
      A    |    B
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜