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 slclassify
Home > sltoolbox > utils > slclassify.m

slclassify

PURPOSE ^

SLCLASSIFY Classifies a set of samples according to final scores

SYNOPSIS ^

function [decisions, decscores] = slclassify(scores, clabels, op, varargin)

DESCRIPTION ^

SLCLASSIFY Classifies a set of samples according to final scores

 $ Syntax $
   - decisions = slclassify(scores, clabels, op, ...)
   - [decisions, decscores] = slclassify(scores, clabels, op, ...)

 $ Arguments $
   - scores:       the score matrix
   - clabels:      the class labels of reference samples
   - op:           the score attribute
   - decisions:    the classification decisions
   - decscores:    the scores of the classified targets

 $ Description $
   - decisions = slclassify(scores, clabels, op, ...) classifies
     a set of query samples to classes. Suppose there are m referenced
     targets and n query samples, scores should be an m x n matrix, with
     each column representing the scores of the corresponding sample to
     all targets.
     If op is 'high', then the samples are classified to the target of
     highest score, otherwise, the samples are classified to the target
     of lowest score.
     Moreover, you can specify following properties to have more
     control on the classification process.
       \*
       \t   Properties of Classification
       \h     name     &  description
             'scheme'  &  The classification scheme
                          'nn':  using normal nearest sample
                                 classification (default)
                          'loo': leave-one-out nearest sample scheme
                                 (only for the case, when gallery and
                                  query sets are the same)
       \*

   - [decisions, decscores] = slclassify(scores, clabels, op, ...)
     additionally outputs the scores on classified samples.

 $ Remarks $
   - The outputs are 1xn row vectors.
   - In leave-one-out scheme, it is assumed that the referenced samples
     and the query samples are actually the same set and in same order.
 
 $ History $
   - Created by Dahua Lin, on Aug 9th, 2006
   - Modified by Dahua Lin, on Aug 16th, 2006
       - eliminate the qlabel parameters, which are essentially not
         needed.
       - add functionality to support schemes. In current revision,
         it supports nearest-neighbor ('nn') and leave-one-out ('loo').

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:
  • slclassify_eucnn SLCLASSIFY_EUCNN Classifies samples using Euclidena-based NN
  • slcorrectrate SLCORRECTRATE Computes the correct rate of classification

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [decisions, decscores] = slclassify(scores, clabels, op, varargin)
0002 %SLCLASSIFY Classifies a set of samples according to final scores
0003 %
0004 % $ Syntax $
0005 %   - decisions = slclassify(scores, clabels, op, ...)
0006 %   - [decisions, decscores] = slclassify(scores, clabels, op, ...)
0007 %
0008 % $ Arguments $
0009 %   - scores:       the score matrix
0010 %   - clabels:      the class labels of reference samples
0011 %   - op:           the score attribute
0012 %   - decisions:    the classification decisions
0013 %   - decscores:    the scores of the classified targets
0014 %
0015 % $ Description $
0016 %   - decisions = slclassify(scores, clabels, op, ...) classifies
0017 %     a set of query samples to classes. Suppose there are m referenced
0018 %     targets and n query samples, scores should be an m x n matrix, with
0019 %     each column representing the scores of the corresponding sample to
0020 %     all targets.
0021 %     If op is 'high', then the samples are classified to the target of
0022 %     highest score, otherwise, the samples are classified to the target
0023 %     of lowest score.
0024 %     Moreover, you can specify following properties to have more
0025 %     control on the classification process.
0026 %       \*
0027 %       \t   Properties of Classification
0028 %       \h     name     &  description
0029 %             'scheme'  &  The classification scheme
0030 %                          'nn':  using normal nearest sample
0031 %                                 classification (default)
0032 %                          'loo': leave-one-out nearest sample scheme
0033 %                                 (only for the case, when gallery and
0034 %                                  query sets are the same)
0035 %       \*
0036 %
0037 %   - [decisions, decscores] = slclassify(scores, clabels, op, ...)
0038 %     additionally outputs the scores on classified samples.
0039 %
0040 % $ Remarks $
0041 %   - The outputs are 1xn row vectors.
0042 %   - In leave-one-out scheme, it is assumed that the referenced samples
0043 %     and the query samples are actually the same set and in same order.
0044 %
0045 % $ History $
0046 %   - Created by Dahua Lin, on Aug 9th, 2006
0047 %   - Modified by Dahua Lin, on Aug 16th, 2006
0048 %       - eliminate the qlabel parameters, which are essentially not
0049 %         needed.
0050 %       - add functionality to support schemes. In current revision,
0051 %         it supports nearest-neighbor ('nn') and leave-one-out ('loo').
0052 %
0053 
0054 %% parse and verify input arguments
0055 
0056 if nargin < 3
0057     raise_lackinput('slclassify', 3);
0058 end
0059 if ~isnumeric(scores) || ndims(scores) ~= 2
0060     error('sltoolbox:invalidarg', ...
0061         'scores should be an 2D numeric matrix');
0062 end
0063 m = size(scores, 1);
0064 if length(clabels) ~= m 
0065     error('sltoolbox:sizmismatch', ...
0066         'The sizes of labels are inconsistent with the score matrix');
0067 end
0068 
0069 opts.scheme = 'nn';
0070 opts = slparseprops(opts, varargin{:});
0071 
0072 %% Main skeleton
0073 
0074 switch opts.scheme
0075     case 'nn'
0076         [decisions, decscores] = classify_nn(scores, clabels, op);
0077     case 'loo'
0078         [decisions, decscores] = classify_loo(scores, clabels, op);
0079     otherwise
0080         error('sltoolbox:invalidarg', ...
0081             'Invalid scheme for classification: %s', opts.scheme);
0082 end
0083 
0084 
0085 
0086 
0087 %% Decision-making core routines
0088 
0089 %% NN
0090 
0091 function [decisions, decscores] = classify_nn(scores, clabels, op)
0092 
0093 switch op
0094     case 'high'
0095         [decscores, decinds] = max(scores, [], 1);
0096     case 'low'
0097         [decscores, decinds] = min(scores, [], 1);
0098     otherwise
0099         error('sltoolbox:invalidarg', ...
0100             'Invalid score option %s', op);
0101 end
0102 
0103 clabels = clabels(:)';
0104 decisions = clabels(decinds);
0105 
0106 
0107 %% LOO
0108 
0109 function [decisions, decscores] = classify_loo(scores, clabels, op)
0110 
0111 n = size(scores, 1);
0112 if size(scores, 2) ~= n
0113     error('sltoolbox:sizmismatch', ...
0114         'In leave-one-out scheme, the score matrix should be square');
0115 end
0116 if n < 2
0117     error('sltoolbox:invalidarg', ...
0118         'In leave-one-out scheme, the set should have at least two elements');
0119 end
0120 
0121 % preprocessing to disable the selection of self
0122 inds_diag = (1 : n+1 : n^2);
0123 switch op
0124     case 'high'
0125         scores(inds_diag) = -Inf;
0126     case 'low'
0127         scores(inds_diag) = Inf;
0128     otherwise
0129         error('sltoolbox:invalidarg', ...
0130             'Invalid score option %s', op);
0131 end
0132 
0133 % classify
0134 switch op
0135     case 'high'
0136         [decscores, decinds] = max(scores, [], 1);
0137     case 'low'
0138         [decscores, decinds] = min(scores, [], 1);
0139 end
0140 
0141 clabels = clabels(:)';
0142 decisions = clabels(decinds);
0143         
0144         
0145         
0146 
0147 
0148

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

Contact us at files@mathworks.com