-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add NearestNeighbors.jl models * Fix tests
- Loading branch information
Showing
6 changed files
with
131 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
const DTModel = Union{ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
const DecisionTreeModel = Union{ | ||
AdaBoostStumpClassifier, | ||
DecisionTreeClassifier, | ||
RandomForestClassifier, | ||
DecisionTreeRegressor, | ||
RandomForestRegressor | ||
} | ||
|
||
function fit(model::DTModel, input, output) | ||
function fit(model::DecisionTreeModel, input, output) | ||
cols = Tables.columns(output) | ||
names = Tables.columnnames(cols) | ||
outcol = first(names) | ||
y = Tables.getcolumn(cols, outcol) | ||
outnm = first(names) | ||
y = Tables.getcolumn(cols, outnm) | ||
X = Tables.matrix(input) | ||
DT.fit!(model, X, y) | ||
FittedModel(model, outcol) | ||
FittedModel(model, outnm) | ||
end | ||
|
||
function predict(fmodel::FittedModel{<:DTModel}, table) | ||
outcol = fmodel.cache | ||
function predict(fmodel::FittedModel{<:DecisionTreeModel}, table) | ||
outnm = fmodel.cache | ||
X = Tables.matrix(table) | ||
ŷ = DT.predict(fmodel.model, X) | ||
(; outcol => ŷ) |> Tables.materializer(table) | ||
(; outnm => ŷ) |> Tables.materializer(table) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
abstract type NearestNeighborsModel end | ||
|
||
struct KNNClassifier{M<:Metric} <: NearestNeighborsModel | ||
k::Int | ||
metric::M | ||
leafsize::Int | ||
reorder::Bool | ||
end | ||
|
||
KNNClassifier(k, metric=Euclidean(); leafsize=10, reorder=true) = KNNClassifier(k, metric, leafsize, reorder) | ||
|
||
struct KNNRegressor{M<:Metric} <: NearestNeighborsModel | ||
k::Int | ||
metric::M | ||
leafsize::Int | ||
reorder::Bool | ||
end | ||
|
||
KNNRegressor(k, metric=Euclidean(); leafsize=10, reorder=true) = KNNRegressor(k, metric, leafsize, reorder) | ||
|
||
function fit(model::NearestNeighborsModel, input, output) | ||
cols = Tables.columns(output) | ||
outnm = Tables.columnnames(cols) |> first | ||
outcol = Tables.getcolumn(cols, outnm) | ||
_checkoutput(model, outcol) | ||
(; metric, leafsize, reorder) = model | ||
data = Tables.matrix(input, transpose=true) | ||
tree = if metric isa MinkowskiMetric | ||
NN.KDTree(data, metric; leafsize, reorder) | ||
else | ||
NN.BallTree(data, metric; leafsize, reorder) | ||
end | ||
FittedModel(model, (tree, outnm, outcol)) | ||
end | ||
|
||
function predict(fmodel::FittedModel{<:NearestNeighborsModel}, table) | ||
(; model, cache) = fmodel | ||
tree, outnm, outcol = cache | ||
data = Tables.matrix(table, transpose=true) | ||
indvec, _ = NN.knn(tree, data, model.k) | ||
aggfun = _aggfun(model) | ||
ŷ = [aggfun(outcol[inds]) for inds in indvec] | ||
(; outnm => ŷ) |> Tables.materializer(table) | ||
end | ||
|
||
function _checkoutput(::KNNClassifier, x) | ||
if !(elscitype(x) <: DST.Categorical) | ||
throw(ArgumentError("output column must be categorical")) | ||
end | ||
end | ||
|
||
function _checkoutput(::KNNRegressor, x) | ||
if !(elscitype(x) <: DST.Continuous) | ||
throw(ArgumentError("output column must be continuous")) | ||
end | ||
end | ||
|
||
_aggfun(::KNNClassifier) = mode | ||
_aggfun(::KNNRegressor) = mean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters