Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slpca
Home > sltoolbox > subspace > slpca.m

slpca

PURPOSE ^

SLPCA Learns a PCA model from training samples

SYNOPSIS ^

function S = slpca(X, varargin)

DESCRIPTION ^

SLPCA Learns a PCA model from training samples

 $ Syntax $
   - S = slpca(X)
   - S = slpca(X, ...)

 $ Arguments $
   - X:        the training sample matrix
   - S:        the struct representing the learned PCA

 $ Description $
   - S = slpca(X) learns a PCA model from the samples X by default way.

   - S = slpca(X, ...) learns a PCA model from the samples X according to
     the properties specified:
     \*
     \t   Table 1. The properties of PCA learning
     \h    name       &    description                               \\
          'method'    & The method using in training the PCA model.
                        Currently, there are three methods available:
                        default = 'auto'.
                        1. 'auto': automatically selection of the best;
                        2. 'std':  use standard way based on covariance;
                        3. 'svd':  use SVD-based computation
                        4. 'trans': use a transposed way, it is typically
                           used for small-sample-size and high-dimension
                           cases.                                    \\
          'preserve'  & Determine how many components are preserved, it is
                        given in following form: {sch, ...}, which is used
                        as parameters in sldim_by_eigval.       \\
          'weights'   & The 1 x n row vector of sample weights.  
                        If the weights are not specified, then it 
                        considers that all samples have weights 1. 
                        default = [].   \\
     \*
                           
 $ History $
   - Created by Dahua Lin on Apr 24, 2006
   - Modified by Dahua Lin on Apr 25, 2006
       - Extract the dimension determination part to an independent
         function, in order to offer more flexible preservation process,
         and make it reusable in more functions.
   - Modified by Dahua Lin on Aug 17, 2006
       - Add a field energyratio
   - Modified by Dahua Lin, on Sep 10, 2006
       - replace sladd by sladdvec to increase efficiency

CROSS-REFERENCE INFORMATION ^

This function calls:
  • sladdvec SLADDVEC adds a vector to columns or rows of a matrix
  • slmulvec SLMULVEC multiplies a vector to columns or rows of a matrix
  • slnormalize SLNORMALIZE Normalize the sub-arrays
  • slsymeig SLSYMEIG Compute the eigenvalues and eigenvectors for symmetric matrix
  • slmean SLMEAN Compute the mean vector of samples
  • sldim_by_eigval SLDIM_BY_EIGVAL Determines the dimension of principal subspace by eigenvalues
  • slignorevars SLIGNOREVARS Ignores the input variables
  • slparseprops SLPARSEPROPS Parses input parameters
