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

sldlda

PURPOSE ^

SLDLDA Performs Direct Linear Discriminant Analysis

SYNOPSIS ^

function T = sldlda(X, nums, varargin)

DESCRIPTION ^

SLDLDA Performs Direct Linear Discriminant Analysis

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

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

 $ Description $
   - T = sldlda(X, nums) performs direct LDA on the samples X using 
     default settings.

   - T = sldlda(X, nums, ...) performs direct LDA on the samples X
     with the specified properties.
     \*
     \t   Table 1.  The properties of Fisher Discriminant Analysis   \\
     \h     name     &     description                                \\
           'pdimset' &  The cell containing the arguments for determining
                        the range space of Sb. They will be input to
                        slrangespace for dimension determination.
           'whiten'  &  The cell containing the arguments for computing 
                        the whitening transform in 2nd stage. They will
                        input to slwhiten_from_cov.
                        default = {}.       \\
           '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 following stages: 
      First solves the range space of between-class scattering, 
      projecting all samples onto it. Then, solve the whitening transform
      of the projected within-class scattering.
      
   -# If Sb or its computing rule is given, the range space is  directly 
      solved from Sb, otherwise the null space is solved from class 
      centers. 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 May 1st, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slmulvec SLMULVEC multiplies a vector to columns or rows of a matrix
  • slmeans SLMEANS Compute the mean vectors
  • slwhiten_from_cov SLWHITEN_FROM_COV Compute the whitening transform from covariance matrix
  • slrangespace SLRANGESPACE Determines the subspace of the range of X
  • 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 = sldlda(X, nums, varargin)
