首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >scikit learn中的PCA投影与重建

scikit learn中的PCA投影与重建
EN

Stack Overflow用户
提问于 2016-04-12 15:48:53
回答 2查看 29.2K关注 0票数 26

我可以通过下面的代码在scikit中执行主成分分析: X_train有279180行和104列。

代码语言:javascript
复制
from sklearn.decomposition import PCA
pca = PCA(n_components=30)
X_train_pca = pca.fit_transform(X_train)

现在,当我想要将特征向量投影到特征空间时,我必须执行以下操作:

代码语言:javascript
复制
""" Projection """
comp = pca.components_ #30x104
com_tr = np.transpose(pca.components_) #104x30
proj = np.dot(X_train,com_tr) #279180x104 * 104x30 = 297180x30

但我对这一步犹豫不决,因为Scikit documentation说:

数组:components_,n_components,n_features

特征空间中的主轴,表示数据中最大方差的方向。

在我看来,它似乎已经被投影了,但当我检查源代码时,它只返回特征向量。

什么是正确的方式如何投射它?

最终,我的目标是计算重建的均方误差。

代码语言:javascript
复制
""" Reconstruct """
recon = np.dot(proj,comp) #297180x30 * 30x104 = 279180x104

"""  MSE Error """
print "MSE = %.6G" %(np.mean((X_train - recon)**2))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-12 16:37:27

你可以做到

代码语言:javascript
复制
proj = pca.inverse_transform(X_train_pca)

这样你就不必担心如何做乘法了。

pca.fit_transformpca.transform之后,您获得的是通常称为每个样本的“负载”,这意味着您需要使用components_ (特征空间中的主轴)的线性组合来最好地描述每个组件的多少。

你瞄准的投影又回到了原始的信号空间。这意味着您需要使用组件和加载返回到信号空间。

因此,这里有三个步骤来消除歧义。这里,您一步一步地了解了使用PCA对象可以做什么,以及它是如何实际计算的:

  1. pca.fit估计分量(在居中的Xtrain上使用奇异值分解):

from sklearn.decomposition import PCA import numpy as np from numpy.testing import assert_array_almost_equal #这个变量应该是X_train而不是Xtrain吗?X_train = np.random.randn(100,50) pca = PCA(n_components=30) pca.fit(X_train) U,S,VT = np.linalg.svd(X_train - X_train.mean(0)) assert_array_almost_equal(VT:30,VT

  1. pca.transform会按照您所描述的那样计算负载

X_train_pca = pca.transform (X_train ) X_train_pca2 =(X_train- pca.mean_).dot(pca.components_.T) assert_array_almost_equal(X_train_pca,.dot

  1. pca.inverse_transform可获得您感兴趣的信号空间中分量的投影

X_projected = pca.inverse_transform(X_train_pca) X_projected2 = X_train_pca.dot(pca.components_) + pca.mean_ assert_array_almost_equal(X_projected,pca.components_

现在可以评估投影损失。

代码语言:javascript
复制
loss = np.sum((X_train - X_projected) ** 2, axis=1).mean()
票数 46
EN

Stack Overflow用户

发布于 2019-06-10 20:39:26

在@eickenberg的帖子上,这里是如何对数字图像进行pca重建:

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn import decomposition

n_components = 10
image_shape = (8, 8)

digits = load_digits()
digits = digits.data

n_samples, n_features = digits.shape
estimator = decomposition.PCA(n_components=n_components, svd_solver='randomized', whiten=True)
digits_recons = estimator.inverse_transform(estimator.fit_transform(digits))

# show 5 randomly chosen digits and their PCA reconstructions with 10 dominant eigenvectors
indices = np.random.choice(n_samples, 5, replace=False)
plt.figure(figsize=(5,2))
for i in range(len(indices)):
    plt.subplot(1,5,i+1), plt.imshow(np.reshape(digits[indices[i],:], image_shape)), plt.axis('off')
plt.suptitle('Original', size=25)
plt.show()
plt.figure(figsize=(5,2))
for i in range(len(indices)):
    plt.subplot(1,5,i+1), plt.imshow(np.reshape(digits_recons[indices[i],:], image_shape)), plt.axis('off')
plt.suptitle('PCA reconstructed'.format(n_components), size=25)
plt.show()

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36566844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档