This function is called by:
  • slfld SLFLD Performs Fisher Linear Discriminant Analysis
  • slnlda SLNLDA Performs Nullspace-based Linear Discriminant Analysis

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function S = slpca(X, varargin)
0002 %SLPCA Learns a PCA model from training samples
0003 %
0004 % $ Syntax $
0005 %   - S = slpca(X)
0006 %   - S = slpca(X, ...)
0007 %
0008 % $ Arguments $
0009 %   - X:        the training sample matrix
0010 %   - S:        the struct representing the learned PCA
0011 %
0012 % $ Description $
0013 %   - S = slpca(X) learns a PCA model from the samples X by default way.
0014 %
0015 %   - S = slpca(X, ...) learns a PCA model from the samples X according to
0016 %     the properties specified:
0017 %     \*
0018 %     \t   Table 1. The properties of PCA learning
0019 %     \h    name       &    description                               \\
0020 %          'method'    & The method using in training the PCA model.
0021 %                        Currently, there are three methods available:
0022 %                        default = 'auto'.
0023 %                        1. 'auto': automatically selection of the best;
0024 %                        2. 'std':  use standard way based on covariance;
0025 %                        3. 'svd':  use SVD-based computation
0026 %                        4. 'trans': use a transposed way, it is typically
0027 %                           used for small-sample-size and high-dimension
0028 %                           cases.                                    \\
0029 %          'preserve'  & Determine how many components are preserved, it is
0030 %                        given in following form: {sch, ...}, which is used
0031 %                        as parameters in sldim_by_eigval.       \\
0032 %          'weights'   & The 1 x n row vector of sample weights.
0033 %                        If the weights are not specified, then it
0034 %                        considers that all samples have weights 1.
0035 %                        default = [].   \\
0036 %     \*
0037 %
0038 % $ History $
0039 %   - Created by Dahua Lin on Apr 24, 2006
0040 %   - Modified by Dahua Lin on Apr 25, 2006
0041 %       - Extract the dimension determination part to an independent
0042 %         function, in order to offer more flexible preservation process,
0043 %         and make it reusable in more functions.
0044 %   - Modified by Dahua Lin on Aug 17, 2006
0045 %       - Add a field energyratio
0046 %   - Modified by Dahua Lin, on Sep 10, 2006
0047 %       - replace sladd by sladdvec to increase efficiency
0048 %
0049 
0050 %% parse and verify input arguments
0051 
0052 % for size
0053 if ndims(X) ~= 2
0054     error('sltoolbox:invaliddims', 'X should be a 2D sample matrix');
0055 end
0056 [d, n] = size(X);
0057 
0058 % for options
0059 opts.method = 'auto';
0060 opts.preserve = {'rank'};
0061 opts.weights = [];
0062 opts = slparseprops(opts, varargin{:});
0063 
0064 % check options
0065 
0066 switch opts.method
0067     case 'auto'
0068         fh_learnpca = @pca_auto;
0069     case 'std'
0070         fh_learnpca = @pca_std;
0071     case 'svd'
0072         fh_learnpca = @pca_svd;
0073     case 'trans'
0074         fh_learnpca = @pca_trans;
0075     otherwise
0076         error('sltoolbox:invalidarg', ...
0077             'Invalid PCA learning method %s', opts.method);
0078 end
0079 
0080 
0081 if ~isempty(opts.weights)
0082     if ~isequal(size(opts.weights), [1, n])
0083         error('sltoolbox:sizmismatch', ...
0084             'The sample weights should be a 1 x n row vector');
0085     end
0086 end
0087 
0088 
0089 %% Compute
0090 
0091 % data centralization
0092 vmean = slmean(X, opts.weights);
0093 X = sladdvec(X, -vmean, 1);
0094 
0095 % sample scaling
0096 % in order to normalize the energy per unit sample weight
0097 if isempty(opts.weights)
0098     X = sqrt(1 / n) * X;
0099 else
0100     w = max(opts.weights, 0);
0101     tw = sum(w);
0102     sf = sqrt(w / tw);
0103     X = slmulvec(X, sf, 2);
0104     clear sf;
0105 end
0106     
0107 % learn full size PCA
0108 
0109 [P, evals] = fh_learnpca(X);
0110 
0111 
0112 % preserve principal components
0113 
0114 evals = max(evals, 0);
0115 kmax = min([size(P, 2), d, n-1]);
0116 if kmax < size(P, 2)
0117     P = P(:, 1:kmax);
0118     evals = evals(1:kmax);
0119 end
0120 k = sldim_by_eigval(evals, opts.preserve{:});
0121 
0122 
0123 %% Output the PCA struct
0124 
0125 S.sampledim = d;
0126 S.feadim    = k;
0127 
0128 if isempty(opts.weights)
0129     S.support = n;
0130 else
0131     S.support = tw;
0132 end
0133 
0134 S.vmean = vmean;
0135 if k < kmax
0136     S.P = P(:, 1:k);
0137     S.eigvals = evals(1:k);
0138     S.residue = sum(evals(k+1:kmax));
0139 else
0140     S.P = P;
0141     S.eigvals = evals;
0142     S.residue = 0;
0143 end
0144 
0145 prinenergy = sum(S.eigvals);
0146 S.energyratio = prinenergy / (prinenergy + S.residue);
0147 
0148 
0149 %% Sub functions for Learning PCA from centralized samples
0150 
0151 function [P, evals] = pca_auto(X)
0152 
0153 [d, n] = size(X);
0154 if d <= n
0155     [P, evals] = pca_std(X);
0156 else
0157     [P, evals] = pca_trans(X);
0158 end
0159 
0160 
0161 function [P, evals] = pca_std(X)
0162 
0163 C = X * X';
0164 [evals, P] = slsymeig(C);
0165 
0166 
0167 function [P, evals] = pca_svd(X)
0168 
0169 [P, D, V] = svd(X, 0);
0170 clear V;
0171 evals = diag(D) .^ 2;
0172 
0173 slignorevars(V);
0174 
0175 function [P, evals] = pca_trans(X)
0176 
0177 Ct = X' * X;
0178 [evals, P] = slsymeig(Ct);
0179 P = slnormalize(X * P);
0180 
0181 
0182 
0183

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com