# K nearest neighbor utility in Julia

**URL:** https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479
**Category:** Community
**Created:** [February 2, 2021, 7:05pm UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479 "2021-02-02T19:05:19Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [February 2, 2021, 7:05pm UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479/1 "2021-02-02T19:05:19Z")

</div>

Hi. Is there a working K-nearest neighbors package or sub-package available in Julia (apart from Scikit Learn)? There used to be kNN but that is not currently working. A search within the past year yields essentially nothing (except a proposed interface). Maybe it exists as a part of another package? Thanks for any help.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [February 2, 2021, 7:18pm UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479/2 "2021-02-02T19:18:14Z")

</div>

[GitHub - KristofferC/NearestNeighbors.jl: High performance nearest neighbor data structures and algorithms for Julia.](https://github.com/KristofferC/NearestNeighbors.jl) might suit your needs.

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [February 2, 2021, 7:29pm UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479/3 "2021-02-02T19:29:00Z")

</div>

Thanks very much for that. I am looking for something for elementary students in applied Data Science to use, with a simple interface. This package certainly looks very powerful, and potentially useful for academics and serious researchers! I was hoping for something like the (defunct) kNN.jl package.

Perhaps I can write them a wrapper using this, that doesn’t require them to understand trees, etc.

---

<div class="post-metadata">

### Author: ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)
#### Post date: [February 2, 2021, 7:44pm UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479/4 "2021-02-02T19:44:15Z")

</div>

Yeah it doesn’t look like rocket science ( imho it’s never bad to understand the parts that make a method work), but you could easily set up an API similar to scikit where fit builds a tree and predict does the search over input points.

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [February 2, 2021, 7:47pm UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479/5 "2021-02-02T19:47:37Z")

</div>

Thanks, yes, in this class we are reviewing and comparing the performance of lots of methods, and don’t have time to go into depth, unfortunately. I probably can write something, but I wondered if something already existed (as I mentioned, the old kNN.jl would have been perfect).

I agree it’s not Rocket science, and am glad of that, because it means that even I might be able to understand it well enough to write the wrapper!

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [February 2, 2021, 9:59pm UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479/6 "2021-02-02T21:59:45Z")

</div>

> [@compleat](#):
>
> This package certainly looks very powerful, and potentially useful for academics and serious researchers! I was hoping for something like the (defunct) kNN.jl package.
> 
> Perhaps I can write them a wrapper using this, that doesn’t require them to understand trees, etc.

They don’t have to “understand” trees. They just call one constructor and then the `knn` function. I think they can handle it 🙂

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [February 3, 2021, 12:42am UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479/7 "2021-02-03T00:42:35Z")

</div>

I still think there are quite a few non-obvious steps involved if one wanted to use this for kNN classification/regression (it would take me a day to figure it out and debug it).

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [February 3, 2021, 1:34am UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479/8 "2021-02-03T01:34:11Z")

</div>

MLJ has kNN classifiers and regressers based on NeatestNeighbors.jl

In general it’s a go-to for “classical machine learning” (and more)

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [February 3, 2021, 8:52am UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479/9 "2021-02-03T08:52:21Z")

</div>

> [@compleat](#):
>
> I still think there are quite a few non-obvious steps involved if one wanted to use this for kNN classification/regression (it would take me a day to figure it out and debug it).

Okay, it seems that you are not really interested in just doing a knn search (which was my initial assumption) but you want a more full fledged classifier framework. Then yes, you probably have to look “higher up” the stack.

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [February 3, 2021, 11:19am UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479/10 "2021-02-03T11:19:09Z")

</div>

> [@oxinabox](#):
>
> MLJ has kNN classifiers and regressers based on NeatestNeighbors.jl
> 
> In general it’s a go-to for “classical machine learning” (and more)

MLJ has kNN classifiers and regressers based on NeatestNeighbors.jl

In general it’s a go-to for “classical machine learning” (and more)

Thanks for that. I thought MLJ was only Neural Networks, etc. (I quite like Flux), but I’ll definitely take a look! This might be what I was hoping to find.

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [February 3, 2021, 12:27pm UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479/11 "2021-02-03T12:27:42Z")

</div>

> [@kristoffer.carlsson](#):
>
> Okay, it seems that you are not really interested in just doing a knn search (which was my initial assumption) but you want a more full fledged classifier framework. Then yes, you probably have to look “higher up” the stack.

Hi. Thank you for your input (and also for the package, which seems to drive the suggestion I will probably go with) I’m sorry I didn’t make my ‘Data Science’ requirement clearer above.

---

<div class="post-metadata">

### Author: ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)
#### Post date: [February 3, 2021, 3:07pm UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479/12 "2021-02-03T15:07:33Z")

</div>

Some nearest neighbor implementations for various data can be found here: [https://github.com/JuliaNeighbors](https://github.com/JuliaNeighbors)

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [February 3, 2021, 3:44pm UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479/13 "2021-02-03T15:44:08Z")

</div>

> [@compleat](#):
>
> Thanks for that. I thought MLJ was only Neural Networks, etc. (I quite like Flux), but I’ll definitely take a look! This might be what I was hoping to find.

MLJ is actually mostly a tool to use and compose “general ML” models though it does in particular interface Flux via MLJFlux. Otherwise it exposes most models from ScikitLearn as well as a bunch of Julia models like NearestNeighbors, DecisionTrees, GLM, LightGBM, MLJLinearModels, …

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [February 3, 2021, 8:11pm UTC](https://discourse.julialang.org/t/k-nearest-neighbor-utility-in-julia/54479/14 "2021-02-03T20:11:34Z")

</div>

Edit: I read too fast you message, I didn’t implemented the KNN algorithm (not yet…), altought it is discussed in the notes cited below. ([here](https://github.com/sylvaticus/MITx_6.86x/blob/master/Unit%2002%20-%20Nonlinear%20Classification%2C%20Linear%20regression%2C%20Collaborative%20Filtering/Unit%2002%20-%20Nonlinear%20Classification%2C%20Linear%20regression%2C%20Collaborative%20Filtering.md))… If you want to collaborate in writing it… 😉

I believe you could find interesting the `cluster` module of [BetaML](https://github.com/sylvaticus/BetaML.jl)… I have wrote it with exactly your user case in mind: something simple for learning the algorithm and a easy to read code, altought not as much performant as in other packages …

Note that the algorithms in BetaML have a companion repository of the [notes of a MITx course in machine learning](https://github.com/sylvaticus/MITx_6.86x) where the algorithms are explained in greater detail…
