Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one should I use for dimension reduction with PCA in MATLAB, pcacov or eigs?

I'm trying to reduce my training set dimension from 1296*70000 to 128*70000. I wrote Below code:

A=DicH;
[M N]=size(A);
mu=mean(A,2);%mean of columns

Phi=zeros(M,N);
C=zeros(M,M);
for j=1:N
    Phi(:,j)=A(:,j)-mu;
    c=Phi(:,j)*(Phi(:,j))';
    C=C+c;
end

C=C/N;%Covariance Dictionary
[V,landa] = eigs(C,128);%Eigen Vectors & Eigen Values
E=V'*Phi;%Reduced Dic
%*******************Using Pcacov*****************
%S=zeros(M,1);
%[U,landa] = pcacov(C);%Eigen Vectors & Eigen Values
% for k=1:128;
%     S=V(:,k)+S;
%     U(:,k)=S;
% end
%E=U'*Phi;%Reduced Dic

I get two different answers! Which one should I use "eigs" or "pcacov"??

like image 998
Mehran Avatar asked Dec 12 '25 22:12

Mehran


1 Answers

You should take advantage of the built-in functions in Matlab, and use the pca function directly, or even the cov function if you want to compare eigs to pcaconv.

Now to answer your question, both return the same eigenvectors but not in the same order. See the following example:

>> load hald
>> covx = cov(ingredients);
>> [COEFF,latent] = pcacov(covx)

COEFF =

   -0.0678   -0.6460    0.5673    0.5062
   -0.6785   -0.0200   -0.5440    0.4933
    0.0290    0.7553    0.4036    0.5156
    0.7309   -0.1085   -0.4684    0.4844


latent =

  517.7969
   67.4964
   12.4054
    0.2372

>> [V, D] = eigs(covx)                    

V =

    0.5062    0.5673    0.6460   -0.0678
    0.4933   -0.5440    0.0200   -0.6785
    0.5156    0.4036   -0.7553    0.0290
    0.4844   -0.4684    0.1085    0.7309


D =

    0.2372         0         0         0
         0   12.4054         0         0
         0         0   67.4964         0
         0         0         0  517.7969

>> 

In your code, you overwrite the result of pcavconv in the commented-out section with a transformation of the result of eigs so it is not clear what your are comparing at this point. When using pcacov, you just need to extract the 128 first columns of U.

like image 158
damienfrancois Avatar answered Dec 16 '25 23:12

damienfrancois



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!