kmeans数据上的pca图

时间:2019-12-11 16:09:40

标签: python scatter-plot pca

我在Python中使用高维对数据进行了聚类(使用kmeans),之后,我想使用PCA构建散点图。但是我的情节很奇怪,我不明白为什么?我还发现PCA组件具有负值。有人可以建议如何建立正确的散点图吗?谢谢enter image description here

#Normalize data
scaler = MinMaxScaler()
new2 = pd.DataFrame(scaler.fit_transform(dd))
#Kmeans
kmeans = KMeans(n_clusters=5)
kmeans.fit(new2)
clusters = kmeans.predict(new2)
#PCA and scatter plot
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(new2)
principalDf = pd.DataFrame(data = principalComponents
             , columns = ['principal component 1', 'principal component 2'])
finalDf = pd.concat([principalDf, new2[['Cluster']]], axis = 1)

fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(1,1,1) 
ax.set_xlabel('Principal Component 1', fontsize = 15)
ax.set_ylabel('Principal Component 2', fontsize = 15)
ax.set_title('2 component PCA', fontsize = 20)
targets = ['0','1','2','3','4']
colors = ['red','blue','black','pink','green']
for target, color in zip(targets,colors):
    indicesToKeep = finalDf['Cluster'] == target
    ax.scatter(finalDf.loc[indicesToKeep, 'principal component 1']
               , finalDf.loc[indicesToKeep, 'principal component 2']
               , c = color
               , s = 50)
ax.legend(targets)
ax.grid()

0 个答案:

没有答案