Skip to content

In xgboost, ntreelimit has been deprecated in favour of iteration_range #655

@tiagomaie

Description

@tiagomaie

parsnip/R/boost_tree.R

Lines 484 to 486 in 0af9c86

xgb_by_tree <- function(tree, object, new_data, type, ...) {
pred <- xgb_pred(object$fit, newdata = new_data, ntreelimit = tree)

L485 should instead be:
pred <- xgb_pred(object$fit, newdata = new_data, iterationrange = c(1, tree + 1), ntreelimit = NULL)

ntreelimit has been deprecated.
It is tree + 1 as in the xgboost docs they mention that iteration_range should be a half-open set.
This should fix the warnings given when running tune::tune_grid for example, see below.

library(parsnip)
library(dplyr)

# using iris dataset

# defining train/test split
train_ids <- 1:(nrow(iris) - 50)
test_ids <- (nrow(iris) - 49):nrow(iris)

# defining recipe
d_recipe <- recipes::recipe(
  Species ~ .,
  data = iris[train_ids, ] %>% dplyr::mutate(Species = factor(Species))
)

# defining model
g_model <-
  parsnip::boost_tree(trees = tune::tune()) %>%
  parsnip::set_engine("xgboost", objective = "binary:logistic") %>%
  parsnip::set_mode("classification")


# defining workflow
wf <- workflows::workflow() %>%
  workflows::add_recipe(d_recipe) %>%
  workflows::add_model(g_model)

# tuning params
res <- wf %>% tune::tune_grid(
    resamples=rsample::vfold_cv(
      recipes::bake(d_recipe %>% recipes::prep(), new_data = NULL)
    )
  )
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip
#> [18:38:53] WARNING: amalgamation/../src/c_api/c_api.cc:718: `ntree_limit` is deprecated, use `iteration_range` instead.
#> [18:38:53] WARNING: amalgamation/../src/c_api/c_api.cc:718: `ntree_limit` is deprecated, use `iteration_range` instead.
#> [18:38:53] WARNING: amalgamation/../src/c_api/c_api.cc:718: `ntree_limit` is deprecated, use `iteration_range` instead.
...
#> [18:38:59] WARNING: amalgamation/../src/c_api/c_api.cc:718: `ntree_limit` is deprecated, use `iteration_range` instead.
#> [18:38:59] WARNING: amalgamation/../src/c_api/c_api.cc:718: `ntree_limit` is deprecated, use `iteration_range` instead.
#> [18:38:59] WARNING: amalgamation/../src/c_api/c_api.cc:718: `ntree_limit` is deprecated, use `iteration_range` instead.

Created on 2022-02-09 by the reprex package (v2.0.1)

I should note that this potential fix doesn't account for functionality that you can have with iterationrange but that it is currently not possible anyway with ntreelimit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions