Home > Machine Learning, matlab > Principal Component Analysis (Matlab Code)

Principal Component Analysis (Matlab Code)

function [evec, eval, meanA] = PCA(A, maxVec)

%Principal Component Analysis
%A has th
[ rows, cols] = size(A);
if(nargin < 2)
maxVec = rows;
end

maxVec = min(maxVec, rows);

meanA = mean(A,2);
A = A – meanA*ones(1, cols);
%[evec eval] = eig(A’*A);
[evec eval] = eig(A*A’);

[eval ind]  =  sort(-1*diag(eval));
%[eval ind]  =  sort(diag(eval), ”);
eval    = -1*eval(1:maxVec);
evec    = evec(:, ind(1:maxVec));

function num = findTopPCAvec(eval, percent)

%num is number of evals whose sum makes the percent*(total sum of the
%eval).
%eval: eigen values; sorted in descending order
%percent: percent for which you want to find num

totalV = length(eval);
totalSum = sum(eval);
for num=1:totalV
if( round(100*sum(eval(1:num)))/totalSum >= percent)
return;
end
end

function [coffs] = calcPcaCoff( evec, data, meanVec)

%calculates the cofficients of the data
%   Evec; each eigen vector is in the col
%   data: each feature vector is col vector
%   meanVec by default is zeros
%Returns
%   coffs: each cofficient is

[ rows, cols] = size(data);
if(nargin < 3)
meanVec = zeros(rows,1);
end

coffs = evec’*(data – repmat(meanVec, 1, cols));

  1. kartic
    May 5, 2009 at 10:32 am | #1

    i need the exact comment in matlab to get the output from the third layer where the network contains five layers

    • mohsenam
      May 5, 2009 at 4:17 pm | #2

      in PCA? You are talking about the Neural Network?

      • kartic
        May 7, 2009 at 4:54 am | #3

        i know that but what i need smebdy’s help.if that is not possible can you the syntax of pca for signal compression

  2. kartic
    May 7, 2009 at 5:02 am | #4

    sir if possible send your phone number to my mail-id because am new to matlab i have so many doubts

  3. kartic
    May 8, 2009 at 7:13 pm | #5

    function [coffs] = calcPcaCoff( evec, data, meanVec)
    in the above function what values should be given to data and meanvec

  4. kartic
    May 11, 2009 at 9:49 am | #6

    i have some doubts regarding the implementation
    1.could u plz explain me the implementation of all the three function( sorry just now going through the basics of matlab)
    2.say if my input data is s(large data set) then can i directly apply this to first function or i should perform filtering before applying to it.
    3.already i have applied my input to the first function then what is the need of applying once again to third function (data).
    4.whether meanA and meanvec are same

  5. mohsenam
    May 7, 2009 at 6:18 pm | #7

    Kartic I have never worked on the Signal Compression, but If you can represent signal as the vectors then it will be simply having matrix of singals and using the PCA code.
    Please tell me what you actually want to do. Post here what problem you are working on, I will be glad if I could be of any help

  6. kartic
    May 8, 2009 at 6:58 am | #8

    sir, i have to reduce the dimension of my inputs,inorder to reduce it am using pca.so could u plz help me in sending out the function which implements PCA.

  7. kartic
    May 8, 2009 at 7:00 am | #9

    sir, i need the pca syntax to reduce that matrix which is too large

  8. mohsenam
    May 8, 2009 at 5:19 pm | #10

    {Please stop using word sir, it makes me feel responsible and old }

    The above code does take the PCA of matrix A, Where A’s columns are the data-vectors i.e. [x1, x2, x3,,.....xn]
    Make this matrix A send it to the PCA function given above, it will send you back the eigen vectors and values.
    Chose only top 50 percent, lets say this number is p

    Now to calculate smaller matrix to represent A,
    project each data-vector in A to the eigen-vectors you have calculated in this way for each xi you will have p coefficients.
    this will give you p by n matrix instead of n by n matrix
    { you can use the third fuction (calcPcaCoff) given in the above post for calculating the cofficients}

    Please note that you should consult some Linear Algebra book to really understand benefits of the eigen vectors and values. It will be beneficial to you for long time.
    THanks
    I hope it helps you out.
    Bye

  9. kartic
    May 8, 2009 at 7:15 pm | #11

    function [coffs] = calcPcaCoff( evec, data, meanVec)
    in the above fun what is the value of data and meanvec

  10. mohsenam
    May 8, 2009 at 7:21 pm | #12

    In your case
    data is the matrix on which you have calculated the eigen vectors.
    meanVec is the mean of the data, which is also returned by the first function.

  11. kartic
    May 11, 2009 at 9:50 am | #13

    have some doubts regarding the implementation
    1.could u plz explain me the implementation of all the three function( sorry just now going through the basics of matlab)
    2.say if my input data is s(large data set) then can i directly apply this to first function or i should perform filtering before applying to it.
    3.already i have applied my input to the first function then what is the need of applying once again to third function (data).
    4.whether meanA and meanvec are same

  12. mohsenam
    May 11, 2009 at 6:17 pm | #14

    Kartic, you should go through the PCA from the Pattern Recognition by Duda and Hart.
    Ok, first function is simple calculating the eigen-vectors and eigen values of inputted data. {I dont know what you mean by filtering of data if it is large.}

    Second function is used in the case you want to select few top vectors based on their total eigen-values. Note that the eigen values tell us the importance of the eigen-vector.

    Third Function is not doing the PCA it is projecting the data on to the eigen-vectors

    Now: to really understand what 3rd function is doing you need to know why we do the PCA, what eigen-vectors of data represent.
    For which you should see some literature.
    eigen-vecotrs of Data: represent the orthogonal directions in which the data is spread, e.g. if our data when plotted is kind of ellipse, then the 2 eigen vectors we will get from PCA would be major and minor axis of such ellipse.
    By projecting data on the eigen vectors you are trying to find the coordinates of the data in the eigen-vector space.
    e.g. [2,3] generally means go 2 unit in x direction and 3 unit in y-direction. similarly after projections the coefficients we get tells where in the eigen-vector space that data-point lies.

    4) yes meanA and meanvec is same and is the mean of the data.

  13. kartic
    May 12, 2009 at 6:25 pm | #15

    A=0.0918 -0.1479 0.0990
    0.0990 -0.1479 0.0918

    if A is my input can u get me the coefficients after performing pca to it

  14. mohsenam
    May 12, 2009 at 6:37 pm | #16

    Kartic please refer some books and lectures about linear algebra, eigen analysis and PCA

    as for you question
    1) use first function to get eigen vectors and values
    2) use third function to find the coefficients.

  15. kartic
    May 18, 2009 at 6:28 pm | #17

    this is kartic once again my doubt is each signal of my database contains 4974 data . for example am using only one recording say 4974 whether it is possible for the above pca program to reduce it to six coefficients since my input layer of neural networks takes six values

  1. No trackbacks yet.