Skip to main content
. 2014 Oct 22;14(10):19806–19842. doi: 10.3390/s141019806

ALGORITHM 2: K-nearest neighbor pseudocode.

Input: Dataset D= { (x1,c1),…,(xN,cN) }, and unlabeled instance x=(x1,…,xN).
Output: predicted class Ci.
for each classified example (xi,ci) do
  calculate distance d(xi,x)
  order d(xi,x) from lowest to highest
  select k nearest neighbors to x
  vote for majority class among k neighbors, Ci
  return Ci.
end