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
26 changes: 20 additions & 6 deletions src/NearestNeighbors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,40 @@ 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 = similar(y, length(idxs))

if typeof(y) <: AbstractVector
ymat = reshape(y, length(y), 1)
preds = similar(ymat, length(idxs), 1)
else # for multi-target prediction
ymat = MMI.matrix(y)
preds = similar(ymat, length(idxs), size(ymat, 2))
end

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

for i in eachindex(idxs)
idxs_ = idxs[i]
println(idxs_)
dists_ = dists[i]
values = y[idxs_]
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_)
preds[i,:] .= sum(values .* w_) / sum(w_)
else
preds[i] = sum(values .* w_ .* (1.0 .- dists_ ./ sum(dists_))) / (sum(w_) - 1)
preds[i,:] .= sum(values .* w_ .* (1.0 .- dists_ ./ sum(dists_))) / (sum(w_) - 1)
end
end
return preds
if typeof(x) <: 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)
end
end


# ====

metadata_pkg.((KNNRegressor, KNNClassifier),
Expand All @@ -161,7 +175,7 @@ metadata_pkg.((KNNRegressor, KNNClassifier),

metadata_model(KNNRegressor,
input = Table(Continuous),
target = Union{AbstractVector{Continuous}, AbstractVector{<:AbstractArray{Continuous}}},
target = Union{AbstractVector{Continuous}, Table{Continuous}},
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
16 changes: 7 additions & 9 deletions test/NearestNeighbors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,19 @@ 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)
ymat = vcat(fill( 0.0, n, 2), fill(2.0, n, 2), fill(-2.0, n, 2))
yv = Tables.table(ymat; header = [:a, :b])

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]])

for col in [:a, :b]
@test all(pv[col][1:ntest] .≈ [0.0])
@test all(pv[col][ntest+1:2*ntest] .≈ [2.0])
@test all(pv[col][2*ntest+1:end] .≈ [-2.0])
end



Expand Down