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');

在此处输入图像描述

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

4

1 回答 1

8

将点转换为新基的主要谬误正在运行:

y = (W*x')';

维基百科说:

投影向量是矩阵的列

Y = W*·Z, 

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

但尺寸和尺寸的pca()回报WL×MYNxL

因此,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
于 2012-11-09T10:37:36.360 回答