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 1 commit
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
5 changes: 2 additions & 3 deletions src/NearestNeighbors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const MMI = MLJModelInterface
using Distances

import ..NearestNeighbors
import ..Tables

const NN = NearestNeighbors

Expand Down Expand Up @@ -141,20 +142,18 @@ function MMI.predict(m::KNNRegressor, (tree, y, w), X)

for i in eachindex(idxs)
idxs_ = idxs[i]
println(idxs_)
dists_ = dists[i]
values = [ymat[j,:] for j in idxs_]
if w !== nothing
w_ = w[idxs_]
end
println(preds)
if m.weights == :uniform
preds[i,:] .= sum(values .* w_) / sum(w_)
else
preds[i,:] .= sum(values .* w_ .* (1.0 .- dists_ ./ sum(dists_))) / (sum(w_) - 1)
end
end
if typeof(x) <: AbstractArray
if typeof(y) <: AbstractArray
return preds
Copy link
Member

Choose a reason for hiding this comment

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

Correct but probably less confusing (and future-proof) to use if typeof(y) <: AbstractVector.

else
return MMI.table(preds, names=Tables.schema(y).names, prototype=y)
Expand Down
3 changes: 2 additions & 1 deletion test/NearestNeighbors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using MLJModels.NearestNeighbors_
using CategoricalArrays
using MLJBase
using Random
using Tables

Random.seed!(5151)

Expand Down Expand Up @@ -140,7 +141,7 @@ infos[:docstring]
infos = info_dict(knnr)

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

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