[6] | 1 | %KANNC 1-Nearest Neighbor Classifier using ANN Matlab Wrapper |
---|
[5] | 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 |
---|
[6] | 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 |
---|
[5] | 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 | |
---|
| 26 | function out = kannc(a,knn,opt) |
---|
| 27 | |
---|
| 28 | prtrace(mfilename); |
---|
| 29 | |
---|
[8] | 30 | annquerycheck; |
---|
[5] | 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 | elseif nargin == 2 & ismapping(knn) % execution |
---|
| 51 | % to avoid confusion, rename trained mapping |
---|
| 52 | w = knn; |
---|
| 53 | v = +w; % get datafield |
---|
| 54 | [k,c] = size(w); |
---|
| 55 | nlab = getnlab(v.data); |
---|
| 56 | J = annquery(+v.data',(+a)',v.knn)'; |
---|
| 57 | n = size(J,1); % no of test objects |
---|
| 58 | J = nlab(J); |
---|
| 59 | if size(J,2) == 1 |
---|
| 60 | out = zeros(2,n); |
---|
| 61 | out([1:2:n*2]+J'-1) = ones(1,n); |
---|
| 62 | out = out'; |
---|
| 63 | else |
---|
[7] | 64 | out = hist(J',[1:c])'; |
---|
[5] | 65 | end |
---|
| 66 | % Use Bayes estimators for posteriors |
---|
[7] | 67 | out = (out+ones(size(out,1),c))/(v.knn+c); |
---|
[5] | 68 | out = setdat(a,out,w); |
---|
| 69 | else |
---|
| 70 | error('Illegal input') |
---|
| 71 | end |
---|
| 72 | |
---|
| 73 | |
---|