2015-04-06 67 views
0

我正在将我的聚类中心投影到2个主要组件上,但给出的图不在我的2组数据点的正确中心位置。我的代码如下。有没有人看到我要去哪里错了? PCA很好,但集群的其中一个数据点是离开的。我会提到我的一半质心数据点是负面的。我玩过反转pca转换,真的不知道错误来自哪里。任何帮助是极大的赞赏!为什么使用Sklearn将错误的簇投影到PCA上?

import numpy as np 
import sklearn 
from sklearn.cluster import KMeans 
from sklearn.decomposition import PCA 
import matplotlib.pyplot as plt 


data = normalize(key) 

key_N=normal(key,key) 

pca=PCA(n_components=21) 
pca.fit(data[:,0:-1]) 
keytrain_T = pca.transform(data[:,0:-1]) 

kmeans = KMeans(n_clusters=2, init='k-means++', n_init=100, max_iter=300, 
      tol=0.0001, precompute_distances='auto', verbose=0, random_state=None, copy_x=True, n_jobs=1) 
kmeans.fit(data[:,0:-1]) 

centroid = cluster_centers_ 
print("The centroids:",centroid) 

# Project the cluster points to the two first principal components 
clusters = pca.fit_transform(centroid) 

print("The clusters:",clusters) 
+1

你可以发布图片吗? –

+0

我没有足够的积分... – user4476006

+0

您可以发布一个链接imgur ... –

回答

1

fit_transform其中荷兰国际集团的重心,而不是transform

+0

我需要四个最后的观点,从我的21维聚类中心。当我使用变换时,图表仍然显示相同,并且我的群集出来:''群集:[[-3.73604149e-01 -7.88968144e-02 -2.39775253e-04 -2.98255905e-03-03-O3 -5.53224119e- 03 -3.71999147e-03 -2.40050141e-03 -3.85699695e-04 6.72346604e-03 4.83367176e-03 7.86504453e-04 -3.69889640e-03 3.47965218e-04 -3.10482175e-03 -1.61504788e-03 1.68468915 e-03 -9.73136520e-04 5.83231053e-04 -8.74801939e-04 -2.84281009e-04 -2.53746653e-04]''(对于2个簇) – user4476006

+0

使用fit_transform,它变为:'簇:[[ -1.35841000e + 00 4.29736603e-16] [1.35841000e + 00 4.29736603e-16]]'' – user4476006

+0

该图有两个基本上有两个粗糙的均匀中心点。群集应该在每个中间,但是一个完全不在中心...... – user4476006