如何将新点投影到PCA的新基上?

3
例如,我有9个变量和362个案例。我进行了PCA计算,并发现前3个PCA坐标对我来说足够了。
现在,我有一个新的点在我的9维结构中,我想将它投影到主成分系统坐标上。如何获得它的新坐标?
%# here is data (362x9)
load SomeData

[W, Y] = pca(data, 'VariableWeights', 'variance', 'Centered', true);

%# orthonormal coefficient matrix
W = diag(std(data))\W;

% Getting mean and weights of data (for future data)
[data, mu, sigma] = zscore(data);
sigma(sigma==0) = 1;

%# New point in original 9dim system
%# For example, it is the first point of our input data
x = data(1,:);
x = bsxfun(@minus,x, mu);
x = bsxfun(@rdivide, x, sigma);

%# New coordinates as principal components
y0 = Y(1,:); %# point we should get in result
y = (W*x')'; %# our result

%# error
sum(abs(y0 - y)) %# 142 => they are not the same point

%# plot
figure()
plot(y0,'g'); hold on;
plot(y,'r');

如何获取投影到新主成分基的新点的坐标?

你有关于 pca() 函数的文档吗?通常在 Matlab 中我使用 princomp() - Isaac
Y(1,:)y 是否朝着同一个方向? - Isaac
现在,我正在尝试新版本的Matlab。其中函数 princomp() 被路由到了 pca()。好的,我会尝试在旧版本中运行它,更何况我需要让它在旧版的Matlab中工作。 - Larry Foobar
@Isaac,是的,Y(1,:)y都是1x9 - Larry Foobar
方向,而非维度。Y(1,:) 是否近似为 y 的倍数? - Isaac
@Isaac,抱歉,我误解了。我已经更新了我的帖子。 - Larry Foobar
1个回答

8

主要谬误在于将点转换为新的基础:

y = (W*x')';

维基百科说:

The projected vectors are the columns of the matrix

Y = W*·Z, 

where Y is L×N, W is M×L, Z is M×N,

但是pca()返回大小为L×MW和大小为NxLY

因此,在Matlab中正确的等式是:

y = x*W

以下是修正后的代码:
[W, Y] = pca(data, 'VariableWeights', 'variance', 'Centered', true);
W = diag(std(data))\W;

%# Getting mean and weights of data (for future data)
[~, mu, we] = zscore(data);
we(we==0) = 1;

%# New point in original 9dim system
%# For example, it is the first point of our input data
x = data(1,:); 
x = bsxfun(@minus,x, mu);
x = bsxfun(@rdivide, x, we);

%# New coordinates as principal components
y = x*W;
y0 = Y(1,:);
sum(abs(y0 - y)) %# 4.1883e-14 ~= 0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接