Python |散点图未显示

Python | Scatterplot is not showing

我正在对我的数据集执行 PCA,我可以获得正确的结果。但是当我尝试可视化 PCA 时,它没有显示。 这是我的尝试:

#Import dataset
dataset = pd.read_csv('Data.csv', names=['0','1','2','3','target'],header=0)
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values


#PCA
from sklearn.decomposition import PCA
pca = PCA(n_components = 2)
X = pca.fit_transform(X)
principalDf = pd.DataFrame(data = X, columns = ['principal component 1', 
'principal component 2'])
finalDf = pd.concat([principalDf, dataset['target']], axis = 1)

#Visualizing 
fig = plt.figure(figsize = (8,8))
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 = ['1', '2', '3', '4']
colors = ['r', 'g', 'b', 'hotpink']
for target, color in zip(targets,colors):
    indicesToKeep = finalDf['target'] == target
    ax.scatter(finalDf.loc[indicesToKeep, 'principal component 1']
           , finalDf.loc[indicesToKeep, 'principal component 2']
           , c = color
           , s = 50)
ax.legend(targets)
ax.grid()

但这不起作用,我无法弄清楚。我怎样才能解决这个问题?

您正在为 finalDf 编制索引以获取 DataFrame 的切片,其中 target 列值与列表 targets = ['1', '2', '3', '4'].[=19 中的单个 target 相同=]

由于您的target列中的值不是str类型,而是数值类型,因此没有满足此条件的数据:

'1' != 1
'2' != 2
'3' != 3
'4' != 4

因此没有绘制数据。

要获得所需的切片,而不是 targets = ['1', '2', '3', '4'],您应该使用数值作为目标:

targets = [1, 2, 3, 4]