-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastKNN.m
34 lines (24 loc) · 1000 Bytes
/
fastKNN.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
% The MIT License (MIT)
%
% Copyright (c) 2016 Markus Bergholz
% https://github.com/markuman/fastKNN
function [classified, k, dist, idx] = fastKNN(trained, unknown, k, distance)
if (nargin <= 3)
% Minkowski Distance
% for p == 2, Minkowski becomes equal Euclidean
% for p == 1, Minkowski becomes equal city block metric
% for p ~= 1 && p ~= 2 -> Minkowski https://en.wikipedia.org/wiki/Minkowski_distance
distance = 2;
end
% trained data has one more column as unknown, the class
[dist, idx] = getDistance(trained, unknown, distance);
if (nargin <= 2)
% determine k value when no one is given
% possible number of categories + 1
k = numel( unique( trained(:,end) ) ) + 1;
end
classified = mode(trained(idx(1:k), end));
end % function fastKNN
function [values, idx] = getDistance(x, y, p)
[values, idx] = sort (sum ( abs( (x(:,1:end-1) - y(ones(size(x,1),1), :) ) .^p), 2) .^ (1 / p));
end