SKM*_*SKM 7 matlab image-processing pca dimensionality-reduction
我发现很难将理论与实施联系起来.我很感激帮助知道我的理解错误.
符号 - 粗体大写的矩阵和粗体字母小写字母的向量
是一个数据集
观察,每个
变量.所以,鉴于这些观察
- 维数据向量,
- 维主轴是
,为
在
哪里
是目标维度.
列
形成一个正交的基础
功能和输出
是最小化平方重建误差的主成分投影:
数据模型是
X(i,j) = A(i,:)*S(:,j) + noise
Run Code Online (Sandbox Code Playgroud)
其中PCA应在X上完成以获得输出S. S必须等于Y.
问题1:减少的数据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;
Run Code Online (Sandbox Code Playgroud)
更新[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)
Run Code Online (Sandbox Code Playgroud)
情节是
这与答案中显示的非常不同.只有S的一个组件与S_reconstructed的组件完全匹配.不应该重建源输入S的整个原始二维空间吗?即使我切断了噪音,那么S的一个组成部分也会被精确地重建.
我发现没有人回答你的问题,所以这里是:
您计算的内容Y是主成分X'(您所说的pc具有误导性,这些不是主成分)。这些Y列是正交的,并且应该近似地跨越相同的空间S(不完全是,因为您引入了噪声)。
如果你想重建Xrnoise,你必须查看理论(例如这里)并正确应用它:可以通过将主成分投影回原始空间来检索原始数据,如下所示:
Xrnoise_reconstructed = Y*pc'
Run Code Online (Sandbox Code Playgroud)
pinv(Ar)*Xrnoise_reconstructed那么,如果要重构 ,还需要通过 进行改造S。
对更新 [8 月 10 日] 的答复:(8 月 12 日编辑)
您的Ar矩阵没有定义正交基,因此转置Ar'不是逆变换。因此,我之前提供的答案是错误的。答案已在上面更正。