'pca' vs 'svd' or 'eig' functions
조회 수: 12 (최근 30일)
이전 댓글 표시
Hi,
I am trying to generate the principal components from a set of data. However, i get an entirely different result when i use the 'pca' function compared to the 'eig' function. The 'eig' function gives the same results as the 'svd' function for my data.
I am using the raw data as input into the 'pca' function.
For 'eig' - I am calculating the correlation matrix and then using that as input into the 'eig' function.
I am very puzzled on why i get different results and would be grateful for your help! Code below:
testmat = rand(20,5);
testcorrelMat = corr(testmat);
testeig = eig(testcorrelMat);
testsvd = svd(testcorrelMat);
[testcoeff, ~, testlatent] = pca(testmat);
[sort(testsvd), sort(testeig), sort(testlatent)]
댓글 수: 0
채택된 답변
the cyclist
2021년 3월 16일
You will get the same result from pca() if you standardize the input data first:
rng default
testmat = rand(20,5);
% Standardize the data
testmat = (testmat - mean(testmat))./std(testmat);
testcorrelMat = corr(testmat);
testeig = eig(testcorrelMat);
testsvd = svd(testcorrelMat);
[testcoeff, ~, testlatent] = pca(testmat);
[sort(testsvd), sort(testeig), sort(testlatent)]
댓글 수: 2
Steven Lord
2021년 3월 16일
To normalize the data you can use the normalize function to normalize by 'zscore' (which is the default normalization method.)
rng default
testmat = rand(20,5);
% Standardize the data
testmat = normalize(testmat);
testcorrelMat = corr(testmat);
testeig = eig(testcorrelMat);
testsvd = svd(testcorrelMat);
[testcoeff, ~, testlatent] = pca(testmat);
results = [sort(testsvd), sort(testeig), sort(testlatent)]
format longg
results - results(:, 1)
Looks pretty good to me.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!