Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R-package] Rename weight -> weights #4975

Merged
merged 5 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 4 additions & 3 deletions R-package/R/lightgbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ NULL
#' @description Simple interface for training a LightGBM model.
#' @inheritParams lgb_shared_params
#' @param label Vector of labels, used if \code{data} is not an \code{\link{lgb.Dataset}}
#' @param weight vector of response values. If not NULL, will set to dataset
#' @param weights Sample / observation weights for rows in the input data. If `NULL`, will assume that all
jameslamb marked this conversation as resolved.
Show resolved Hide resolved
#' observations / rows have the same importance / weight.
#' @param objective Optimization objective (e.g. `"regression"`, `"binary"`, etc.).
#' For a list of accepted objectives, see
#' \href{https://lightgbm.readthedocs.io/en/latest/Parameters.html#objective}{
Expand All @@ -117,7 +118,7 @@ NULL
#' @export
lightgbm <- function(data,
label = NULL,
weight = NULL,
weights = NULL,
params = list(),
nrounds = 100L,
verbose = 1L,
Expand All @@ -140,7 +141,7 @@ lightgbm <- function(data,

# Check whether data is lgb.Dataset, if not then create lgb.Dataset manually
if (!lgb.is.Dataset(x = dtrain)) {
dtrain <- lgb.Dataset(data = data, label = label, weight = weight, init_score = init_score)
dtrain <- lgb.Dataset(data = data, label = label, weight = weights, init_score = init_score)
}

train_args <- list(
Expand Down
5 changes: 3 additions & 2 deletions R-package/man/lightgbm.Rd

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

26 changes: 26 additions & 0 deletions R-package/tests/testthat/test_basic.R
Original file line number Diff line number Diff line change
Expand Up @@ -2927,3 +2927,29 @@ test_that("lightgbm() defaults to 'regression' objective if objective not otherw
expect_true(any(model_txt_lines == "objective=regression"))
expect_false(any(model_txt_lines == "objective=regression_l1"))
})

test_that("lightgbm() accepts 'weight' and 'weights'", {
data(mtcars)
X <- as.matrix(mtcars[, -1L])
y <- as.numeric(mtcars[, 1L])
w <- rep(1.0, nrow(X))
model <- lightgbm(
X
, y
, weights = w
, obj = "regression"
, nrounds = 5L
, verbose = -1L
)

# Avoid a bad CRAN check due to partial argument matches
lgb_args <- list(
X
, y
, weight = w
, obj = "regression"
, nrounds = 5L
, verbose = -1L
)
model <- do.call(lightgbm, lgb_args)
})