different result of PCA function and my PCA
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
HI. I wrote a code for principal component analysis ( PCA).
the number of results are same but in some PC the sign of some of them is different (ex. In PC3 coeff=~coeff2). I am confused about the signs.Is my code incomplete? what is the meaning of this sign?
I checked the result of minitab and there is same problem. anyway I want my results to be the same with the results of the MATLAB function.
data=importdata('n_data.txt');
%% function
[coeff] = pca(data,'Algorithm','eig')
%% my PCA
C = cov(data);
[u,d]=eig(C); %find eigen value and eigen vectors
D=diag(d); % Convert matrix to column vector
[D,indx]=sort(D,'descend'); % Sort eigen value and corresponded eigen vector
Coeff2=u(:,indx)
0 Kommentare
Antworten (2)
John D'Errico
am 12 Okt. 2022
Bearbeitet: John D'Errico
am 12 Okt. 2022
Congratulations! You are the 1 millionth person to have this happen, and then be confused. Your reward is a free trip to Newark, New Jersey. Free, that is, in the sense, that we pay nothing towards it, you pay all costs, make all arrangements. ;-)
Anyway, the eigenvectors that are returned (as part of the PCA) are NOT unique. They can be completely arbitrarily multiplied by -1. Think about it like this. What is an eigenvector, eigenvalue pair, but A solution to the problem:
A*x = lambda*x
But if x and lambda form a valid solution to the problem, then is it not true that -x and lambda ALSO form a solution? So is it not equally true that
A*(-x) = lambda*(-x)
What did you find? That in some cases, the vectors had a DIFFERENT sign. This is comepletely expected, and cannot be controlled, nor should you care.
0 Kommentare
David Goodmanson
am 12 Okt. 2022
Bearbeitet: David Goodmanson
am 12 Okt. 2022
Hi Farshad,
Each column vector in the cof matrix is arbitrary up to an overall constant of absolute value 1, which for the case of everything being real means an overall factor of +-1. That is the nature of the disagreement that you have, which is good since it shows that your code is basically correct. Those sign changes do not really matter, but it's not the only thing that changes.
In the usual case, X is an nxm matrix with n>m. The means are subtracted down each column, Xm = X - mean(X). Then the scores are calculated with
score = Xm*cof
Then, since the cof matrix is unitary,
Xm = score*cof' (1)
Suppose that the kth column of your cof matrix has a sign change of -1 compared to Matlab pca. If you compute the scores, you find that the kth column of your scores also has a sign change of -1 compared to Matlab pca scores. What's important is, if cof and score stay paired up, (1) is unchanged and characterizes the data.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Dimensionality Reduction and Feature Extraction finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!