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 slkpca
Home > sltoolbox > kernel > slkpca.m

slkpca

PURPOSE ^

SLPCA Learns a Kernel PCA model from training samples

SYNOPSIS ^

function [A, evs] = slkpca(K0, varargin)

DESCRIPTION ^

SLPCA Learns a Kernel PCA model from training samples

 $ Syntax $
   - A = slkpca(K0)
   - A = slkpca(K0, ...)
   - [A, evs] = slkpca(K0, ...)

 $ Arguments $
   - X:        the training sample matrix
   - A:        the projection coefficient matrix 
   - evs:      the egienvalues of all n0-1 dimensions 
               (including preserved and discarded)

 $ Description $
   - A = slpca(K0) learns a Kernel PCA model from the samples X by 
     default settings.

   - A = slpca(K0, ...) learns a Kernel PCA model from the samples X 
     according to the properties specified:
     \*
     \t   Table 1. The properties of Kernel PCA learning
     \h    name       &    description                               \\
          '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 equal weights. 
                        default = [].   \\
        'centralized' & Whether the K0 has been centralized. default =
                        false. If the K0 is not centralized, it will 
                        first centralize it before subsequent steps. \\
     \*
  
 $ Remarks $
   -# The coefficient vectors are normalized so that a^T * K * a = 1.

 $ History $
   - Created by Dahua Lin on May 02, 2006
   - Modified by Dahua Lin on Sep 10, 2006
       - use slmulvec and slmulrowcols to increase efficiency
         in the weighted cases

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slmulrowcols SLMULROWCOLS Multiplies the vectors to all rows and all columns
  • slmulvec SLMULVEC multiplies a vector to columns or rows of a matrix
  • slsymeig SLSYMEIG Compute the eigenvalues and eigenvectors for symmetric matrix
  • slcenkernel SLCENKERNEL Compute the centralized kernel matrix
  • sldim_by_eigval SLDIM_BY_EIGVAL Determines the dimension of principal subspace by eigenvalues
  • slparseprops SLPARSEPROPS Parses input parameters
This function is called by:

SOURCE CODE ^

0001 function [A, evs] = slkpca(K0, varargin)
0002 %SLPCA Learns a Kernel PCA model from training samples
0003 %
0004 % $ Syntax $
0005 %   - A = slkpca(K0)
0006 %   - A = slkpca(K0, ...)
0007 %   - [A, evs] = slkpca(K0, ...)
0008 %
0009 % $ Arguments $
0010 %   - X:        the training sample matrix
0011 %   - A:        the projection coefficient matrix
0012 %   - evs:      the egienvalues of all n0-1 dimensions
0013 %               (including preserved and discarded)
0014 %
0015 % $ Description $
0016 %   - A = slpca(K0) learns a Kernel PCA model from the samples X by
0017 %     default settings.
0018 %
0019 %   - A = slpca(K0, ...) learns a Kernel PCA model from the samples X
0020 %     according to the properties specified:
0021 %     \*
0022 %     \t   Table 1. The properties of Kernel PCA learning
0023 %     \h    name       &    description                               \\
0024 %          'preserve'  & Determine how many components are preserved, it is
0025 %                        given in following form: {sch, ...}, which is used
0026 %                        as parameters in sldim_by_eigval.       \\
0027 %          'weights'   & The 1 x n row vector of sample weights.
0028 %                        If the weights are not specified, then it
0029 %                        considers that all samples have equal weights.
0030 %                        default = [].   \\
0031 %        'centralized' & Whether the K0 has been centralized. default =
0032 %                        false. If the K0 is not centralized, it will
0033 %                        first centralize it before subsequent steps. \\
0034 %     \*
0035 %
0036 % $ Remarks $
0037 %   -# The coefficient vectors are normalized so that a^T * K * a = 1.
0038 %
0039 % $ History $
0040 %   - Created by Dahua Lin on May 02, 2006
0041 %   - Modified by Dahua Lin on Sep 10, 2006
0042 %       - use slmulvec and slmulrowcols to increase efficiency
0043 %         in the weighted cases
0044 %
0045 
0046 %% parse and verify input arguments
0047 
0048 % for K0
0049 n0 = size(K0, 1);
0050 if ~isequal(size(K0), [n0 n0])
0051     error('sltoolbox:invaliddims', 'K0 should be a square matrix');
0052 end
0053 
0054 % for options
0055 opts.preserve = {};
0056 opts.weights = [];
0057 opts.centralized = false;
0058 opts = slparseprops(opts, varargin{:});
0059 
0060 if isempty(opts.weights)
0061     isweighted = false;
0062 else
0063     isweighted = true;
0064     if ~isequal(size(opts.weights), [1, n0])
0065         error('sltoolbox:sizmismatch', ...
0066             'The weights should be a 1 x n0 row vector');
0067     end
0068 end
0069 
0070 
0071 %% compute
0072 
0073 %% centralize the gram matrix
0074 if ~opts.centralized
0075     K0 = slcenkernel(K0, [], opts.weights);
0076 end
0077 
0078 %% solve the eigen-problem
0079 
0080 if ~isweighted      % non weighted case
0081     [evs, A] = slsymeig(K0);
0082     evs = evs(1:n0-1);
0083     
0084     k = sldim_by_eigval(evs, opts.preserve{:});
0085     A = A(:, 1:k);
0086     A = slmulvec(A, 1 ./ sqrt(evs(1:k))', 2); % normalize
0087     
0088     evs = evs / n0;
0089     
0090 else                % weighted case
0091     
0092     w = max(opts.weights, 0);
0093     sw = sqrt(w)'; % sw is a column vector
0094     K0w = slmulrowcols(K0, sw', sw);
0095     [evs, A] = slsymeig(K0w);
0096     evs = evs(1:n0-1);
0097     
0098     k = sldim_by_eigval(evs, opts.preserve{:});
0099     A = A(:, 1:k);
0100     A = slmulrowcols(A, 1 ./ sqrt(evs(1:k))', sw); % normalize
0101     
0102     evs = evs(1:n0-1) / sum(w);    
0103 end
0104

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

Contact us at files@mathworks.com