Comments (7)
You need to define
function Distances.evaluate(...)
or state
import Distances: evaluate
upfront.
from nearestneighbors.jl.
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.
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.
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.
I added some docs about this.
from nearestneighbors.jl.
For the new version, the second function you defined should not be needed.
from nearestneighbors.jl.
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)
- README.md Misleading Custom Metric Documentation
- Document that `inrangecount` also counts the point itself HOT 2
- [Question] Can you insert new data into an existing KDTree object? HOT 2
- Compilation time issues with very high dimensions HOT 3
- Reverse Cuthill-McKee ordering option HOT 1
- Querying number of distance evaluations HOT 3
- Make datatypes of the KNN results selectable for potentially lower memory overhead
- Does ball tree work with any metric? HOT 2
- Add example with `skip` option to documentation HOT 1
- Julia 1.10 is waiting on IO to finish during compilation HOT 3
- It should be possible to make `KDNode` smaller
- KDTree: Wrong results for non-Euclidean metrics
- Cannot build KDTree with Subarrays since v0.4.14 HOT 3
- KDTree with Matrix{ComplexF64} HOT 1
- Can't do `knn` on `AbstractVector{SVector}` HOT 2
- Test benchmarks and have them run on CI
- 1.0 road map HOT 5
- `get_min_distance_sq` seems weird
- Interface for tree traversal / walking of BallTree/KDTree HOT 3
- Trees for integer input data errors now it seems
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nearestneighbors.jl.