用Kmeans聚类法计算和表示质心

2024-10-01 04:45:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在被困在学校练习。练习如下。在

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]?我现在需要采取什么步骤来“绘制相应的星团质心以查看它们是如何表示的”?在

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()

Tags: ofthetoinimportdataasmeans