2012-11-09 32 views
3

例如,我有9个变量和362个案例。我做了PCA计算,发现前3个PCA坐标对我来说已经足够了。如何将新点投影到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'); 

enter image description here

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

+0

你有什么'pca()'函数的文档?通常在matlab中,我使用'princomp()'。 – Isaac

+0

'Y(1,:)'和'y'在同一个方向吗? – Isaac

+0

现在,我正在尝试新版本的Matlab。有函数'princomp()'被路由到'pca()'。好吧,我会尝试旧版本,所以我需要它在旧的Matlab –

回答

7

主要谬论是在转换点,新的基础操作:

y = (W*x')'; 

维基说:

投影向量矩阵

Y = W*·Z, 

其中Y is L×N, W is M×L, Z is M×N的列,

pca()回报大小L×MW和大小的YNxL

所以,在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