Implementation of PCA algorithm in MATLAB

Source: Internet
Author: User

function [V,s,e]=princa (x) [M,n]=size (x); The% calculates the row m and column n% of the matrix-------------the first step: the normalized matrix-----------------%mv=mean (X); % calculates the mean value of each variable st=std (X); % calculates the standard deviation of each variable x= (X-repmat (mv,m,1))./repmat (st,m,1); % normalized matrix x-------------Second step: Calculate the correlation coefficient matrix-----------------percent r1=x ' *x/(m-1); % method One: Covariance matrix calculation formula% R2=cov (X);     % method Two: Covariance matrix calculation function R=corrcoef (X); % method Three: correlation coefficient matrix function%-------------third step: compute eigenvectors and eigenvalues-----------------%[v,d]=eig (R);       The% computes the characteristic vector matrix V of the matrix R and the eigenvalue Matrix D, with eigenvalues from small to large v= (Rot90 (V)) ';      % The eigenvector Matrix V is sorted from large to small d=rot90 (Rot90 (D));  % The eigenvalue matrix is sorted from large to small e=diag (D);          % convert eigenvalue matrix to eigenvalue vector%-------------Fourth step: Calculate contribution rate and accumulative contribution rate-----------------%ratio=0; % cumulative contribution rate for k=1:n    r=e (k)/sum (E);   % K principal component contribution rate    ratio=ratio+r;  % Cumulative Contribution    if (ratio>=0.9)  % take the cumulative contribution rate greater than or equal to 90% of the main component break        ;    endend%-------------Fifth step: Calculate the score-----------------%s=x*v;

If principal component analysis is required, a function (Princomp) with Matlab comes in. The Princomp call is as follows:
[Coeff,score,latent,tsquare] = Princomp (Zscore (X))

Zscore (x) is a standardized operation on the Matrix X.
Coeff is the matrix of all eigenvectors of the covariance matrix corresponding to the X-matrices, i.e., the transformation matrix or the projection matrix, each column corresponds to a characteristic vector of eigenvalues, and the order of the columns is descending by the size of the eigenvalues.
The equivalent of V in the above program, which represents the coefficient of the principal component.
Score is the scoring of the principal component, i.e. the expression of the original X matrix in the principal component space. Each row corresponds to a sample observation, and each column corresponds to a main component (variable), and its number of rows and columns is the same as the number of lines in X. (equivalent to S in the above program)
A latent is a vector of eigenvalues of the covariance matrix corresponding to X. (equivalent to E in a program)
Relationship between eigenvalues and fractions: Latent=diag (CoV (score));
Tsquare is the T-square statistic representing the hotelling of each sample point
When calculating PCA, if there is a ready-made covariance matrix directly, the function Pcacov is used to calculate.

Implementation of PCA algorithm in MATLAB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.