Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PCA——用Numpy计算缩减矩阵_Python_Numpy_Machine Learning_Pca_Eigenvector - Fatal编程技术网

Python PCA——用Numpy计算缩减矩阵

Python PCA——用Numpy计算缩减矩阵,python,numpy,machine-learning,pca,eigenvector,Python,Numpy,Machine Learning,Pca,Eigenvector,我尝试使用PCA将输入图像的大小从4096 x 4096减小到4096 x 163,同时保留其重要属性。然而,我的方法有问题,因为我得到了错误的结果。我相信这是在构建我的矩阵U时。下面列出了我的结果与正确的结果。 起始代码: # Reshape data to 4096 x 163 X_reshape = np.transpose(X_all, (1,2,3,0)).reshape(-1, X_all.shape[0]) X = X_reshape[:, :163] mean_array =

我尝试使用PCA将输入图像的大小从4096 x 4096减小到4096 x 163,同时保留其重要属性。然而,我的方法有问题,因为我得到了错误的结果。我相信这是在构建我的矩阵U时。下面列出了我的结果与正确的结果。 起始代码:

# Reshape data to 4096 x 163
X_reshape = np.transpose(X_all, (1,2,3,0)).reshape(-1, X_all.shape[0])
X = X_reshape[:, :163]

mean_array = np.mean(X, axis = 1)
X_tilde = np.reshape(mean_array, (4096,1))
X_tilde = X - X_tilde   

# Construct the covariance matrix for computing u'_i
covmat = np.cov(X_tilde.T)

# Compute u'_i, which is stored in the variable v
w, v = np.linalg.eig(covmat)

# Compute u_i from u'_i, and store it in the variable U
U = np.dot(X_tilde, v)

# Normalize u_i, i.e., each column of U
U = U / np.linalg.norm(U)
我的结果:

PC1 explains 0.08589754681312775% of the total variance
PC2 explains 0.07613195994874819% of the total variance
First 100 PCs explains 0.943423133356313% of the total variance

Shape of U: (4096, 163)
First 5 elements of first column of U: [-0.00908046 -0.00905446 -0.00887831 -0.00879843 -0.00850907]
First 5 elements of last column of U: [0.00047628 0.00048451 0.00045043 0.00035762 0.00025785]
预期成果:

PC1 explains 14.32% of the total variance
PC2 explains 7.08% of the total variance
First 100 PCs explains 94.84% of the total variance

Shape of U: (4096, 163)
First 5 elements of first column of U:  [0.03381537 0.03353881 0.03292298 0.03238798 0.03146345]
First 5 elements of last column of U:   [-0.00672667 -0.00496044 -0.00672151 -0.00759426 
-0.00543667]
我的计算一定有问题,我就是想不出是什么。如果您需要更多信息,请告诉我

我使用的证据:


在我看来,你的步骤似乎有问题。在计算特征向量和特征值之前,从输入中删除维度,因此在这一阶段有效地随机删除了大量输入,而没有任何理由

# Reshape data to 4096 x 163
X_reshape = np.transpose(X_all, (1,2,3,0)).reshape(-1, X_all.shape[0])
X = X_reshape[:, :163]
我不太明白上面调用
transpose
的意图,但我认为这并不重要。只有在计算协方差矩阵的特征向量和特征值后,才能从输入中删除维度。而且您不会明确地从数据中删除维度;截断特征向量矩阵,然后将该缩减的特征向量矩阵用于投影步骤

这种情况下的协方差矩阵应为4096x4096矩阵。特征值和特征向量将按顺序返回,最大特征值和相应的特征向量在开始处。然后可以将特征向量的数量截断为163,并创建降维投影


我可能对作业有误解,但我很确定这就是问题所在。我不愿意多说,因为这是家庭作业。

为什么不使用奇异值分解(SVD)?在任何情况下,包括MWE来生成输出。我很好奇的一件事是,为什么要对单个图像进行PCA?你在尝试实现压缩算法吗?@lbragile应该提到这是我正在学习的CS课程的作业。我不能选择我使用的方法。教授希望我们使用numpy,并找出如何从头开始实现这一点。我将编辑这篇文章,以包含我所关注的证据。@jhill515见上文comment@z.rubi,我知道教授希望你以某种方式做事。有趣的事实是,numpy.linalg.svd()返回U矩阵,这是AA'的特征向量,V矩阵是A'A的特征向量。也就是说,你有理由要求教授激发这个问题。这就是为什么我问你是在尝试实现压缩还是在为另一个学习者进行预处理降维。从某种意义上说,如果您试图将4096x4096图像缩减为具有相同行空间的图像,这似乎非常奇怪,即使是对于宠物示例也是如此。