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.