gpt4 book ai didi

matlab - 如何投影一个新的点到PCA新的基础上?

转载 作者:太空宇宙 更新时间:2023-11-03 20:16:45 24 4
gpt4 key购买 nike

例如,我有 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');

enter image description here

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

最佳答案

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

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 和大小为 NxL 的 Y

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

关于matlab - 如何投影一个新的点到PCA新的基础上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13303300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com