PCA 降维后对数据画图sklearn pca

import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import torch

data=torch.rand((5,1024))
labels=torch.tensor([1,1,0,1,0])

# 创建一个随机的 PCA 模型,该模型包含两个组件
randomized_pca = PCA(n_components=2,svd_solver="randomized")
# 拟合数据
reduced_data_rpca = randomized_pca.fit_transform(data)

# 创建一个常规的 pca 模型
pca=PCA(n_components=2)
reduced_data_pca = pca.fit_transform(data)

# 画图
# 二维图像
# https://blogs.csdn.net/Discover304/article/details/121991061
colors = ['black', 'teal']
for i in range(len(colors)):
    x = reduced_data_pca[:,0][labels==i]
    y = reduced_data_pca[:,1][labels==i]
    plt.scatter(x,y,c=colors[i],s=5)
plt.legend([0,1], bbox_to_anchor=(1.05,1), loc=2, borderaxespad=0.)
plt.show()

你可能感兴趣的:(python,人工智能,深度学习)