Skip to content

Instantly share code, notes, and snippets.

@ajbrock
Created October 23, 2017 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajbrock/b78afe0c4a6c16c083b74ce7e119386a to your computer and use it in GitHub Desktop.
Save ajbrock/b78afe0c4a6c16c083b74ce7e119386a to your computer and use it in GitHub Desktop.
%% knn Custom K-Nearest Neighbors Function
% class = knn_all(x,labels,K,weighted)
%
% A Brock, 29.9.15
%
% This function is a modification of the knn function to process the KNN-value of
% each member of a dataset. It uses parallel processing to quickly compute the class of
% x based on the K nearest neighbors of each member of x in x. The user can choose if
% they want to use the weighted version of the algorithm, which allows the
% K nearest neighbors to "vote" for the desired class, with the weight of
% their vote based on the inverse of their distance from the record
%
% The x data must be a gpuArray ordered with each row containing a record and
% each column containing a field. The labels data must be a column vector
% containing the class labels, where the number of possible labels is equal
% to max(labels)+1, so for a binary classifier this would be max(labels) =
% 1 so #labels = 1+1 = 2. It is also assumed that the minimum class value
% is 0.
%
%
% This function uses parallel computing to place the computational burden
% on the GPU and to speed up processing speed.
function class = knn_all(x,labels,K,weighted)
class = gpuArray(zeros(size(x,1),1));
% cv = gpuArray(zeros(max(labels)+1,1));
cv = zeros(max(labels)+1,1);
for i = 1:length(class)
d = sum((bsxfun(@minus,x,x(i,:)).^2),2); % d is a gpuarray
d(i) = inf; % Make sure that the current record is not included. Could also just ignore the very firstest entry.
% Locate K nearest neighbors
cv(:) = 0;
for k = 1:K
[dist,NNk] = min(d);
NN = gather(NNk);
d(NN) = inf;
if ~weighted
cv(labels(NN)+1) = cv(labels(NN)+1) + 1;
else
cv(labels(NN)+1) = cv(labels(NN)+1) + gather(1/max(dist,eps));
end
end
[~,class(i)] = max(cv);
end
class = gather(class - 1);
end
% redacted: cv(labels(NN)+1) = cv(labels(NN)+1) + gather(weighted/max(dist,eps)) + (1-weighted);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment