1 %> @brief k-Nearest Neighbours Classifier
3 %> Posterior probabilities are calculated
using the Laplace estimator [1].
6 %> [1] Kuncheva, Combining Pattern Classifiers, page 154, 2004.
15 properties(SetAccess=private)
16 %> Training data is part of the model in the k-NN algorithm
22 function o = clssr_knn(o)
23 o.classtitle = 'k-Nearest Neighbours
';
28 methods(Access=protected)
30 function o = do_boot(o)
33 function o = do_train(o, data)
34 o.classlabels = data.classlabels;
38 o.classes = o.get_classes(data);
44 %> Uses Laplace estimator for the posterior probabilities
46 %> Reference: Kuncheva, Combining Pattern Classifiers, page 154, 2004.
47 function est = do_use(o, data)
49 no_obs_test = data.no;
50 no_obs_train = size(o.X, 1);
51 no_classes = length(o.classlabels);
53 votes = zeros(no_obs_test, no_classes);
55 est.classlabels = o.classlabels;
56 est = est.copy_from_data(data);
62 v_test = data.X(i, :);
65 dists = sum(bsxfun(@minus, o.X, v_test).^2, 2);
67 [vv, idxs] = sort(dists);
69 for j = 1:min(no_obs_train, o.k)
70 votes(i, o.classes(idxs(j))+1) = votes(i, o.classes(idxs(j))+1)+1; % Here a weight on the vote could be put
77 irverbose(sprintf('%d/%d
', i, no_obs_test));
82 est.X = (votes+1)/(no_classes+o.k); % The Laplace formula
k-Nearest Neighbours Classifier