2023-10-12 23:51:06 +00:00
|
|
|
% 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)
|
|
|
|
|
2023-10-22 04:32:00 +00:00
|
|
|
% perform PCA
|
2023-10-22 06:04:22 +00:00
|
|
|
data = vertcat(training_data, test_data);
|
|
|
|
coeff = pca(data);
|
2023-10-22 04:32:00 +00:00
|
|
|
|
2023-10-22 06:04:22 +00:00
|
|
|
% project data using the number of eigenvectors defined by neigenvectors
|
|
|
|
eigenvectors = coeff(:,1:neigenvectors);
|
|
|
|
projected_data = data * eigenvectors;
|
2023-10-12 23:51:06 +00:00
|
|
|
|
2023-10-22 06:04:22 +00:00
|
|
|
% split matrix back out
|
|
|
|
training_rows = size(training_data, 1);
|
|
|
|
projected_training_data = projected_data(1:training_rows,:);
|
|
|
|
projected_test_data = projected_data(training_rows+1:end,:);
|
2023-10-12 23:51:06 +00:00
|
|
|
|
2023-10-22 06:04:22 +00:00
|
|
|
% compute test error for kNN with differents k's. Fill in
|
2023-10-12 23:51:06 +00:00
|
|
|
% test_errors with the results for each k in ks.
|
|
|
|
test_errors = zeros(1,length(ks));
|
2023-10-22 04:32:00 +00:00
|
|
|
for i = 1:length(ks)
|
|
|
|
k = ks(i);
|
|
|
|
|
2023-10-22 06:04:22 +00:00
|
|
|
test_errors(i) = KNN(k, projected_training_data, projected_test_data, training_labels, test_labels);
|
2023-10-22 04:32:00 +00:00
|
|
|
end
|
2023-10-12 23:51:06 +00:00
|
|
|
|
|
|
|
% 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
|