%% matlab_demo_07.m
%%
%% CSS 581, Introduction to Machine Learning, Winter 2014
%% Computing & Software Systems, University of Washington Bothell
%% written by J. Jeffry Howbert, 2012-01-31

%% demo of nearest neighbor classification on MNIST dataset
clear all;
close all;
load mnistabridged;

trainData = double( train );
testData = double( test );
trainLabel = double( trainlabels );
testLabel = double( testlabels );
nTrain = length( trainlabels );
nTest = length( testlabels );

% %%&& for test only
% nTestSub = 100;
% testData = testData( 1 : nTestSub, : );
% testLabel = testLabel( 1 : nTestSub );
% nTest = length( testLabel );
% %%&& end for test only

tic;
k = 1;
testPredIdx = knnsearch( trainData, testData, 'K', k );   %%&& knnsearch() doesn't exist in R2009a!!
testPredAll = trainLabel( testPredIdx );    % get labels of all k-nearest neighbors for each test sample
testPred = mode( testPredAll, 2 );          % choose most common nn label for each test sample
toc;

% % poor man's k-nearest neighbor
% tic;
% testMinDistIdx = zeros( 1, nTest );
% for ix = 1 : nTest
%     % get difference between vector of test sample and all training samples
%     diff = trainData - repmat( testData( ix, : ), nTrain, 1 );
%     % element-wise squares of difference vector
%     diff2 = diff .^ 2;
%     % add up squares to get square of Euclidean difference - don't need to
%     % take square root
%     sumDiff2 = sum( diff2, 2 );
%     [ testMinDist, testMinDistIdx( ix ) ] = min( sumDiff2 );
% end
% testPred = trainLabel( testMinDistIdx );
% toc;

nTestCorrect = sum( testLabel == testPred );
fprintf( 1, 'correct predictions on test set     :   %d / %d,  %5.2f%%\n', ...
    nTestCorrect, nTest, 100 * nTestCorrect / nTest );
[ mat, order ] = confusionmat( testLabel, testPred );
disp( ' ' );
disp( order' );
disp( mat );

