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

slclassify_blks

PURPOSE ^

SLCLASSIFY_BLKS Classifies samples according to blockwise scores

SYNOPSIS ^

function [decisions, decscores] = slclassify_blks(scores, n, blocks, clabels, op, varargin)

DESCRIPTION ^

SLCLASSIFY_BLKS Classifies samples according to blockwise scores

 $ Syntax $
   - [decisions, decscores] = slclassify_blks(scores, n, blocks, clabels, op, ...)

 $ Arguments $
   - scores:       the score matrix
   - n:            the number of query samples
   - blocks:       the cell array of block limits
   - clabels:      the class labels of reference samples
   - op:           the score attribute  
   - decisions:    the classification decisions
   - decscores:    the scores of the classified targets

 $ Remarks $
   - An extension of slclassify to support blockwise scores.

 $ 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:
  • slcorrectrate_blks SLCORRECTRATE_BLKS Computes the correct rate based on blockwise scores

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [decisions, decscores] = slclassify_blks(scores, n, blocks, clabels, op, varargin)
0002 %SLCLASSIFY_BLKS Classifies samples according to blockwise scores
0003 %
0004 % $ Syntax $
0005 %   - [decisions, decscores] = slclassify_blks(scores, n, blocks, clabels, op, ...)
0006 %
0007 % $ Arguments $
0008 %   - scores:       the score matrix
0009 %   - n:            the number of query samples
0010 %   - blocks:       the cell array of block limits
0011 %   - clabels:      the class labels of reference samples
0012 %   - op:           the score attribute
0013 %   - decisions:    the classification decisions
0014 %   - decscores:    the scores of the classified targets
0015 %
0016 % $ Remarks $
0017 %   - An extension of slclassify to support blockwise scores.
0018 %
0019 % $ History $
0020 %   - Created by Dahua Lin on Aug 9th, 2006
0021 %   - Modified by Dahua Lin, on Aug 16th, 2006
0022 %       - eliminate the qlabel parameters, which are essentially not
0023 %         needed.
0024 %       - add functionality to support schemes. In current revision,
0025 %         it supports nearest-neighbor ('nn') and leave-one-out ('loo').
0026 %
0027 
0028 %% parse and verify input arguments
0029 
0030 if nargin < 5
0031     raise_lackinput('slclassify_blks', 5);
0032 end
0033 if ~iscell(scores)
0034     error('sltoolbox:invalidarg', ...
0035         'The scores should be a cell array of filenames');
0036 end
0037 if ~isequal(size(scores), size(blocks))
0038     error('sltoolbox:sizmismatch', ...
0039         'The sizes of scores and blocks are inconsistent');
0040 end
0041 [nrows, ncols] = size(blocks);
0042 if ~ismember(op, {'high', 'low'})
0043     error('sltoolbox:invalidarg', ...
0044         'Invalid score option %s', op);
0045 end
0046 
0047 opts.scheme = 'nn';
0048 opts = slparseprops(opts, varargin{:});
0049 
0050 if ~ismember(opts.scheme, {'nn', 'loo'})
0051     error('sltoolbox:invalidarg', ...
0052         'Invalid scheme for classification: %s', opts.scheme);
0053 end
0054 
0055 
0056 %% Verify regularity of blocks
0057 
0058 rowlims = vertcat(blocks{:, 1});
0059 rowlims = reshape(rowlims(:, 1), [2, nrows]);
0060 collims = vertcat(blocks{1, :});
0061 collims = reshape(collims(:, 2), [2, ncols]);
0062 
0063 for i = 1 : nrows
0064     for j = 1 : ncols
0065         rl = rowlims(:, i);
0066         cl = collims(:, j);
0067         if ~isequal(blocks{i, j}, [rl cl])
0068             error('sltoolbox:sizmismatch', ...
0069                 'The blocks are nonregular');
0070         end
0071     end
0072 end
0073 
0074 
0075 %% Classify
0076 
0077 % first-batch (on first block row)
0078 [decinds, decscores] = procbatch(scores(1, :), n, rowlims(:, 1), collims, op, opts);
0079 
0080 % following batches
0081 
0082 if nrows > 1
0083     
0084     for i = 2 : nrows
0085         [curinds, curscores] = procbatch(scores(i, :), n, rowlims(:, i), collims, op, opts);
0086         
0087         % update
0088         switch op
0089             case 'high'
0090                 to_replace = curscores > decscores;
0091             case 'low'
0092                 to_replace = curscores < decscores;
0093         end
0094         decinds(to_replace) = curinds(to_replace);
0095         decscores(to_replace) = curscores(to_replace);        
0096     end
0097     
0098 end
0099 
0100 % convert indices to decision labels
0101 decisions = clabels(decinds);
0102 if size(decisions, 1) > 1
0103     decisions = decisions';
0104 end
0105 
0106 
0107 %% Internal function to process each block (batch)
0108 
0109 function [decinds, decscores] = procblock(filename, rlim, clim, op, opts)
0110 
0111 matsiz = [rlim(2) - rlim(1) + 1, clim(2) - clim(1) + 1];
0112 s = slreadarray(filename);
0113 if ~isequal(size(s), matsiz)
0114     error('sltoolbox:sizmismatch', ...
0115         'Illegal size of array in %s', filename);
0116 end
0117 
0118 if strcmpi(opts.scheme, 'loo')
0119     % select the disabled scores
0120     sl1 = max(rlim(1), clim(1));
0121     sl2 = min(rlim(2), clim(2));
0122     if sl1 <= sl2 % contains some elements to be disabled
0123         % calculate the local indices
0124         srs = (sl1:sl2) - (rlim(1)-1);
0125         scs = (sl1:sl2) - (clim(1)-1);
0126         sdinds = (scs - 1) * matsiz(1) + srs;        
0127         % disable the selected elements
0128         switch op
0129             case 'high'
0130                 s(sdinds) = -Inf;
0131             case 'low'
0132                 s(sdinds) = Inf;
0133         end
0134     end
0135 end        
0136 
0137 indbase = rlim(1) - 1;
0138 
0139 switch op
0140     case 'high'
0141         [decscores, decinds] = max(s, [], 1);
0142     case 'low'
0143         [decscores, decinds] = min(s, [], 1);
0144 end
0145     
0146 
0147 if indbase ~= 0
0148     decinds = decinds + indbase;
0149 end
0150 
0151 
0152 
0153 function [decinds, decscores] = procbatch(srow, n, rlim, collims, op, opts)
0154 
0155 decinds = zeros(1, n);
0156 decscores = zeros(1, n);
0157 ncols = size(srow, 2);
0158 
0159 for j = 1 : ncols    
0160     sc = collims(1, j);
0161     ec = collims(2, j);
0162     [curinds, curdsco] = procblock(srow{1, j}, rlim, [sc; ec], op, opts);
0163     decinds(sc:ec) = curinds;
0164     decscores(sc:ec) = curdsco;        
0165 end
0166

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

Contact us at files@mathworks.com