Skip to content
Open
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

* `min_grid()` now returns rows in the order of the non-submodel parameters rather than putting rows with submodels first (#987).

* `select_by_pct_loss()` no longer emits a spurious warning when multiple tuning parameter combinations tie for the best loss (#1195)

# tune 2.1.0

* Model tuning has been enabled for quantile regression models. (#1125)
Expand Down
3 changes: 2 additions & 1 deletion R/select_best.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ select_by_pct_loss.tune_results <- function(

# discard models more complex than the best and
# remove models with greater increase in loss than the limit
best_index <- which(summary_res$.loss == 0)
# (use max() since multiple configurations can tie for the best loss)
best_index <- max(which(summary_res$.loss == 0))
summary_res |>
dplyr::slice(1:best_index) |>
dplyr::filter(.loss < limit) |>
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/_snaps/select_best.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@
Error in `select_by_pct_loss()`:
! Could not sort results by `weight_funk` and `desc(K)`.

# select_by_pct_loss() handles ties for the best loss (#1195)

Code
res <- select_by_pct_loss(knn_results, metric = "accuracy", limit = 5, K)

# show_best with survival models

Code
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-select_best.R
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,28 @@ test_that("percent loss", {
)
})

test_that("select_by_pct_loss() handles ties for the best loss (#1195)", {
skip_if_not_installed("dials", minimum_version = "1.4.0")

knn_results <- readRDS(test_path("data", "knn_results.rds"))

tied_metrics <- tibble::tibble(
K = c(1, 4, 12, 14, 17),
weight_func = "rectangular",
exponent = 1,
.config = paste0("cfg", 1:5),
mean = c(0.70, 0.75, 0.90, 0.90, 0.80)
)
local_mocked_bindings(
.filter_perf_metrics = function(...) tied_metrics
)

expect_snapshot(
res <- select_by_pct_loss(knn_results, metric = "accuracy", limit = 5, K)
)
expect_equal(res$K, 12)
})

test_that("select_by_* can handle metrics with direction == 'zero'", {
skip_on_cran()
skip_if_not_installed("kknn")
Expand Down
Loading