source: prextra/kannc.m @ 113

Last change on this file since 113 was 31, checked in by bduin, 12 years ago
File size: 1.9 KB
Line 
1%KANNC 1-Nearest Neighbor Classifier using ANN Matlab Wrapper
2%
3%   W = KANNC(A,K,OPTION)
4%
5% INPUT
6%   A      Dataset
7%   K      Number of desired neighbours
8%   OPTION Options for ANNQUERY
9%
10% OUTPUT
11%   W      NN classifier using the ANN Query package
12%
13% DESCRIPTION 
14% This is the nearest neighbor implementation for PRTools using the ANN
15% Matlab Wrapper package. It should be in the path. If needed download it
16% http://webscripts.softpedia.com/scriptDownload/ANN-MATLAB-Wrapper-Download-33976.html
17%
18% SEE ALSO
19% MAPPINGS, DATASETS, ANNQUERY, KNNC
20
21% Copyright: R.P.W. Duin, r.p.w.duin@prtools.org
22% Faculty EWI, Delft University of Technology
23% P.O. Box 5031, 2600 GA Delft, The Netherlands
24
25
26function out = kannc(a,knn,opt)
27
28        prtrace(mfilename);
29       
30        annquerycheck;
31        name = 'KANNC';
32        % No input data, return an untrained classifier.
33        if nargin < 3, opt = []; end
34        if nargin < 2, knn = 1; end
35        if (nargin == 0) | (isempty(a))
36                out = mapping(mfilename,'untrained',{knn,opt});
37                out = setname(out,name);
38        elseif isdataset(a) & ~ismapping(knn) % training
39                islabtype(a,'crisp');
40                isvaldfile(a,1,2); % at least 1 object per class, 2 classes
41                a = testdatasize(a);
42                a = testdatasize(a,'objects');
43                a = seldat(a);    % get labeled objects only
44                [m,k,c] = getsize(a);
45                v.data = a;
46                v.knn  = knn;
47                v.opt  = opt;
48                out = mapping(mfilename,'trained',v,getlablist(a),k,c);
49                out = setname(out,name);
50                out = setbatch(out,0);
51        elseif nargin == 2 & ismapping(knn)  % execution
52                % to avoid confusion, rename trained mapping
53                w = knn;
54                v = +w;   % get datafield
55                [k,c] = size(w);
56                nlab = getnlab(v.data);
57                J = annquery(+v.data',(+a)',v.knn)';
58                n = size(J,1); % no of test objects
59                J = nlab(J);
60                if size(J,2) == 1
61                        out = zeros(c,n);
62                        out([1:c:n*c]+J'-1) = ones(1,n);
63                        out = out';
64                else
65                        out = hist(J',[1:c])';
66                end
67                % Use Bayes estimators for posteriors
68                out = (out+ones(size(out,1),c))/(v.knn+c);
69                out = setdat(a,out,w);
70        else
71                error('Illegal input')
72        end
73               
74       
Note: See TracBrowser for help on using the repository browser.