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 slfld
Home > sltoolbox > subspace > slfld.m

slfld

PURPOSE ^

SLFLD Performs Fisher Linear Discriminant Analysis

SYNOPSIS ^

function T = slfld(X, nums, varargin)

DESCRIPTION ^

SLFLD Performs Fisher Linear Discriminant Analysis

 $ Syntax $
   - T = slfld(X, nums)
   - T = slfld(X, nums, ...)

 $ Arguments $
   - X:        the training sample matrix
   - nums:     the numbers of samples in all classes
   - T:        the solved transform matrix

 $ Description $
   - T = slfld(X, nums) performs Fisher linear discriminant analysis 
     on the samples X in default way.

   - T = slfld(X, nums, ...) performs Fisher linear discriminant analysis
     on the samples X according to the specified properties. 
     \*
     \t   Table 1.  The properties of Fisher Discriminant Analysis   \\
     \h     name     &     description                                \\
           'prepca'  &  Whether to perform a preamble PCA to first 
                        reduce the dimensions to the samples' rank.
                        default = false.                              \\
           'whiten' &  The cell containing the arguments for computing 
                       the whitening transform.  default = {}.       \\
           'dimset'  &  The cell containing the arguments for determining
                        the output feature dimension. default = {}.
                        (refer to sldim_by_eigval).                   \\
           'Sb'      &  The pre-computed between-class scattering matrix
                        or the cell containing the arguments for 
                        computing the scatter matrix in the form
                        {type, ...}, which is input to slscatter.     \\
           'Sw'      &  The pre-computed within-class scattering matrix
                        or the cell containing the arguments for 
                        computing the scatter matrix in the form
                        {type, ...}, which is input to slscatter.     \\
         'weights'   &  The sample weights. default = [].             \\
     \*  

 $ Remarks $
   -# The function solves the transform in mainly two stages: 
      First, whiten the samples so that the between-class scattering
      become the identity matrix; then solves the projection to maximize
      the whitened between-class scattering. If prepca is set to true
      a preamble step for reducing the dimensions to rank by PCA is taken.

   -# The function is very flexible. By tuning the arguments for
      pre-pca, whitening, and the computation of Sb and Sw, it turns to
      be various LDA-based algorithms, including fisher LDA, 
      regularized LDA, weighted pairwise LDA, dual-space LDA etc.
      
   -# If Sw or its computing rule is given, the whiten transform is 
      directly solved from Sw,  otherwise the whitening transform is 
      solved from samples. If Sb is given, the whitened between-class 
      scattering is computed by directly applying the whiten transform 
      to Sb, otherwise, Sb is computed from whitenned samples. If both 
      Sb and Sw are given, then the samples are not used in the function. 
      In this cases, you can simply input an empty X. 

   -# If both Sb and Sw are given, the pre-pca step will not be conducted.
      no matter whether prepca is true or false.

 $ History $
   - Created by Dahua Lin on Apr 30th, 2006
   - Modified by Dahua Lin on Sep 10th, 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
  • slsymeig SLSYMEIG Compute the eigenvalues and eigenvectors for symmetric matrix
  • slmeans SLMEANS Compute the mean vectors
  • slwhiten_from_cov SLWHITEN_FROM_COV Compute the whitening transform from covariance matrix
  • slwhiten_from_samples SLWHITEN_FROM_SAMPLES Compute the whitening matrix from sample matrix
  • sldim_by_eigval SLDIM_BY_EIGVAL Determines the dimension of principal subspace by eigenvalues
  • slpca SLPCA Learns a PCA model from training samples
  • slscatter SLSCATTER Compute the scatter matrix
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
  • slnums2bounds SLNUMS2BOUNDS Compute the index-boundaries from section sizes
  • slparseprops SLPARSEPROPS Parses input parameters
