0

我有一个由 kmeans 聚类的数据集。一位朋友告诉我,我可以展示代表每个聚类中心的图片。他给了我这个简短的示例代码:

for i in xrange(len(np.unique(labels))):
     this_cluster = np.where(labels == i)[0]
     fig, ax = plt.subplots(len(this_cluster))
     for im in this_cluster:
        ax.imshow(images[im])

我已经尝试过了,但它不起作用……例如,我有一个包含 20 张图片的小数据集。Kmeans 为这 20 张照片返回 50 个中心。所以我的 np.unique(labels) with (labels = kmeans.labels_?!) 等于 50...所以“i”从 0 运行到 49...我的第一个“this_cluster”看起来像这样:

[   4    8   18   19   35   37   50  135  140  146  156  214  371  506  563
  586  594  887  916  989  993 1021 1061 1105 1121 1128 1405 1409 1458 1466
 1481 1484 1505 1572 1573 1620 1784 1817 1835 1854 1945 1955 2004 2006 2054
 2135 2204 2245 2319 2321 2343 2391 2410 2414 2486 2502 2530 2594 2624 2629
 2825 2828 2833 2911 3017 3097 3245 3246 3298 3347 3493 3568 3627 3677 3701
 3789 3866 3941 3944 3969 4022 4115 4214 4215 4432 4527 4559 4594 4645 4668
 4699 4785 4797 4802 4807 4831 4892 4905 4921 4929 4932 5076 5178 5233 5249
 5318 5463 5508 5571 5621 5644 5661 5678 5690 5727 5736 5737 5755 5777 5961
 6088 6089 6107 6197 6353 6487 6500 6515 6565 6575 6601 6706 6749]

因此,如果下一个 for 开始它会中断,i=4因为只有 20 张图片,而且images[im]withim>20会让我越界……我认为“this_clusters”是从数据集中提取的描述符,由 kmeans 计算并设置为集群 0。 ..所以这不可能吧?!还是我走错路了。也许有人可以帮助我。

编辑*:

create sets

X_train_pos, X_test_pos, X_dataset_train_pos, X_dataset_test_pos  = train_test_split(X_desc_pos, dataset_pos, test_size=0.5) 
  X_train_neg, X_test_neg, X_dataset_train_neg, X_dataset_test_neg = train_test_split(X_desc_neg,  dataset_neg, test_size=0.5) 
  # merge list of array descriptor into descriptor list
  x1 = numpy.vstack(X_train_pos)
  x2 = numpy.vstack(X_train_neg)

  # compute cluster centers 
  kmeans, n_clusters = dataset_module.create_center_data(numpy.vstack((x1,x2)),numpy.vstack((X_dataset_train_pos,X_dataset_train_neg)))

计算kmeans

def create_center_data(data,dataset): 
    n_clusters = len(data)
    n_clusters = math.sqrt(n_clusters/2)
    n_clusters = int(n_clusters)
    kmeans = KMeans(init='k-means++', n_clusters=n_clusters, n_init=1)
    kmeans.fit(data)
    numpy.set_printoptions(threshold=numpy.nan)
    labels = kmeans.labels_
    for i in xrange(len(numpy.unique(labels))):
        this_cluster = numpy.where(labels == i)[0]
        fig, ax = plt.subplots(len(this_cluster))
        for im in this_cluster:
            pic = open(dataset[im], "rb")
            ax.imshow(pic)
    return kmeans, n_clusters

数据看起来像:

[[ 36.   1.   9. ...,   0.   0.   0.]
 [  0.   0.   1. ...,   0.   0.   0.]
 [  0.   0.   0. ...,   0.   0.   1.]
 ..., 
 [ 49.  26.   0. ...,  12.   4.   5.]
 [  0.   0.   0. ...,   0.   0.   0.]
 [  0.   3.   8. ...,   0.   0.   3.]]

data = 20张图片的所有描述符...

数据集是一个带有图片路径的numpy数组

问候

琳达

4

1 回答 1

1

如果您对 SIFT 描述符进行聚类,则您的聚类手段将看起来像 sift 描述符,而不是图像。

我相信您正在考虑 EigenFaces,但这与 k-means 无关。

于 2013-08-22T16:02:07.083 回答