-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User-defined metrics #23
Comments
I would suspect that you might not even need the second function. It should fall back to https://github.com/KristofferC/NearestNeighbors.jl/blob/master/src/evaluation.jl#L45 if no method with those argument types for that You are right that a small example in the README would be good. The API is just the one from |
I found I had to define the second function too. Without it I get: ERROR: LoadError: MethodError: `evaluate` has no method matching
evaluate(::dev.MyMetric, ::Array{Float64,2}, ::Array{Float64,1}, ::Int64)
Closest candidates are:
evaluate(::Union{Distances.Chebyshev,Distances.ChiSqDist, [...]
[...]
in create_bsphere at /home/ben/.julia/v0.4/NearestNeighbors/src/hyperspheres.jl:38
in build_BallTree at /home/ben/.julia/v0.4/NearestNeighbors/src/ball_tree.jl:98
in build_BallTree at /home/ben/.julia/v0.4/NearestNeighbors/src/ball_tree.jl:113 (repeats 4 times)
in BallTree at /home/ben/.julia/v0.4/NearestNeighbors/src/ball_tree.jl:67 In fact this is how I found I had to implement this. Feel free to use/point towards the above snippet if this is useful. As far as the API goes, good to know that sticking to the user-facing interface of |
Right now, it might be a bit cheaper to create the |
I added some docs about this. |
For the new version, the second function you defined should not be needed. |
Perhaps I'm missing something, but I'm still having trouble with defining a custom metric. I want to compute the weighted nearest neighbor. That is, each point comprising the tree has an associated weight; points that are weighted highly will seem further away. This is somewhat related to the idea of a weighted Voronoi diagram where the tree members represent generator points in the WVD. Here is a snippet: using NearestNeighbors, Distances
struct WEuclidean <: Metric end
function evaluate(d::WEuclidean, a,b)
w = a[end] * b[end]
return w * euclidean(a[1:end-1], b[1:end-1])
end
anchors = [0.25 0.75; 0.75 0.25; 0.5 1]
balltree = BallTree(anchors, WEuclidean()) So the last element in each vector is its weight. When I query the tree to find nearest neighbors, the query point will have a 1 there. When I run the snippet, I get:
But aren't |
You need to define function Distances.evaluate(...) or state import Distances: evaluate upfront. |
Thanks for a great package.
I was able to do a
knn
search on aBallTree
using my own distance by simply doing this:This is already awesome! What would be even better is if an official API was provided for doing this. This would guarantee that a quick hack like the above will not break at the next update and also that proper checks and optimizations are enforced.
The text was updated successfully, but these errors were encountered: