source: prextra/kannc.m @ 7

Last change on this file since 7 was 7, checked in by bduin, 14 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        name = 'KANNC';
31        % No input data, return an untrained classifier.
32        if nargin < 3, opt = []; end
33        if nargin < 2, knn = 1; end
34        if (nargin == 0) | (isempty(a))
35                out = mapping(mfilename,'untrained',{knn,opt});
36                out = setname(out,name);
37        elseif isdataset(a) & ~ismapping(knn) % training
38                islabtype(a,'crisp');
39                isvaldfile(a,1,2); % at least 1 object per class, 2 classes
40                a = testdatasize(a);
41                a = testdatasize(a,'objects');
42                a = seldat(a);    % get labeled objects only
43                [m,k,c] = getsize(a);
44                v.data = a;
45                v.knn  = knn;
46                v.opt  = opt;
47                out = mapping(mfilename,'trained',v,getlablist(a),k,c);
48                out = setname(out,name);
49        elseif nargin == 2 & ismapping(knn)  % execution
50                % to avoid confusion, rename trained mapping
51                w = knn;
52                v = +w;   % get datafield
53                [k,c] = size(w);
54                nlab = getnlab(v.data);
55                J = annquery(+v.data',(+a)',v.knn)';
56                n = size(J,1); % no of test objects
57                J = nlab(J);
58                if size(J,2) == 1
59                        out = zeros(2,n);
60                        out([1:2:n*2]+J'-1) = ones(1,n);
61                        out = out';
62                else
63                        out = hist(J',[1:c])';
64                end
65                % Use Bayes estimators for posteriors
66                out = (out+ones(size(out,1),c))/(v.knn+c);
67                out = setdat(a,out,w);
68        else
69                error('Illegal input')
70        end
71               
72       
Note: See TracBrowser for help on using the repository browser.