[MATLAB] 如何使用coefficient和original data计算PCA score

原文地址:https://www.mathworks.com/matlabcentral/answers/197028-calculate-pca-score-from-coefficient-and-original-data


问:I want to do cross validation of a non-linear regression on principal components as input. Therefore, I do PCA on my training set, and use the regression coefficient to transform the standardized test set into their principal components. However in the following I found pc score!=zscore(data)*coef. Appreciate if anyone can explain to me. Thanks alot.

[coef2, score2, latent2, explained2, mu2] = pca(data_raw_ccc(:,2:end));
zdata=zscore(data_raw_ccc(:,2:end));
score3=zdata*coef2;
diff=score3-score2; % NOT EQUAL 0, WHY?

答:By default, pca centers the data around the mean, but does not scale it to unit variance.
The original data can be reconstructed by

score2*coef2

得知:matlab中pca(data)会对data进行去均值处理,然后计算。
下面是matlab的例子,用的内置数据集

load cities
[wcoeff,score,~,~,~] = pca(ratings);
cscores = ratings*wcoeff;
mean_ratings=mean(ratings,1);
mean_cscores=(ratings-mean_ratings)*wcoeff;

score!=cscores
而对ratings去均值后
score==mean_cscores

你可能感兴趣的:(杂项一一笔记)