Comments (4)
The restriction on the metrics for KDTree
is of algorithmic type and has nothing to do with the given implementation here. But BallTree
and BruteTree
should work with pretty much any metric from Distances.jl.
from nearestneighbors.jl.
Thank you for your answer, I re-read the code and I agree that it should work.
I had read too fast the time prior, you made a special case for some metrics in the hypersphere, but there is another code path.
when I change runtest.jl to run the BallTree with the SqEuclidean metric, I get an error:
using NearestNeighbors
using StaticArrays
using Test
using LinearAlgebra
using Distances: Distances, Metric, evaluate, PeriodicEuclidean, SqEuclidean # CHANGED HERE
struct CustomMetric1 <: Metric end
Distances.evaluate(::CustomMetric1, a::AbstractVector, b::AbstractVector) = maximum(abs.(a .- b))
function NearestNeighbors.interpolate(::CustomMetric1,
a::V,
b::V,
x,
d,
ab) where {V <: AbstractVector}
idx = (abs.(b .- a) .>= d - x)
c = copy(Array(a))
c[idx] = (1 - x / d) * a[idx] + (x / d) * b[idx]
return c, true
end
struct CustomMetric2 <: Metric end
Distances.evaluate(::CustomMetric2, a::AbstractVector, b::AbstractVector) = norm(a - b) / (norm(a) + norm(b))
# TODO: Cityblock()
const metrics = [SqEuclidean()] # CHANGED HERE
const fullmetrics = metrics # CHANGED HERE
const trees = [BallTree] # CHANGED HERE
const trees_with_brute = trees # CHANGED HERE
include("test_knn.jl")
include("test_inrange.jl")
include("test_monkey.jl")
include("test_datafreetree.jl")
@testset "periodic euclidean" begin
pred = PeriodicEuclidean([Inf, 2.5])
l = [0.0 0.0; 0.0 2.5]
S = BallTree(l, pred)
@test inrange(S,[0.0,0.0], 1e-2, true) == [1, 2]
end
error message:
tree type: Error During Test at /Users/nraynaud/.julia/dev/NearestNeighbors/test/test_knn.jl:7
Got exception outside of a @test
MethodError: no method matching BallTree(::Matrix{Float64}, ::SqEuclidean; leafsize=2)
Closest candidates are:
BallTree(::AbstractVecOrMat{T}) where T<:AbstractFloat at ~/.julia/packages/NearestNeighbors/VZzTb/src/ball_tree.jl:84 got unsupported keyword argument "leafsize"
BallTree(::AbstractVecOrMat{T}, ::M; leafsize, storedata, reorder, reorderbuffer) where {T<:AbstractFloat, M<:Metric} at ~/.julia/packages/NearestNeighbors/VZzTb/src/ball_tree.jl:84
Stacktrace:
[1] (::var"#test#1"{SqEuclidean, UnionAll})(data::Matrix{Float64})
@ Main ~/.julia/dev/NearestNeighbors/test/test_knn.jl:9
[2] macro expansion
@ ~/.julia/dev/NearestNeighbors/test/test_knn.jl:64 [inlined]
is that expected?
Edit: for context I'm new to julia, so I may completely misread the situation.
from nearestneighbors.jl.
Sorry, now I was too quick. From the error message, we learn that BallTree
requires a Metric
, most likely because it assumes validity of the triangle inequality to build the ball tree. As for the kdtree, yet another, additional assumption is required ("Minkowski metric").
I don't think there is oversight here, this package is very carefully written. It may just be that there is sparse documentation with regard to the underlying assumptions, but I haven't looked at docs and code for a while.
from nearestneighbors.jl.
I see, I was under the impression that everything the euclidean distance did could be done by the square euclidean distance. I was under the impression that the square root function being monotonous meant that it wouldn't mess with distance comparisons.
Thank you for your time.
from nearestneighbors.jl.
Related Issues (20)
- knn: skipped items output when there is a skip function has always the last index and not 0 index HOT 3
- `nn` lacking docstring HOT 1
- AssertionError in kmedoids alg
- bug: BoundsError when the skip function returns true for all points HOT 3
- 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 2
- 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
- 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}
- Can't do `knn` on `AbstractVector{SVector}`
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.