我正在生成一个使用scikitlearn,numpy和matplotlib的PCA。我想知道如何标记每个点(我的数据中的行)。我找到了#34;注释"在matplotlib中,但这似乎是用于标记特定坐标,或者只是按照它们出现的顺序将文本放在任意点上。我试图从这个中抽象出来,但由于在matplot之前出现的PCA部分而挣扎。有没有办法我可以用sklearn做到这一点,而我还在制作情节,所以我不会失去与我从中得到它的行的连接? 这是我的代码:
# Create a Randomized PCA model that takes two components
randomized_pca = decomposition.RandomizedPCA(n_components=2)
# Fit and transform the data to the model
reduced_data_rpca = randomized_pca.fit_transform(x)
# Create a regular PCA model
pca = decomposition.PCA(n_components=2)
# Fit and transform the data to the model
reduced_data_pca = pca.fit_transform(x)
# Inspect the shape
reduced_data_pca.shape
# Print out the data
print(reduced_data_rpca)
print(reduced_data_pca)
def rand_jitter(arr):
stdev = .01*(max(arr)-min(arr))
return arr + np.random.randn(len(arr)) * stdev
colors = ['red', 'blue']
for i in range(len(colors)):
w = reduced_data_pca[:, 0][y == i]
z = reduced_data_pca[:, 1][y == i]
plt.scatter(w, z, c=colors[i])
targ_names = ["Negative", "Positive"]
plt.legend(targ_names, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.xlabel('First Principal Component')
plt.ylabel('Second Principal Component')
plt.title("PCA Scatter Plot")
plt.show()
答案 0 :(得分:1)