0002 %SLDLDA Performs Direct Linear Discriminant Analysis
0003 %
0004 % $ Syntax $
0005 %   - T = sldlda(X, nums)
0006 %   - T = sldlda(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 = sldlda(X, nums) performs direct LDA on the samples X using
0015 %     default settings.
0016 %
0017 %   - T = sldlda(X, nums, ...) performs direct LDA on the samples X
0018 %     with the specified properties.
0019 %     \*
0020 %     \t   Table 1.  The properties of Fisher Discriminant Analysis   \\
0021 %     \h     name     &     description                                \\
0022 %           'pdimset' &  The cell containing the arguments for determining
0023 %                        the range space of Sb. They will be input to
0024 %                        slrangespace for dimension determination.
0025 %           'whiten'  &  The cell containing the arguments for computing
0026 %                        the whitening transform in 2nd stage. They will
0027 %                        input to slwhiten_from_cov.
0028 %                        default = {}.       \\
0029 %           'Sb'      &  The pre-computed between-class scattering matrix
0030 %                        or the cell containing the arguments for
0031 %                        computing the scatter matrix in the form
0032 %                        {type, ...}, which is input to slscatter.     \\
0033 %           'Sw'      &  The pre-computed within-class scattering matrix
0034 %                        or the cell containing the arguments for
0035 %                        computing the scatter matrix in the form
0036 %                        {type, ...}, which is input to slscatter.     \\
0037 %         'weights'   &  The sample weights. default = [].             \\
0038 %     \*
0039 %
0040 % $ Remarks $
0041 %   -# The function solves the transform in mainly following stages:
0042 %      First solves the range space of between-class scattering,
0043 %      projecting all samples onto it. Then, solve the whitening transform
0044 %      of the projected within-class scattering.
0045 %
0046 %   -# If Sb or its computing rule is given, the range space is  directly
0047 %      solved from Sb, otherwise the null space is solved from class
0048 %      centers. If both Sb and Sw are given, then the samples are not
0049 %      used in the function. In this cases, you can simply input an empty X.
0050 %
0051 %   -# If both Sb and Sw are given, the pre-pca step will not be conducted.
0052 %      no matter whether prepca is true or false.
0053 %
0054 % $ History $
0055 %   - Created by Dahua Lin on May 1st, 2006
0056 %
0057 
0058 %% parse and verify input arguments
0059 
0060 if nargin < 2
0061     raise_lackinput('slfld', 2);
0062 end
0063 
0064 % check size
0065 
0066 if ~isempty(X)    
0067     if ndims(X) ~= 2
0068         error('sltoolbox:invaliddims', ...
0069             'The sample matrix X should be a 2D matrix');
0070     end
0071     [d, n] = size(X);
0072     
0073     k = length(nums);
0074     if ~isequal(size(nums), [1, k]);
0075         error('sltoolbox:invaliddims', ...
0076             'The nums vector should be a row vector');
0077     end
0078     if sum(nums) ~= n
0079         error('sltoolbox:sizmismatch', ...
0080             'The total number in nums is not consistent with that in X');
0081     end
0082 end
0083 
0084 % check options
0085 opts.pdimset = {};
0086 opts.whiten = {};
0087 opts.Sb = {'Sb'};
0088 opts.Sw = {'Sw'};
0089 opts.weights = [];
0090 opts = slparseprops(opts, varargin{:});
0091 
0092 has_Sb = ~isempty(opts.Sb) && isnumeric(opts.Sb);
0093 has_Sw = ~isempty(opts.Sw) && isnumeric(opts.Sw);
0094 if has_Sb && has_Sw
0095     d = size(opts.Sw, 1);
0096     
0097     if ~isequal(size(opts.Sb), [d, d]) || ~isequal(size(opts.Sw), [d, d])
0098         error('sltoolbox:sizmismatch', ...
0099             'Size consistency in Sb and Sw');
0100     end
0101         
0102 else
0103     if isempty(X)
0104         error('sltoolbox:invalidargs', ...
0105             'The samples cannot be empty when Sb or Sw is not pre-computed');
0106     end
0107     if (has_Sb && ~isequal(size(opts.Sb), [d, d])) || (has_Sw && ~isequal(size(opts.Sw), [d, d]))
0108         error('sltoolbox:sizmismatch', ...
0109             'Size consistency in Sb and Sw');
0110     end
0111     
0112 end
0113 w = opts.weights;
0114 
0115 %% Step 1: Compute range space of Sb
0116 
0117 if has_Sb
0118     T1 = slrangespace({'cov', opts.Sb}, opts.pdimset{:});
0119 elseif ~isempty(opts.Sb) && ~isequal(opts.Sb, {'Sb'})
0120     Sb = slscatter({'cov', X}, opts.Sb{:}, 'sweights', w, 'nums', nums);
0121     T1 = slrangespace(Sb, opts.pdimset{:});
0122     clear Sb;
0123 else
0124     Xc = get_weighted_centers(X, w, nums);
0125     T1 = slrangespace(Xc, opts.pdimset{:});
0126     clear Xc wc;
0127 end
0128 
0129 
0130 %% Step 2: Compute the whiten transform for Sw on range space
0131 
0132 if has_Sw
0133     PSw = T1' * opts.Sw * T1;
0134 else
0135     X = T1' * X;
0136     PSw = slscatter(X, opts.Sw{:}, 'sweights', w, 'nums', nums);
0137 end
0138 T2 = slwhiten_from_cov(PSw, opts.whiten{:});
0139 T2 = flipdim(T2, 2);
0140 
0141 %% Integrate the transforms
0142 
0143 T = T1 * T2;
0144 
0145 
0146 %% The function for computing weighted centers
0147 function [Xc, wc] = get_weighted_centers(X, w, nums)
0148 
0149 Xc = slmeans(X, w, nums);
0150 if isempty(w)
0151     wc = nums;
0152 else
0153     k = length(nums);
0154     [sp, ep] = slnums2bounds(nums);
0155     wc = zeros(1, k);
0156     for i = 1 : k
0157         wc(i) = sum(w(sp(i):ep(i)));
0158     end
0159 end
0160 wc = sqrt(max(wc, 0));
0161 Xc = slmulvec(Xc, wc, 2);
0162 
0163

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

Contact us at files@mathworks.com