Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/" # Location of package manifests
schedule:
interval: "monthly"
- package-ecosystem: "julia"
directories: # Location of Julia projects
- "/"
- "/docs"
schedule:
interval: "weekly"
ignore:
- dependency-name: "NearestNeighborModels"
19 changes: 0 additions & 19 deletions .github/workflows/CompatHelper.yml

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ jobs:
fail-fast: false
matrix:
version:
- '1.6'
- 'lts'
- '1'
os:
- ubuntu-latest
arch:
- x64
steps:
- uses: actions/checkout@v3
- uses: julia-actions/setup-julia@v1
- uses: julia-actions/setup-julia@v3
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
- uses: julia-actions/cache@v2
env:
cache-name: cache-artifacts
with:
Expand Down
8 changes: 4 additions & 4 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ NearestNeighborModels = "636a865e-7cf4-491e-846c-de09b730eb36"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"

[compat]
Documenter = "^0.24"
MLJBase = "^0.19"
StableRNGs = "^1.0"
julia = "^1.0"
Documenter = "1"
MLJBase = "1"
StableRNGs = "1.0"
julia = "1.10"
10 changes: 5 additions & 5 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ using Documenter, MLJBase, NearestNeighborModels

makedocs(;
authors = """
Anthony D. Blaom <anthony.blaom@gmail.com>,
Sebastian Vollmer <s.vollmer.4@warwick.ac.uk>,
Thibaut Lienart <thibaut.lienart@gmail.com> and
Okon Samuel <okonsamuel50@gmail.com>
Thibaut Lienart <thibaut.lienart@gmail.com> and
Anthony D. Blaom <anthony.blaom@gmail.com>,
Sebastian Vollmer <s.vollmer.4@warwick.ac.uk>,
""",
format = Documenter.HTML(;
prettyurls= get(ENV, "CI", "false") == "true"
),
modules = [NearestNeighborModels],
pages=[
pages = [
"Home" => "index.md",
"API" => "api.md"
],
doctest = false, # don't runt doctest as doctests are automatically run separately in ci.
repo = "https://github.com/JuliaAI/NearestNeighborModels.jl/blob/{commit}{path}#L{line}",
repo = Remotes.GitHub("JuliaAI", "NearestNeighborModels"),
sitename = "NearestNeighborModels.jl",
)

Expand Down
68 changes: 38 additions & 30 deletions docs/src/index.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# NearestNeighborModels - Docs

NearestNeighborModels is a julia package providing implemtation of various
k-nearest-neighbor classifiers and regressors models for use with
[MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/) machine learning
framework. It also provides users with an array of weighting kernels to choose
from for prediction.
NearestNeighborModels is a julia package providing implemtation of various
k-nearest-neighbor classifiers and regressors models for use with
[MLJ](https://juliaml.ai) machine learning framework. It also provides users with an array
of weighting kernels to choose from for prediction.

NearestNeighborModels builds on Kristoffer Carlsson's
[NearestNeighbors](https://github.com/KristofferC/NearestNeighbors.jl) package(for
performing efficient nearest neighbor searches) and earlier contributions from Thibaut
Lienart originally residing in
[MLJModels.jl](https://github.com/alan-turing-institute/MLJModels.jl/blob/98618d7be53f72054de284fa1796c5292d9071bb/src/NearestNeighbors.jl#L1).
NearestNeighborModels builds on Kristoffer Carlsson's
[NearestNeighbors](https://github.com/KristofferC/NearestNeighbors.jl) package(for
performing efficient nearest neighbor searches) and earlier contributions from Thibaut
Lienart originally residing in
[MLJModels.jl](https://github.com/JuliaAI/MLJModels.jl/blob/98618d7be53f72054de284fa1796c5292d9071bb/src/NearestNeighbors.jl#L1).


# Installation
Expand All @@ -20,28 +19,30 @@ as shown below.

```julia
using Pkg
Pkg.add("NearestNeighborModels")
Pkg.add("NearestNeighborModels")
```

# Usage

To use any model implemented in this package, the model must first be wrapped in an MLJ
machine alongside the required data. Users also get additional features from MLJ including
To use any model implemented in this package, the model must first be wrapped in an MLJ
machine alongside the required data. Users also get additional features from MLJ including
performance evaluation, hyper-parameter tuning, stacking etc.
The following example shows how to train a `KNNClassifier` on the crabs dataset.

```julia
using NearestNeighborModels, MLJBase
X, y = @load_crabs; # loads the crabs dataset from MLJBase
import NearestNeighborModels: KNNClassifier, Inverse
import MLJBase: @load_crabs, fit!, machine, nrows, partition, predict, predict_mode
X, y = @load_crabs; # a table and a vector
train_inds, test_inds = partition(1:nrows(X), 0.7, shuffle=false);
knnc = KNNClassifier(weights = Inverse()) # KNNClassifier instantiation
knnc_mach = machine(knnc, X, y) # wrap model and required data in an MLJ machine
fit!(knnc_mach, rows=train_inds) # train machine on a subset of the wrapped data `X`
knnc_mach = machine(knnc, X, y) # wrap model and required data in an MLJ machine
fit!(knnc_mach, rows=train_inds) # train machine on a subset of the wrapped data `X`
```
`UnivariateFinite` predictions can be obtained from the trained machine as shown below
```@meta
DocTestSetup = quote
using NearestNeighborModels, MLJBase
DocTestSetup = quote
import NearestNeighborModels: KNNClassifier, Inverse
import MLJBase: @load_crabs, fit!, machine, nrows, partition, predict, predict_mode
X, y = @load_crabs;
train_inds, test_inds = partition(1:nrows(X), 0.7, shuffle=false);
knnc = KNNClassifier(weights = Inverse())
Expand All @@ -51,7 +52,7 @@ end
```
```jldoctest ex1
julia> predict(knnc_mach, rows=test_inds)
60-element UnivariateFiniteVector{Multiclass{2}, String, UInt32, Float64}:
60-element CategoricalDistributions.UnivariateFiniteVector{ScientificTypesBase.Multiclass{2}, String, UInt32, Float64}:
UnivariateFinite{Multiclass{2}}(B=>0.315, O=>0.685)
UnivariateFinite{Multiclass{2}}(B=>1.0, O=>0.0)
UnivariateFinite{Multiclass{2}}(B=>1.0, O=>0.0)
Expand Down Expand Up @@ -114,24 +115,27 @@ function custom_kernel(dists::AbstractMatrix)
return weights
end

# Then we wrap it in a `UserDefinedKernel`
# `sort = true` because our `custom_kernel` function relies on `dists` being sorted in
# Then we wrap it in a `UserDefinedKernel` with
# `sort = true` because our `custom_kernel` function relies on `dists` being sorted in
# ascending order.
import NearestNeighborModels.UserDefinedKernel
weighting_kernel = UserDefinedKernel(func=custom_kernel, sort=true)
```
We will now train a `MultitargetKNNRegressor` that makes use of our simple custom-defined
We will now train a `MultitargetKNNRegressor` that makes use of our simple custom-defined
`weighting_kernel` for prediction.
```julia
using NearestNeighborModels, MLJBase
import NearestNeighborModels: MultitargetKNNRegressor, Inverse
import MLJBase: @load_crabs, fit!, machine, nrows, partition, predict, predict_mode
import MLJBase
using StableRNGs #for reproducibility of this example

n = 50
p = 5
l = 2
rng = StableRNG(100)
# `table` converts an `AbstractMatrix` into a `Tables.jl` compactible table
X = table(randn(rng, (n, p))) # feature table
Y = table(randn(rng, (n, l))) # target table
X = MLJBase.table(randn(rng, (n, p))) # feature table
Y = MLJBase.table(randn(rng, (n, l))) # target table

train_inds, test_inds = partition(1:nrows(X), 0.8, shuffle=false);
multi_knnr = MultitargetKNNRegressor(weights=weighting_kernel)
Expand All @@ -141,7 +145,11 @@ fit!(multi_knnr_mach, rows=train_inds) # train machine on a subset of the wrappe
And of course predicting with the test-dataset gives:
```@meta
DocTestSetup = quote
using NearestNeighborModels, MLJBase, StableRNGs
import NearestNeighborModels: MultitargetKNNRegressor, Inverse
import NearestNeighborModels.UserDefinedKernel
import MLJBase: @load_crabs, fit!, machine, nrows, partition, predict, predict_mode
import MLJBase
using StableRNGs
function custom_kernel(dists::AbstractMatrix)
weights = similar(Array{Float16}, size(dists))
weights[:, 1] .= 2.0
Expand All @@ -153,8 +161,8 @@ DocTestSetup = quote
p = 5
l = 2
rng = StableRNG(100)
X = table(randn(rng, (n, p))) # feature table
Y = table(randn(rng, (n, l))) # target table
X = MLJBase.table(randn(rng, (n, p))) # feature table
Y = MLJBase.table(randn(rng, (n, l))) # target table
train_inds, test_inds = partition(1:nrows(X), 0.8, shuffle=false);
multi_knnr = MultitargetKNNRegressor(weights=weighting_kernel)
multi_knnr_mach = machine(multi_knnr, X, Y) #wrap model and required data in an MLJ machine
Expand Down Expand Up @@ -183,5 +191,5 @@ julia> MLJBase.matrix(table_predictions)
```@meta
DocTestSetup = nothing
```
see [MLJ docs](https://alan-turing-institute.github.io/MLJ.jl/dev/) for help on additional
see [MLJ docs](https://JuliaAI.github.io/MLJ.jl/stable/) for help on additional
features such as hyper-parameter tuning, performance evaluation, stacking etc.
Loading