This function is called by:
  • sllda SLLDA Trains a Linear Discriminant Model using specified method

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function T = slfld(X, nums, varargin)
0002 %SLFLD Performs Fisher Linear Discriminant Analysis
0003 %
0004 % $ Syntax $
0005 %   - T = slfld(X, nums)
0006 %   - T = slfld(X, nums, ...)
0007 %
0008 % $ Arguments $
0009 %   - X:        the training sample matrix
0010 %   - nums:     the numbers of samples in all classes
0011 %   - T:        the solved transform matrix
0012 %
0013 % $ Description $
0014 %   - T = slfld(X, nums) performs Fisher linear discriminant analysis
0015 %     on the samples X in default way.
0016 %
0017 %   - T = slfld(X, nums, ...) performs Fisher linear discriminant analysis
0018 %     on the samples X according to the specified properties.
0019 %     \*
0020 %     \t   Table 1.  The properties of Fisher Discriminant Analysis   \\
0021 %     \h     name     &     description                                \\
0022 %           'prepca'  &  Whether to perform a preamble PCA to first
0023 %                        reduce the dimensions to the samples' rank.
0024 %                        default = false.                              \\
0025 %           'whiten' &  The cell containing the arguments for computing
0026 %                       the whitening transform.  default = {}.       \\
0027 %           'dimset'  &  The cell containing the arguments for determining
0028 %                        the output feature dimension. default = {}.
0029 %                        (refer to sldim_by_eigval).                   \\
0030 %           'Sb'      &  The pre-computed between-class scattering matrix
0031 %                        or the cell containing the arguments for
0032 %                        computing the scatter matrix in the form
0033 %                        {type, ...}, which is input to slscatter.     \\
0034 %           'Sw'      &  The pre-computed within-class scattering matrix
0035 %                        or the cell containing the arguments for
0036 %                        computing the scatter matrix in the form
0037 %                        {type, ...}, which is input to slscatter.     \\
0038 %         'weights'   &  The sample weights. default = [].             \\
0039 %     \*
0040 %
0041 % $ Remarks $
0042 %   -# The function solves the transform in mainly two stages:
0043 %      First, whiten the samples so that the between-class scattering
0044 %      become the identity matrix; then solves the projection to maximize
0045 %      the whitened between-class scattering. If prepca is set to true
0046 %      a preamble step for reducing the dimensions to rank by PCA is taken.
0047 %
0048 %   -# The function is very flexible. By tuning the arguments for
0049 %      pre-pca, whitening, and the computation of Sb and Sw, it turns to
0050 %      be various LDA-based algorithms, including fisher LDA,
0051 %      regularized LDA, weighted pairwise LDA, dual-space LDA etc.
0052 %
0053 %   -# If Sw or its computing rule is given, the whiten transform is
0054 %      directly solved from Sw,  otherwise the whitening transform is
0055 %      solved from samples. If Sb is given, the whitened between-class
0056 %      scattering is computed by directly applying the whiten transform
0057 %      to Sb, otherwise, Sb is computed from whitenned samples. If both
0058 %      Sb and Sw are given, then the samples are not used in the function.
0059 %      In this cases, you can simply input an empty X.
0060 %
0061 %   -# If both Sb and Sw are given, the pre-pca step will not be conducted.
0062 %      no matter whether prepca is true or false.
0063 %
0064 % $ History $
0065 %   - Created by Dahua Lin on Apr 30th, 2006
0066 %   - Modified by Dahua Lin on Sep 10th, 2006
0067 %       - replace sladd by sladdvec to increase efficiency.
0068 %
0069 
0070 %% parse and verify input arguments
0071 
0072 if nargin < 2
0073     raise_lackinput('slfld', 2);
0074 end
0075 
0076 % check size
0077 
0078 if ~isempty(X)    
0079     if ndims(X) ~= 2
0080         error('sltoolbox:invaliddims', ...
0081             'The sample matrix X should be a 2D matrix');
0082     end
0083     [d, n] = size(X);
0084     
0085     k = length(nums);
0086     if ~isequal(size(nums), [1, k]);
0087         error('sltoolbox:invaliddims', ...
0088             'The nums vector should be a row vector');
0089     end
0090     if sum(nums) ~= n
0091         error('sltoolbox:sizmismatch', ...
0092             'The total number in nums is not consistent with that in X');
0093     end
0094 end
0095 
0096 % check options
0097 
0098 opts.prepca = false;
0099 opts.whiten = {};
0100 opts.dimset = {};
0101 opts.Sb = {'Sb'};
0102 opts.Sw = {'Sw'};
0103 opts.weights = [];
0104 opts = slparseprops(opts, varargin{:});
0105 
0106 
0107 has_Sb = ~isempty(opts.Sb) && isnumeric(opts.Sb);
0108 has_Sw = ~isempty(opts.Sw) && isnumeric(opts.Sw);
0109 if has_Sb && has_Sw
0110     use_samples = false;
0111     d = size(opts.Sw, 1);
0112     
0113     if ~isequal(size(opts.Sb), [d, d]) || ~isequal(size(opts.Sw), [d, d])
0114         error('sltoolbox:sizmismatch', ...
0115             'Size consistency in Sb and Sw');
0116     end
0117         
0118 else
0119     if isempty(X)
0120         error('sltoolbox:invalidargs', ...
0121             'The samples cannot be empty when Sb or Sw is not pre-computed');
0122     end
0123     use_samples = true;
0124     if (has_Sb && ~isequal(size(opts.Sb), [d, d])) || (has_Sw && ~isequal(size(opts.Sw), [d, d]))
0125         error('sltoolbox:sizmismatch', ...
0126             'Size consistency in Sb and Sw');
0127     end
0128     
0129 end
0130 w = opts.weights;
0131 
0132 
0133 %% Compute
0134 
0135 %% Step 0: Pre-PCA
0136 pca_computed = false;
0137 if use_samples && opts.prepca
0138     SPCA = slpca(X, 'weights', w);
0139     X = SPCA.P' * sladdvec(X, -SPCA.vmean, 1);
0140     pca_computed = true;
0141 end
0142 
0143 %% Step 1: Compute whiten transform
0144 
0145 if has_Sw
0146     TW = slwhiten_from_cov(opts.Sw, opts.whiten{:});
0147 elseif ~isempty(opts.Sw) && ~isequal(opts.Sw, {'Sw'})
0148     Sw = slscatter(X, opts.Sw{:}, 'sweights', w, 'nums', nums);
0149     TW = slwhiten_from_cov(Sw);
0150     clear Sw;
0151 else
0152     TW = slwhiten_from_samples(make_withinclass_diffvecs(X, w, nums), ...
0153         'weights', w, opts.whiten{:});
0154 end
0155 
0156 if pca_computed
0157     T1 = SPCA.P * TW;
0158     clear SPCA TW;
0159 else
0160     T1 = TW;
0161     clear TW;
0162 end
0163 
0164 
0165 %% Step 2: Compute the second-stage transform
0166 
0167 if has_Sb
0168     WSb = T1' * opts.Sb * T1;
0169 else
0170     X = T1' * X;
0171     WSb = slscatter(X, opts.Sb{:}, 'sweights', w, 'nums', nums);
0172 end
0173 [evs, T2] = slsymeig(WSb);
0174 rk2 = sldim_by_eigval(evs, opts.dimset{:});
0175 T2 = T2(:, 1:rk2);
0176 
0177 %% Integrate the transforms
0178 
0179 T = T1 * T2;
0180 
0181 
0182 %% The function for making the difference vectors
0183 function Y = make_withinclass_diffvecs(X, w, nums)
0184 
0185 mvs = slmeans(X, w, nums);
0186 Y = X;
0187 [sp, ep] = slnums2bounds(nums);
0188 k = length(nums);
0189 for i = 1 : k
0190     Y(:, sp(i):ep(i)) = sladdvec(X(:, sp(i):ep(i)), -mvs(:,i), 1);
0191 end
0192 
0193 
0194 
0195 
0196 
0197

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

Contact us at files@mathworks.com