Matlab PCA algorithm

Source: Internet
Author: User

Matlab built-in PCA Function Form

 [mappedX, mapping] = pca(X, no_dims) 

 

Step for self-writing PCA Functions

% Step 1: input sample matrix % % data = rand (10, 8) + randn () + ones (); % pca dimensionality reduction % second step: calculate the average value of each dimension in the sample, then calculate the deviation between the observed value and the mean, and then calculate the covariance matrix data = bsxfun (@ minus, data, mean (data )); % calculate the mean of the sample matrix C = data' * data; C = C. /(size (data, 1)-1); % calculate the covariance according to the covariance formula, get the covariance moment C % Step 3: Calculate the feature value of the covariance matrix and feature vector matrix fprintf (1, 'calculating generalized eigenvectors and eigenvalues... \ n'); [eigvectors, eigvalues] = eig (C); % eigvectors is a matrix composed of feature vectors, eigvalue The diagonal matrix fprintf (1, 'sorting eigenvectors according to eigenvalues... \ n'); d1 = diag (eigvalues); % returns the value on the diagonal line of the matrix, which is a column vector. [Dsort index] = sort (d1, 'descend'); % sort in descending order. dsort is the sorted value, and index is the index value vsort = eigvectors (:, index ); % sort feature vectors by column according to feature value % Step 4: Calculate total energy, and select the feature value dsum = sum (d2) with the largest contribution rate ); % sum of all the feature values dsum_extract = 0; % calculate the sum of the first several feature values. If the sum of the current feature values is greater than 90%, these feature values can be considered to represent the current matrix p = 0; while (dsum_extract/dsum <0.9) p = p + 1; dsum_extract = sum (dsort (1: p); end % Step 5: calculate the matrix composed of feature vectors corresponding to the first p feature values, and calculate the sample matrix vsort = vsort (:, 1: p) after dimensionality reduction ); % extract the first p Feature vector to obtain the d * p Matrix fprintf (1, 'feature extraction and calculating newData... \ n'); newdata = data * vsort; % generate a matrix of n rows of p columns to reduce the dimension.




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.