Skip to content
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

Support multivariate kNN regression #327

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/NearestNeighbors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ end
function MMI.predict(m::KNNRegressor, (tree, y, w), X)
Xmatrix = MMI.matrix(X, transpose=true) # NOTE: copies the data
idxs, dists = NN.knn(tree, Xmatrix, m.K)
preds = zeros(length(idxs))
preds = similar(y, length(idxs))

ablaom marked this conversation as resolved.
Show resolved Hide resolved
w_ = ones(m.K)

Expand Down Expand Up @@ -161,7 +161,7 @@ metadata_pkg.((KNNRegressor, KNNClassifier),

metadata_model(KNNRegressor,
input = Table(Continuous),
target = AbstractVector{Continuous},
target = Union{AbstractVector{Continuous}, AbstractVector{<:AbstractArray{Continuous}}},
ablaom marked this conversation as resolved.
Show resolved Hide resolved
weights = true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

descr = KNNRegressorDescription
)
Expand Down
17 changes: 15 additions & 2 deletions test/NearestNeighbors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ p2 = predict(knnr, f2, xtest)
@test all(p[ntest+1:2*ntest] .≈ 2.0)
@test all(p[2*ntest+1:end] .≈ -2.0)

y1v = fill( [0.0], n)
y2v = fill( [2.0], n)
y3v = fill([-2.0], n)

yv = vcat(y1v, y2v, y3v)

fv,_,_ = fit(knnr, 1, x, yv)
f2v,_,_ = fit(knnr, 1, x, yv, w)

pv = predict(knnr, fv, xtest)

@test all(pv[1:ntest] .≈ [[0.0]])
@test all(pv[ntest+1:2*ntest] .≈ [[2.0]])
@test all(pv[2*ntest+1:end] .≈ [[-2.0]])



Expand All @@ -128,8 +142,7 @@ infos[:docstring]
infos = info_dict(knnr)

@test infos[:input_scitype] == Table(Continuous)
@test infos[:target_scitype] == AbstractVector{Continuous}

@test infos[:target_scitype] == Union{AbstractVector{Continuous}, AbstractVector{<:AbstractArray{Continuous}}}
infos[:docstring]

ablaom marked this conversation as resolved.
Show resolved Hide resolved
end
Expand Down