Skip to content

Commit 60c537f

Browse files
committed
warns if user passes in params argument to lightgbm model
1 parent 6f1b3fe commit 60c537f

File tree

4 files changed

+35
-24
lines changed

4 files changed

+35
-24
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# bonsai (development version)
22

3+
* The lightgbm engine now warns that arguments passed to `params` argument in `set_engine()` are ignored (#110).
4+
35
* Automatic handling of `num_classes` argument when specifying a multiclass classification objective for the lightgbm engine (#109).
46

57
* Increased the minimum R version to R 4.1.

R/lightgbm.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ train_lightgbm <- function(
100100
...
101101
)
102102

103+
if (!is.null(args$params) && is.list(args$params)) {
104+
cli::cli_warn(c(
105+
"Arguments passed in through {.arg params} as a list will be ignored.",
106+
"Instead pass the arguments directly to the {.code ...}."
107+
))
108+
}
109+
103110
args <- process_bagging(args)
104111
args <- process_objective_function(args, y)
105112

tests/testthat/_snaps/lightgbm.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@
4444
Error in `fit()`:
4545
! `feature_fraction_bynode` must be a number, not a call.
4646

47+
# lightgbm warns if user uses `param` argument in set_engine()
48+
49+
Code
50+
fit(mod_spec, mpg ~ ., mtcars)
51+
Condition
52+
Warning:
53+
Arguments passed in through `params` as a list will be ignored.
54+
Instead pass the arguments directly to the `...`.
55+
Output
56+
parsnip model object
57+
58+
LightGBM Model (1 tree)
59+
Objective: regression
60+
Fitted to dataset with 10 columns
61+
4762
# training wrapper warns on protected arguments
4863

4964
Code

tests/testthat/test-lightgbm.R

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -348,30 +348,6 @@ test_that("bonsai correctly determines objective when label is a factor", {
348348
expect_equal(bst$params$num_class, 3)
349349
})
350350

351-
test_that("bonsai correctly determines num_classes when objective is set", {
352-
skip_if_not_installed("lightgbm")
353-
skip_if_not_installed("modeldata")
354-
355-
suppressPackageStartupMessages({
356-
library(lightgbm)
357-
library(dplyr)
358-
})
359-
360-
data("penguins", package = "modeldata")
361-
penguins <- penguins[complete.cases(penguins), ]
362-
363-
expect_no_error({
364-
bst <- train_lightgbm(
365-
x = penguins[, c("bill_length_mm", "bill_depth_mm")],
366-
y = penguins[["species"]],
367-
num_iterations = 5,
368-
verbose = -1L,
369-
objective = "multiclassova"
370-
)
371-
})
372-
expect_identical(bst$params$objective, "multiclassova")
373-
expect_identical(bst$params$num_class, 3L)
374-
})
375351

376352
test_that("bonsai handles mtry vs mtry_prop gracefully", {
377353
skip_if_not_installed("modeldata")
@@ -535,6 +511,17 @@ test_that("tuning mtry vs mtry_prop", {
535511
)
536512
})
537513

514+
test_that("lightgbm warns if user uses `param` argument in set_engine()", {
515+
mod_spec <- boost_tree() |>
516+
set_engine("lightgbm", params = list(objective = "multiclassova")) |>
517+
set_mode("regression")
518+
519+
expect_snapshot(
520+
mod_spec |>
521+
fit(mpg ~ ., mtcars)
522+
)
523+
})
524+
538525
test_that("training wrapper warns on protected arguments", {
539526
skip_if_not_installed("lightgbm")
540527
skip_if_not_installed("modeldata")

0 commit comments

Comments
 (0)