
% Does classification for a set of data 'testx'.
% Returns average loss per sample and the classification as a vector.
%
% To load the data files, type "variable = load('filename');",
% replacing the terms variable and filename, with the name of the
% variable in which you want to store the data, and the name of the data file.

function [al, classes] = knn_classify(k, testx, testy, trainx, trainy)

testSize = size(testx);
classes(1:testSize(1)) = 0;
loss = 0;

% for each test sample, do knn classification and compare with actual class.
for i=1:testSize(1);
    % for the specifics of the function knn(), see the file knn.m 
    classes(i) = knn(k, testx(i,:), trainx, trainy);
    if  classes(i) ~= testy(i);
        loss = loss + 1;
    end
end

% return average loss per sample
al = loss / testSize(1);