1 | %PE_KNNC K-Nearest Neighbor Classifier for PE spaces
|
---|
2 | %
|
---|
3 | % [W,K,E] = PE_KNNC(A,K)
|
---|
4 | % [W,K,E] = PE_KNNC(A)
|
---|
5 | %
|
---|
6 | % INPUT
|
---|
7 | % A PE dataset
|
---|
8 | % K Number of the nearest neighbors (optional; default: K is
|
---|
9 | % optimized with respect to the leave-one-out error on A)
|
---|
10 | %
|
---|
11 | % OUTPUT
|
---|
12 | % W k-NN classifier
|
---|
13 | % K Number of the nearest neighbors used
|
---|
14 | % E The leave-one-out error of the KNNC
|
---|
15 | %
|
---|
16 | % DESCRIPTION
|
---|
17 | % Computation of the K-nearest neighbor classifier for the PE dataset A.
|
---|
18 | %
|
---|
19 | % Warning: class prior probabilities in A are neglected.
|
---|
20 | %
|
---|
21 | % SEE ALSO
|
---|
22 | % MAPPINGS, DATASETS, KNNC, PE_EM
|
---|
23 |
|
---|
24 | % R.P.W. Duin, r.p.w.duin@prtools.org
|
---|
25 | % Faculty EWI, Delft University of Technology
|
---|
26 | % P.O. Box 5031, 2600 GA Delft, The Netherlands
|
---|
27 |
|
---|
28 | function [w,k,e] = pe_knnc(a,k)
|
---|
29 |
|
---|
30 | if nargin < 2, k = []; end
|
---|
31 |
|
---|
32 | if nargin == 0 | isempty(a)
|
---|
33 | w = mapping(mfilename,'untrained',{k});
|
---|
34 | w = setname(w,'PE K-NN Classifier');
|
---|
35 |
|
---|
36 | elseif ~ismapping(k) % training
|
---|
37 |
|
---|
38 | if ~ispe_dataset(a)
|
---|
39 | [w,k] = knnc(a,k);
|
---|
40 | else
|
---|
41 | if isempty(k) % optimize k in PE space
|
---|
42 | d = pe_distm(a); % find PE distances
|
---|
43 | [v,k,e] = knndc(d,k); % use dis mat routine for optimisation k
|
---|
44 | elseif nargout > 2
|
---|
45 | e = testkd(pe_distm(a),k,'loo');
|
---|
46 | end
|
---|
47 | w = mapping(mfilename,'trained',{a,k},getlablist(a),size(a,2),getsize(a,3));
|
---|
48 | end
|
---|
49 |
|
---|
50 | else % execution, testset is in a, trained mapping is in k
|
---|
51 |
|
---|
52 | %retrieve data
|
---|
53 | trainset = getdata(k,1);
|
---|
54 | k = getdata(k,2);
|
---|
55 | d = pe_distm(a,trainset);
|
---|
56 | [e,w] = testkd(d,k); % confidences in w
|
---|
57 |
|
---|
58 | end
|
---|
59 |
|
---|
60 | return |
---|