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
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# funcml 0.9.11

* `kknn` treated `k` as the largest k of a leave-one-out search, so the
fitted model could use fewer neighbours than asked. `k` is now the exact
number of neighbours. The kernel, fixed at `"optimal"` before, is now the
spec entry `kernel` (default `"optimal"`).
* `e1071_svm` gains the spec entry `epsilon` (default 0.1, the e1071
default), so support vector regression can be tuned on it.
* New `splits()` draws the train/test row indices of any resampling scheme
(`cv()`, `holdout()`, `groupcv()`, `timecv()`) outside `evaluate()`. For a
holdout split the indices are returned directly as `$train` and `$test`.
Expand Down
8 changes: 8 additions & 0 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
#' meta-features are stored at `$state$meta_x`. `superlearner` works the same
#' way with a `resampling` object instead of `folds`.
#'
#' `kknn` takes `spec = list(k, distance, kernel)`: `k` is the exact number
#' of neighbours, `distance` the Minkowski power and `kernel` any kernel
#' accepted by [kknn::kknn()] (default `"optimal"`; for example
#' `"rectangular"` gives an unweighted k-nearest-neighbour vote or mean).
#' `e1071_svm` takes `spec = list(cost, gamma, kernel, epsilon)`; `epsilon`
#' (default 0.1) is the width of the insensitive tube in support vector
#' regression and is ignored for classification.
#'
#' @param formula Model formula.
#' @param data Data frame.
#' @param model Learner id (see `learners()`).
Expand Down
11 changes: 7 additions & 4 deletions R/registry.R
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ build_registry <- function() {
e1071_svm = list(
package = "e1071",
tasks = c("regression", "classification"),
defaults = list(cost = 1, gamma = NULL, kernel = "radial"),
defaults = list(cost = 1, gamma = NULL, kernel = "radial", epsilon = 0.1),
supports = list(prob = TRUE, multiclass = TRUE, importance = FALSE),
fit_xy = function(X, y, spec, task, ...) {
assert_package("e1071", "e1071_svm")
Expand All @@ -574,6 +574,7 @@ build_registry <- function() {
fit <- e1071::svm(
y ~ ., data = df,
cost = spec$cost, gamma = gamma, kernel = spec$kernel,
epsilon = spec$epsilon,
type = if (task == "regression") "eps-regression" else "C-classification",
probability = task == "classification"
)
Expand Down Expand Up @@ -719,16 +720,18 @@ build_registry <- function() {
kknn = list(
package = "kknn",
tasks = c("regression", "classification"),
defaults = list(k = 7, distance = 2),
defaults = list(k = 7, distance = 2, kernel = "optimal"),
supports = list(prob = TRUE, multiclass = TRUE, importance = FALSE),
fit_xy = function(X, y, spec, task, ...) {
assert_package("kknn", "kknn")
df <- data.frame(y = y, X)
# `ks` fixes the number of neighbours at `k`; `kmax` would let the
# leave-one-out search pick any k up to `k`.
fit <- kknn::train.kknn(
y ~ ., data = df,
kmax = spec$k,
ks = spec$k,
distance = spec$distance,
kernel = "optimal"
kernel = spec$kernel
)
list(state = fit)
},
Expand Down
8 changes: 8 additions & 0 deletions man/fit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions tests/testthat/test-kknn-svm-spec.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
test_that("kknn uses exactly k neighbours and the requested kernel", {
skip_if_not_installed("kknn")
d <- mtcars
for (kern in c("rectangular", "triangular")) {
f <- fit(mpg ~ wt + hp, d[1:24, ], model = "kknn",
spec = list(k = 5, distance = 2, kernel = kern))
ref <- kknn::kknn(mpg ~ wt + hp, train = d[1:24, ], test = d[25:32, ],
k = 5, distance = 2, kernel = kern)
expect_equal(as.numeric(predict(f, d[25:32, ])), as.numeric(ref$fitted.values))
expect_equal(enginemodel(f)$best.parameters$k, 5)
}
# rectangular kernel = plain mean of the k nearest outcomes (scaled X)
f <- fit(mpg ~ wt + hp, d[1:24, ], model = "kknn",
spec = list(k = 3, kernel = "rectangular"))
X <- scale(d[1:24, c("wt", "hp")])
xn <- scale(d[25, c("wt", "hp")], center = attr(X, "scaled:center"),
scale = attr(X, "scaled:scale"))
dist <- sqrt(colSums((t(X) - as.numeric(xn))^2))
expect_equal(as.numeric(predict(f, d[25, ])), mean(d$mpg[1:24][order(dist)[1:3]]))
})

test_that("e1071_svm passes epsilon to support vector regression", {
skip_if_not_installed("e1071")
d <- mtcars
for (eps in c(0.01, 1)) {
f <- fit(mpg ~ wt + hp, d, model = "e1071_svm", spec = list(epsilon = eps))
ref <- e1071::svm(mpg ~ wt + hp, data = d, type = "eps-regression",
kernel = "radial", cost = 1, gamma = 1 / 2, epsilon = eps)
expect_equal(enginemodel(f)$epsilon, eps)
expect_equal(as.numeric(predict(f, d)), as.numeric(predict(ref, d)), tolerance = 1e-6)
}
f_lo <- fit(mpg ~ wt + hp, d, model = "e1071_svm", spec = list(epsilon = 0.01))
f_hi <- fit(mpg ~ wt + hp, d, model = "e1071_svm", spec = list(epsilon = 0.5))
expect_gt(enginemodel(f_lo)$tot.nSV, enginemodel(f_hi)$tot.nSV)
})
Loading