使用 K 均值聚类计算和表示质心

Computing and representing centroids with K-means clustering

我目前正忙于学校练习。练习如下

We will consider a subset of the wild faces data described in berg2005[1]. Load the wildfaces data, Data/wildfaces using the loadmat function. Each data object is a 40*40*3=4800 dimensional vector, corresponding to a 3-color 40*40 pixels image. Compute a k-means clustering of the data with K=10 clusters. Plot a few random images from the data set as well as their corresponding cluster centroids to see how they are represented.

[1] Tamara L Berg, Alexander C Berg, Jaety Edwards, and DA Forsyth. Who's in the picture. Advances in Neural Information Processing Systems, 17:137-144, 2005.

现在回答我的问题,如何计算一张图像的质心?我目前能够显示面部并计算数据集的质心。我不明白的是,我怎么知道哪些质心对应于图像 4(如我的代码示例中所用)?我是否必须为整个数据集 X 或只是 X[4] 计算质心?我现在需要采取什么步骤才能 'plot the corresponding cluster centroids to see how they are represented'?

import scipy.io as spio
import sklearn.cluster as cl
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow

faces = spio.loadmat('Data/wildfaces.mat',squeeze_me=True)
X = faces['X']

Y = cl.k_means(X,10)
centroids = Y[0]
clusters = Y[1]

imshow(np.reshape(X[4,:],(3,40,40)).T)
plt.show()

您已经有了质心。每个集群一个。

你不需要计算它们,只显示它们。

查看Y的内容