Principal Component Analysis in MATLAB

前端 未结 1 367
说谎
说谎 2020-12-16 03:36

I\'m implementing PCA using eigenvalue decomposition for sparse data. I know matlab has PCA implemented, but it helps me understand all the technicalities when I write code.

相关标签:
1条回答
  • 2020-12-16 03:50

    Here's how I would do it:

    function [V newX D] = myPCA(X)
        X = bsxfun(@minus, X, mean(X,1));           %# zero-center
        C = (X'*X)./(size(X,1)-1);                  %'# cov(X)
    
        [V D] = eig(C);
        [D order] = sort(diag(D), 'descend');       %# sort cols high to low
        V = V(:,order);
    
        newX = X*V(:,1:end);
    end
    

    and an example to compare against the PRINCOMP function from the Statistics Toolbox:

    load fisheriris
    
    [V newX D] = myPCA(meas);
    [PC newData Var] = princomp(meas);
    

    You might also be interested in this related post about performing PCA by SVD.

    0 讨论(0)
提交回复
热议问题