如何为PCA新基础规划新的观点? [英] How to project a new point to PCA new basis?
问题描述
例如,我有9个变量和362个案例.我已经进行了PCA计算,发现前3个PCA坐标对我来说足够了.
For example, I have 9 variables and 362 cases. I've made PCA calculation, and found out that first 3 PCA coordinates are enough for me.
现在,我在9维结构中有了一个新点,我想将其投影到主成分系统坐标.如何获取新坐标?
Now, I have new point in my 9-dimensional structure, and I want to project it to principal component system coordinate. How to get its new coordinates?
%# 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');
如何获取投影到新的主成分基础上的新点的坐标?
How to get coordinates of a new point projected to new principal component basis?
推荐答案
正在运行的主要谬误将点转换为新的基数:
Main fallacy was in operation that converts points to new basis:
y = (W*x')';
维基百科说:
投影的矢量是矩阵的列
The projected vectors are the columns of the matrix
Y = W*·Z,
其中Y is L×N, W is M×L, Z is M×N
,
但pca()
返回大小为L×M
的W
和大小为NxL
but pca()
returns W
of size L×M
and Y
of size NxL
因此,Matlab中的正确方程为:
so, correct equation in Matlab is:
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
这篇关于如何为PCA新基础规划新的观点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!