csci5521/assignments/hwk02/KNN_Error.m
2023-10-21 23:32:00 -05:00

28 lines
No EOL
914 B
Matlab

% implements KNN_Error, returns nothing but prints out a table of test
% errors for k-nearest neighbors using different values of k.
function [] = KNN_Error(neigenvectors, ks, training_data, test_data, training_labels, test_labels)
% perform PCA
% TODO: project data using the number of eigenvectors defined by neigenvectors
% TODO: compute test error for kNN with differents k's. Fill in
% test_errors with the results for each k in ks.
test_errors = zeros(1,length(ks));
for i = 1:length(ks)
k = ks(i);
test_errors(i) = KNN(k, training_data, test_data, training_labels, test_labels);
end
% print error table
fprintf("-----------------------------\n");
fprintf("| k | test error\n")
fprintf("-----------------------------\n");
for i = 1:length(ks)
fprintf("| %d | %.8f \n", ks(i), test_errors(i));
end
fprintf("-----------------------------\n");
end % Function end