matlab - 如何使用缩减后的数据——主成分分析的输出

标签 matlab image-processing pca dimensionality-reduction

我发现很难将理论与实现联系起来。如果能帮助我了解我的理解错误之处,我将不胜感激。

符号 - 粗体大写矩阵和粗体小写字母向量

<子> **X**<code>n</code> 上的数据集观察,每一个 <code>d</code>变量。因此,鉴于这些观察到的 <code>d</code>维数据向量,<code>m</code>维主轴是 **w**<sub>j</sub> ,对于 j {1,...,m} 其中<code>m</code>是目标维度。

<code>m</code>观察到的数据矩阵的主要成分将是 **Y = X W** 其中矩阵 **Y** in ℝ<sup> n x m</sup> ,矩阵 **X** in ℝ<sup> n x d</sup> 和矩阵 **W** in ℝ<sup> d x m</sup>

**W** 的列为 <code>m</code> 形成一个正交基础特征和输出 **y**<sub>n</sub> 是最小化平方重构误差的主成分投影:

∑<sub>n</sub> ||**x**<sub>n</sub> - **x̂**<sub>n</sub>||<sup>2</sup>
的最优重建 **x**<sub>n</sub> **x̂**<sub>n</sub> = **W*y**<sub>n</sub> 给出.

数据模型是

X(i,j) = A(i,:)*S(:,j) + noise

其中 PCA 应该在 X 上完成以获得输出 S。S 必须等于 Y。

问题一:缩减后的数据 Y 不等于模型中使用的 S。我的理解哪里错了?

问题2:如何重构使得误差最小?

请帮忙。谢谢。

   clear all
clc
n1 = 5; %d dimension
n2 = 500; % number of examples

ncomp = 2; % target reduced dimension
%Generating data according to the model
% X(i,j) = A(i,:)*S(:,j) + noise
Ar = orth(randn(n1,ncomp))*diag(ncomp:-1:1);
T = 1:n2;
%generating synthetic data from a dynamical model
S = [ exp(-T/150).*cos( 2*pi*T/50 )
       exp(-T/150).*sin( 2*pi*T/50 ) ];
% Normalizing to zero mean and unit variance
S = ( S - repmat( mean(S,2), 1, n2 ) );
S = S ./ repmat( sqrt( mean( Sr.^2, 2 ) ), 1, n2 );
Xr = Ar * S;
Xrnoise = Xr + 0.2 * randn(n1,n2);

h1 = tsplot(S);


    X = Xrnoise;

XX = X';
[pc, ~] = eigs(cov(XX), ncomp);
Y = XX*pc;

更新 [8 月 10 日]

根据答案,这里是完整的代码

 clear all
clc
n1 = 5; %d dimension
n2 = 500; % number of examples

ncomp = 2; % target reduced dimension
%Generating data according to the model
% X(i,j) = A(i,:)*S(:,j) + noise
Ar = orth(randn(n1,ncomp))*diag(ncomp:-1:1);
T = 1:n2;
%generating synthetic data from a dynamical model
S = [ exp(-T/150).*cos( 2*pi*T/50 )
       exp(-T/150).*sin( 2*pi*T/50 ) ];
% Normalizing to zero mean and unit variance
S = ( S - repmat( mean(S,2), 1, n2 ) );
S = S ./ repmat( sqrt( mean( S.^2, 2 ) ), 1, n2 );
Xr = Ar * S;
Xrnoise = Xr + 0.2 * randn(n1,n2);

    X = Xrnoise;

XX = X';
[pc, ~] = eigs(cov(XX), ncomp);
Y = XX*pc; %Y are the principal components of X' 
%what you call pc is misleading, these are not the principal components
%These Y columns are orthogonal, and should span the same space 
%as S approximatively indeed (not exactly, since you introduced noise).

%If you want to reconstruct 
%the original data can be retrieved by projecting 
%the principal components back on the original space like this:
Xrnoise_reconstructed = Y*pc';

%Then, you still need to project it through 
%to the S space, if you want to reconstruct S
S_reconstruct = Ar'*Xrnoise_reconstructed';


plot(1:length(S_reconstruct),S_reconstruct,'r')
hold on
 plot(1:length(S),S)

情节是plot这与答案中显示的非常不同。 S 只有一个组件与 S_reconstructed 的组件完全匹配。源输入S的整个原始二维空间不应该被重构吗? 即使我切断噪声,也只有 S 的一个分量被完全重构。

最佳答案

我看到没有人回答你的问题,所以这里是:

您在 Y 中计算的是 X' 的主成分(您所说的 pc 具有误导性,这些不是主成分).这些 Y 列是正交的,并且应该大致跨越与 S 相同的空间(不完全是,因为你引入了噪音)。

如果你想重构Xrnoise,你必须看看理论(例如here)并正确应用它:可以通过将主成分投影回原始空间来检索原始数据像这样:

Xrnoise_reconstructed = Y*pc'

那么,如果要重构S,还需要通过pinv(Ar)*Xrnoise_reconstructed进行改造。

很适合我: Xrnoise and reconstruction

更新 [8 月 10 日] 的回答:(8 月 12 日编辑)

您的 Ar 矩阵没有定义标准正交基,因此,转置 Ar' 不是反向变换。因此,我之前提供的答案是错误的。答案已在上面更正。

关于matlab - 如何使用缩减后的数据——主成分分析的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38690382/

相关文章:

math - 平滑局部吊顶功能

machine-learning - sklearn多类SVM函数

matlab - 在matlab中进行三角矩阵向量乘法的最快方法?

matlab - Matlab 中的噪声消除

自定义颜色的 Matlab 图

c++ - 使用 gcc 4.6.1 通过 mex 启用 AVX 指令

java - 伪代码非极大值抑制

python - 在新的更大图像上复制图像

python - 在 Python 中将回归线和椭圆添加到 3D 散点图

machine-learning - 带 make_pipeline 的 StandardScaler