function predicted=cosmo_classify_knn(samples_train, targets_train, samples_test, opt)
% k-nearest neighbor classifier
%
% predicted=cosmo_classify_nn(samples_train, targets_train, samples_test[, opt])
%
% Inputs:
% samples_train PxR training data for P samples and R features
% targets_train Px1 training data classes
% samples_test QxR test data
% opt struct with field:
% .knn Number of nearest neighbors to be considered
% .norm distance norm (default: 2)
%
% Output:
% predicted Qx1 predicted data classes for samples_test. Each
% predicted sample is laballed as having the most
% samples in the training set within the nearest
% opt.knn samples.
%
% Example:
% ds=cosmo_synthetic_dataset('ntargets',5,'nchunks',15);
% test_chunk=1;
% te=cosmo_slice(ds,ds.sa.chunks==test_chunk);
% tr=cosmo_slice(ds,ds.sa.chunks~=test_chunk);
% opt=struct();
% opt.knn=2;
% pred=cosmo_classify_knn(tr.samples,tr.sa.targets,te.samples,opt);
% % show targets and predicted labels (40% accuracy)
% disp([te.sa.targets pred])
% %|| 1 1
% %|| 2 3
% %|| 3 3
% %|| 4 4
% %|| 5 5
% %
% opt.norm=1; % city-block distance
% pred=cosmo_classify_knn(tr.samples,tr.sa.targets,te.samples,opt);
% % show targets and predicted labels (40% accuracy)
% disp([te.sa.targets pred])
% %|| 1 1
% %|| 2 3
% %|| 3 3
% %|| 4 4
% %|| 5 5
%
% Notes:
% - in the case of knn=1, this function is identical to cosmo_classify_nn
%
% See also: cosmo_crossvalidate, cosmo_crossvalidation_measure
%
% # For CoSMoMVPA's copyright information and license terms, #
% # see the COPYING file distributed with CoSMoMVPA. #