1 | %KANNC 1-Nearest Neighbor Classifier using ANNQUERY |
---|
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 |
---|
15 | % ANNQUERY package |
---|
16 | % |
---|
17 | % SEE ALSO |
---|
18 | % MAPPINGS, DATASETS, ANNQUERY, KNNC |
---|
19 | |
---|
20 | % Copyright: R.P.W. Duin, r.p.w.duin@prtools.org |
---|
21 | % Faculty EWI, Delft University of Technology |
---|
22 | % P.O. Box 5031, 2600 GA Delft, The Netherlands |
---|
23 | |
---|
24 | |
---|
25 | function out = kannc(a,knn,opt) |
---|
26 | |
---|
27 | prtrace(mfilename); |
---|
28 | |
---|
29 | name = 'KANNC'; |
---|
30 | % No input data, return an untrained classifier. |
---|
31 | if nargin < 3, opt = []; end |
---|
32 | if nargin < 2, knn = 1; end |
---|
33 | if (nargin == 0) | (isempty(a)) |
---|
34 | out = mapping(mfilename,'untrained',{knn,opt}); |
---|
35 | out = setname(out,name); |
---|
36 | elseif isdataset(a) & ~ismapping(knn) % training |
---|
37 | islabtype(a,'crisp'); |
---|
38 | isvaldfile(a,1,2); % at least 1 object per class, 2 classes |
---|
39 | a = testdatasize(a); |
---|
40 | a = testdatasize(a,'objects'); |
---|
41 | a = seldat(a); % get labeled objects only |
---|
42 | [m,k,c] = getsize(a); |
---|
43 | v.data = a; |
---|
44 | v.knn = knn; |
---|
45 | v.opt = opt; |
---|
46 | out = mapping(mfilename,'trained',v,getlablist(a),k,c); |
---|
47 | out = setname(out,name); |
---|
48 | elseif nargin == 2 & ismapping(knn) % execution |
---|
49 | % to avoid confusion, rename trained mapping |
---|
50 | w = knn; |
---|
51 | v = +w; % get datafield |
---|
52 | [k,c] = size(w); |
---|
53 | nlab = getnlab(v.data); |
---|
54 | J = annquery(+v.data',(+a)',v.knn)'; |
---|
55 | n = size(J,1); % no of test objects |
---|
56 | J = nlab(J); |
---|
57 | if size(J,2) == 1 |
---|
58 | out = zeros(2,n); |
---|
59 | out([1:2:n*2]+J'-1) = ones(1,n); |
---|
60 | out = out'; |
---|
61 | else |
---|
62 | out = hist(J,[1:c]); |
---|
63 | end |
---|
64 | % Use Bayes estimators for posteriors |
---|
65 | out = (out+ones(size(out,1),2))/(v.knn+c); |
---|
66 | out = setdat(a,out,w); |
---|
67 | else |
---|
68 | error('Illegal input') |
---|
69 | end |
---|
70 | |
---|
71 | |
---|