Coder Social home page Coder Social logo

Comments (7)

dkarrasch avatar dkarrasch commented on May 22, 2024 1

You need to define

function Distances.evaluate(...)

or state

import Distances: evaluate

upfront.

from nearestneighbors.jl.

KristofferC avatar KristofferC commented on May 22, 2024

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 Metric is defined.

You are right that a small example in the README would be good. The API is just the one from Distances.jl: https://github.com/JuliaStats/Distances.jl#computing-the-distance-between-two-vectors

from nearestneighbors.jl.

BenConnault avatar BenConnault commented on May 22, 2024

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 Distances.jl will work. Looking at metrics.jl in Distances.jl, would implementing eval_op, eval_reduce (and/or something else) lead to better performance than a naive evaluate(d::MyMetric,a, b) interface implementation like I did?

from nearestneighbors.jl.

KristofferC avatar KristofferC commented on May 22, 2024

Right now, it might be a bit cheaper to create the eval_op and company, because I believe that creating the slice is expensive compared to the cost of the evaluate function. However, with some refactoring for #24 it should cost the same.

from nearestneighbors.jl.

KristofferC avatar KristofferC commented on May 22, 2024

I added some docs about this.

from nearestneighbors.jl.

KristofferC avatar KristofferC commented on May 22, 2024

For the new version, the second function you defined should not be needed.

from nearestneighbors.jl.

avonmoll avatar avonmoll commented on May 22, 2024

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:

ERROR: MethodError: no method matching evaluate(::WEuclidean, ::SArray{Tuple{3},Float64,1,3}, ::MArray{Tuple{3},Float64,1,3})
Closest candidates are:
  evaluate(::PreMetric, ::AbstractArray{T,1} where T, ::AbstractArray{T,1} where T, ::Bool) at /home/alex/.julia/packages/NearestNeighbors/N7lgR/src/evaluation.jl:25
  evaluate(::CorrDist, ::AbstractArray, ::AbstractArray) at /home/alex/.julia/packages/Distances/HOWRG/src/metrics.jl:269
  evaluate(::MeanAbsDeviation, ::Any, ::Any) at /home/alex/.julia/packages/Distances/HOWRG/src/metrics.jl:442
  ...

But aren't ::SArray{Tuple{3},Float64,1,3} and ::MArray{Tuple{3},Float64,1,3}) subtypes of Any?

from nearestneighbors.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.