Write your own Nearest-neighbor classifier

We have begun to write a nearest neighbor classifier, but we have left out the best part. Write the for loop that iterates over every sample in the testing data and assigns a single label from the training targets to each of the test samples based on which is nearest (where ‘nearest’, for now, is defined based on Euclidian distance). Test your classifers on pairwise or multi-class datasets.

Your function should have the following signature:

function predicted=cosmo_classify_nn(samples_train, targets_train, samples_test, unused)
% 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                (currently ignored)
%
% Output:
%   predicted          Qx1 predicted data classes for samples_test
%
% 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);
%     pred=cosmo_classify_nn(tr.samples,tr.sa.targets,te.samples,struct);
%     % show targets and predicted labels (100% accuracy)
%     disp([te.sa.targets pred])
%     %||      1     1
%     %||      2     2
%     %||      3     3
%     %||      4     4
%     %||      5     5
%
% See also: cosmo_crossvalidate, cosmo_crossvalidation_measure
%
% #   For CoSMoMVPA's copyright information and license terms,   #
% #   see the COPYING file distributed with CoSMoMVPA.           #

Hint: cosmo classify nn skl

Full solution: cosmo classify nn

Extra exercise: write a nearest-mean classifier.

Extra exercise: Try correlation instead of Euclidian distance.

Advanced exercise: write a k-nearest neighbor classifier that considers the nearest k neighbors for each test sample. Bonus points if this classifier uses cosmo winner indices in case of a tie. (solution: cosmo classify knn).

Matlab output: run_classify_naive_bayes