Skip to content

Instantly share code, notes, and snippets.

@glesica
Created January 31, 2014 22:02
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 glesica/8744176 to your computer and use it in GitHub Desktop.
Save glesica/8744176 to your computer and use it in GitHub Desktop.
Minimal KNN implementation in Julia.
using StatsBase
function knn(k, train, classes, obs)
nearest = sortperm(vec(sqrt(sum(broadcast((a, b) -> (a-b)^2, obs, train), 2))))[1:k]
return mode(classes[nearest])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment