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

Generalize KNNRegressor to multitarget case #328

Merged
merged 23 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
63004f7
Merge pull request #313 from alan-turing-institute/dev
ablaom Oct 12, 2020
67e775a
Merge pull request #316 from alan-turing-institute/dev
ablaom Oct 12, 2020
eb59c99
Merge pull request #320 from alan-turing-institute/dev
ablaom Oct 13, 2020
e07b3d5
Merge pull request #324 from alan-turing-institute/dev
ablaom Oct 16, 2020
b02cede
Merge pull request #326 from alan-turing-institute/dev
ablaom Oct 19, 2020
a4c8610
support multivariate kNN regression
mateuszbaran Oct 19, 2020
0b8c3bd
updated target of kNN regressor
mateuszbaran Oct 20, 2020
865a352
changing target of KNNRegressor
mateuszbaran Oct 20, 2020
3062a05
target of kNN regressor again
mateuszbaran Oct 20, 2020
e52ea0a
trying to make the multi-target kNN regressor work with tables
mateuszbaran Oct 21, 2020
a794ade
fixing kNN regressor
mateuszbaran Oct 21, 2020
6f28040
code review fixes
mateuszbaran Oct 22, 2020
9bc0dfc
update model registry
ablaom Oct 22, 2020
16ac6bf
update registry again
ablaom Oct 22, 2020
e7d5853
fix check_registry issue
ablaom Oct 22, 2020
46fea19
Update NearestNeighbors.jl
OkonSamuel Oct 22, 2020
d36948e
fix wrong call signature
OkonSamuel Oct 22, 2020
5325d5b
Update NearestNeighbors.jl
OkonSamuel Oct 22, 2020
a229988
replace `Tables.schema` with `MMI.schema`
OkonSamuel Oct 22, 2020
a1a9ca2
Update NearestNeighbors.jl
OkonSamuel Oct 22, 2020
ea2d9de
Update NearestNeighbors.jl
OkonSamuel Oct 22, 2020
dedcaee
Merge branch 'dev' of https://github.com/alan-turing-institute/MLJMod…
ablaom Oct 26, 2020
c89f593
Merge branch 'dev' into multiple-regression-knn2
ablaom Oct 27, 2020
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
27 changes: 24 additions & 3 deletions src/NearestNeighbors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ 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)
return _predict(m, y, idxs, dists, w)
end
function _predict(m::KNNRegressor, y::AbstractVector, idxs, dists, w)
preds = zeros(length(idxs))

w_ = ones(m.K)

for i in eachindex(idxs)
idxs_ = idxs[i]
dists_ = dists[i]
Expand All @@ -148,6 +149,26 @@ function MMI.predict(m::KNNRegressor, (tree, y, w), X)
return preds
end

function _predict(m::KNNRegressor, y, idxs, dists, w)
ymat = MMI.matrix(y)
preds = similar(ymat, length(idxs), size(ymat, 2))
w_ = ones(m.K)
for i in eachindex(idxs)
idxs_ = idxs[i]
dists_ = dists[i]
values = [view(ymat, j, :) for j in idxs_]
if w !== nothing
w_ = w[idxs_]
end
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
return MMI.table(preds, names=MMI.schema(y).names, prototype=y)
end

# ====

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

metadata_model(KNNRegressor,
input = Table(Continuous),
target = AbstractVector{Continuous},
target = Union{AbstractVector{Continuous}, Table(Continuous)},
weights = true,
descr = KNNRegressorDescription
)
Expand Down
2 changes: 1 addition & 1 deletion src/registry/Metadata.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3319,4 +3319,4 @@
":implemented_methods" = []
":hyperparameters" = "`(:builder, :optimiser, :loss, :epochs, :batch_size, :lambda, :alpha, :optimiser_changes_trigger_retraining)`"
":hyperparameter_types" = "`(\"Any\", \"Any\", \"Any\", \"Int64\", \"Int64\", \"Float64\", \"Float64\", \"Bool\")`"
":hyperparameter_ranges" = "`(nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing)`"
":hyperparameter_ranges" = "`(nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing)`"
3 changes: 2 additions & 1 deletion src/registry/src/check_registry.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function check_registry()
basedir = joinpath(dirname(pathof(MLJModels)), "registry")
# basedir = joinpath(dirname(pathof(MLJModels)), "registry")
basedir = Registry.environment_path
Pkg.activate(basedir)

# Read Metadata.toml
Expand Down
16 changes: 14 additions & 2 deletions 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 @@ -109,7 +110,19 @@ p2 = predict(knnr, f2, xtest)
@test all(p[ntest+1:2*ntest] .≈ 2.0)
@test all(p[2*ntest+1:end] .≈ -2.0)

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)

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 All @@ -128,8 +141,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}, Table(Continuous)}
infos[:docstring]

end
Expand Down