PCA在散点图上无法获得颜色

时间:2018-11-25 10:42:08

标签: python matplotlib pca

我自己在做一个迷你项目。我正在PCA尝试此操作。在绘制图表之后,我似乎无法将颜色显示出来。这些是我的代码的以下步骤。在此之前,我已经缩放并转换了数据。缩放后,我做了一个PCA。这些是步骤。第一:

from sklearn.decomposition import PCA
pca= PCA(n_components= 2)
pca.fit(scaled_data)

x_pca= pca.transform(scaled_data)

principaldf= pd.DataFrame(data=x_pca, 
                      columns=['principal component 1', 
                              'principal component 2'])

此后,我合并了两个数据框并得到了它。

new_df= principaldf.join(df_features)
new_df.head()

enter image description here

接下来,我尝试使用以下代码绘制图形:

color= ['r','g']
plt.scatter(x_pca[:, 0], x_pca[:, 1],
     edgecolor='none', alpha=0.5, c= color)
plt.xlabel('component 1')
plt.ylabel('component 2')

我收到此错误

  

V alueError:'c'参数包含2个元素,不适用于   与大小为261的“ x”一起使用,大小为261的“ y”一起使用。

enter image description here

任何人都可以提供建议或帮助吗?希望我的问题很清楚。谢谢!

1 个答案:

答案 0 :(得分:1)

您可以尝试以下操作:

将数值分配给import subprocess command = ["curl", "-k", "--silent", "https://gitlab.myurl.com/api/v4/groups?page=1&per_page=1&simple=yes&private_token=mytoken&all?page=1&per_page=1"] output = subprocess.check_output(command)

Gender

然后使用颜色和cmap绘制图:

new_df['Gender'] = new_df['Gender'].replace({'Male':0, 'Female':1})

当您传递plt.scatter(x_pca[:, 0], x_pca[:, 1], edgecolor='none', alpha=0.5, c=new_df['Gender'], cmap='RdYlGn') 之类的2项列表时,它不知道哪个点应该是哪种颜色

相关问题