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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], 
[1 ,0 ,1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0 ,0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0]] 

我用K均值算法從scikitlearn形成集羣,但我需要一種方式來可視化這些集羣。 這裏我對K-均值碼

cl= KMeans(n_clusters=4) 
cl.fit(d) 
cen = cl.cluster_centers_ 
lab=cl.labels_ 

我知道如何使用matplot繪製簡單圖形,但我從來沒有使用過多維。我想繪製D然後cluster_centers(質心) 情節應該是這樣的 enter image description here 是否有任何命中可以幫助我完成此任務?

+1

是什麼'D'代表什麼? – MMF

+0

你想如何繪製它? – byxor

+0

對不起,有一個錯字,無論如何這是Term Document 矩陣(TDM),它持有不同詞的頻率。 – kero

回答

2

正如@Mahdi所說,您必須使用一些dimensionality reduction才能在二維屏幕上繪製高維空間中的點。 (當然,你會丟失一些信息,但這是不可避免的。)下面是一個例子,說明如何使用PCA(但是,有不同的技術,檢查上面的參考)。

from sklearn.decomposition import PCA 
from sklearn.cluster import KMeans 
import matplotlib.pyplot as plt 
%matplotlib inline 

# d = ... (the same as in the question) 

n_clusters = 4 
cl= KMeans(n_clusters=n_clusters) 
cl.fit(d) 
cen = cl.cluster_centers_ 
lab=cl.labels_ 

pca = PCA(2) 
# reduce dimension to 2 

coords = pca.fit_transform(d) 

plt.scatter(coords[:, 0], coords[:, 1], 20, c=lab, cmap='spring') 
centroids_transformed = pca.transform(cen) 
# transform centroids coordinate to new space 

plt.scatter(centroids_transformed[:, 0], centroids_transformed[:, 1], 60, 
      c=range(n_clusters), cmap='spring', marker='v') 
# plot centroids. they are presented in the same order as labels, 
# so colors are just range 

enter image description here