对于十个集群,您将获得十个集群中心。您现在可以打印它们,也可以将它们可视化 - 这是我假设您想要做的。
import numpy as np
import matplotlib.pyplot as plt
#fake centers
centers = np.random.random((10,100,100,3))
#print centers
for ci in centers:
print(ci)
#visualize centers:
for ci in centers:
plt.imshow(ci)
plt.show()
编辑:我知道您不仅希望可视化中心,还希望可视化每个集群中的其他成员。
您可以对单个随机成员执行以下操作:
from scipy import ndimage
from sklearn.cluster import KMeans
import numpy as np
import matplotlib.pyplot as plt
import random
#PARAMS
n_clusters=10
#fake train data
original_train = np.random.random((100, 100, 100, 3)) #100 images of each 100 px,py and RGB
n,x,y,c = original_train.shape
flat_train = original_train.reshape((n,x*y*c))
kmeans = KMeans(n_clusters, random_state=0)
clusters = kmeans.fit_predict(flat_train)
centers = kmeans.cluster_centers_
#visualize centers:
for ci in centers:
plt.imshow(ci.reshape(x,y,c))
plt.show()
#visualize other members
for cluster in np.arange(n_clusters):
cluster_member_indices = np.where(clusters == cluster)[0]
print("There are %s members in cluster %s" % (len(cluster_member_indices), cluster))
#pick a random member
random_member = random.choice(cluster_member_indices)
plt.imshow(original_train[random_member,:,:,:])
plt.show()