Skip to content

Commit 7fbee7a

Browse files
authored
[R] Add migration guide for new xgboost (dmlc#11197)
1 parent 178b49b commit 7fbee7a

File tree

6 files changed

+91
-8
lines changed

6 files changed

+91
-8
lines changed

R-package/R/xgb.train.R

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
#' eXtreme Gradient Boosting Training
2-
#'
3-
#' `xgb.train()` is an advanced interface for training an xgboost model.
4-
#' The [xgboost()] function is a simpler wrapper for `xgb.train()`.
5-
#'
1+
#' @title Fit XGBoost Model
2+
#' @description Fits an XGBoost model to given data in DMatrix format (e.g. as produced by [xgb.DMatrix()]).
3+
#' See the tutorial [Introduction to Boosted Trees](https://xgboost.readthedocs.io/en/stable/tutorials/model.html)
4+
#' for a longer explanation of what XGBoost does, and the rest of the
5+
#' [XGBoost Tutorials](https://xgboost.readthedocs.io/en/latest/tutorials/index.html) for further
6+
#' explanations XGBoost's features and usage.
7+
#'
8+
#' Compared to function [xgboost()] which is a user-friendly function targeted towards interactive
9+
#' usage, ``xgb.train`` is a lower-level interface which allows finer-grained control and exposes
10+
#' further functionalities offered by the core library (such as learning-to-rank objectives), but
11+
#' which works exclusively with XGBoost's own data format ("DMatrices") instead of with regular R
12+
#' objects.
13+
#'
14+
#' The syntax of this function closely mimics the same function from the Python package for XGBoost,
15+
#' and is recommended to use for package developers over `xgboost()` as it will provide a more
16+
#' stable interface (with fewer breaking changes) and lower overhead from data validations.
17+
#'
18+
#' See also the [migration guide](https://xgboost.readthedocs.io/en/latest/R-package/migration_guide.html)
19+
#' if coming from a previous version of XGBoost in the 1.x series.
620
#' @param params List of XGBoost parameters which control the model building process.
721
#' See the [online documentation](http://xgboost.readthedocs.io/en/latest/parameter.html)
822
#' and the documentation for [xgb.params()] for details.

R-package/R/xgboost.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,9 @@ check.early.stopping.rounds <- function(early_stopping_rounds, eval_set) {
845845
#' language bindings of XGBoost and which exposes additional functionalities such as training on
846846
#' external memory data and learning-to-rank objectives.
847847
#'
848+
#' See also the [migration guide](https://xgboost.readthedocs.io/en/latest/R-package/migration_guide.html)
849+
#' if coming from a previous version of XGBoost in the 1.x series.
850+
#'
848851
#' By default, most of the parameters here have a value of `NULL`, which signals XGBoost to use its
849852
#' default value. Default values are automatically determined by the XGBoost core library, and are
850853
#' subject to change over XGBoost library versions. Some of them might differ according to the

R-package/man/xgb.train.Rd

Lines changed: 19 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

R-package/man/xgboost.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/R-package/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ Other topics
2323
:maxdepth: 2
2424
:titlesonly:
2525

26+
Migrating code from previous XGBoost versions <migration_guide>
2627
Handling of indexable elements <index_base>
2728
Developer guide: parameters from core library <adding_parameters>

doc/R-package/migration_guide.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.. _migation_guide:
2+
3+
Migrating code from previous XGBoost versions
4+
=============================================
5+
6+
XGBoost's R language bindings had large breaking changes between versions 1.x and 2.x. R code that was working with past XGBoost versions might require modifications to work with the newer versions. This guide outlines the main differences:
7+
8+
- Function ``xgboost()``:
9+
- Previously, this function accepted arguments 'data' and 'label', which have now been renamed to 'x' and 'y', in line with other popular R packages.
10+
- Previously, the 'data' argument which is now 'x' had to be passed as either an XGBoost 'DMatrix' or as an R matrix. Now the argument allows R data.frames, matrices, and sparse matrices from the 'Matrix' package, but not XGBoost's own DMatrices. Categorical columns will be deduced from the types of the columns when passing a data.frame.
11+
- Previously, the 'label' data which is now 'y' had to be passed to ``xgboost()`` encoded in the format used by the XGBoost core library - meaning: binary variables had to be encoded to 0/1, bounds for survival objectives had to be passed as different arguments, among others. In the newest versions, 'y' now doesn't need to be manually encoded beforehand: it should be passed as an R object of the corresponding class as regression functions from base R and core R packages for the corresponding XGBoost objective - e.g. classification problems should be passed a ``factor``, survival problems a ``Surv``, regression problems a numeric vector, and so on. Learning-to-rank is not supported by ``xgboost()``, but is supported by ``xgb.train``.
12+
- Previously, ``xgboost()`` accepted both a ``params`` argument and named arguments under ``...``. Now all training parameters should be passed as named arguments, and all accepted parameters are explicit function arguments with in-package documentation. Some parameters are not allowed as they are determined automatically from the rest of the data, such as the number of classes for multi-classes classification which is determined automatically from 'y'. As well, parameters that have synonyms or which are accepted under different possible arguments (e.g. "eta" and "learning_rate") now accept only their more descriptive form (so "eta" is not accepted, but "learning_rate" is).
13+
- Models produced by this function ``xgboost()`` are now returned with a different class "xgboost", which is a subclass of "xgb.Booster" but with more metadata and a ``predict`` method with different defaults.
14+
- This function ``xgboost()`` is now meant for interactive usage only. For package developers who wish to incorporate the XGBoost package, it is highly recommended to use ``xgb.train`` instead, which is a lower-level function that closely mimics the same function from the Python package and is meant to be less subject to breaking changes.
15+
16+
- Function ``xgb.train()``:
17+
- Previously, ``xgb.train()`` allowed arguments under both a "params" list and as named arguments under ``...``. Now, all training arguments should be passed under ``params``.
18+
- In order to make it easier to discover and pass parameters, there is now a function ``xgb.params`` which can generate a list to pass to the ``params`` argument. ``xgb.params`` is simply a function with named arguments that lists everything accepted by ``xgb.train`` and offers in-package documentation for all of the arguments, returning a simple named list.
19+
- Arguments that are meant to be consumed by the DMatrix constructor must be passed directly to ``xgb.DMatrix`` instead (e.g. argument for categorical features or for feature names).
20+
- Some arguments have been renamed (e.g. previous 'watchlist' is now 'evals', in line with the Python package).
21+
- The format of the callbacks to pass to ``xgb.train`` has largely been re-written. See the documentation of ``xgb.Callback`` for details.
22+
23+
- Function ``xgb.DMatrix()``:
24+
- This function now accepts 'data.frame' inputs and determines which features are categorical from their types - anything with type 'factor' or 'character' will be considered as categorical. Note that when passing data to the 'predict' method, the 'factor' variables must have the same encoding (i.e. same levels) as XGBoost will not re-encode them for you.
25+
- Whereas previously some arguments such as the type of the features had to be passed as a list under argument 'info', they are all now direct function arguments to 'xgb.DMatrix' instead.
26+
- There are now other varieties of DMatrix constructors that might better fit some uses cases -for example, there is 'xgb.QuantileDMatrix' which will quantize the features straight away (therefore avoiding redundant copies and reducing memory consumption) for the histogram method in XGBoost (but note that quantized DMatrices are not usable with the 'exact' sorted-indices method).
27+
- Note that data for 'label' still needs to be encoded in the format consumed by the core XGBoost library - e.g. classification objectives should receive 'label' data encoded as zeros and ones.
28+
- Creation of DMatrices from text files has been deprecated.
29+
30+
- Function ``xgb.cv()``:
31+
- While previously this function accepted 'data' and 'label' similarly to the old ``xgboost()``, now it accepts only ``xgb.DMatrix`` objects.
32+
- The function's scope has been expanded to support more functionalities offered by XGBoost, such as survival and learning-to-rank objectives.
33+
34+
- Method ``predict``:
35+
- There are now two predict methods with different default arguments according to whether the model was produced through ``xgboost()`` or through ``xgb.train()``. Function ``xgboost()`` is more geared towards interactive usage, and thus the defaults for the 'predict' method on such objects (class "xgboost") by default will perform more data validations such as checking that column names match and reordering them otherwise. The 'predict' method for models created through ``xgb.train()`` (class "xgb.Booster") has the same defaults as before, so for example it will not reorder columns to match names under the default behavior.
36+
- The 'predict' method for objects of class "xgboost" (produced by ``xgboost()``, not by ``xgb.train()``) now can control the types of predictions to make through an argument ``type``, similarly as the 'predict' methods in the 'stats' module of base R - e.g. one can now do ``predict(model, type="class")``; while the 'predict' method for "xgb.Booster" objects (produced by ``xgb.train()``), just like before, controls those through separate arguments such as ``outputmargin``.
37+
- Previously, predictions using a subset of the trees were using base-0 indexing and range syntax mimicing Python's ranges, whereas now they use base-1 indexing as is common in R, and their behavior for ranges matches that of R's ``seq`` function. Note that the syntax for "use all trees" and "use trees up to early-stopped criteria" have changed (see documentation for details).
38+
39+
- Booster objects:
40+
- The structure of these objects has been modified - now they are represented as a simple R "ALTLIST" (a special kind of 'list' object) with additional attributes.
41+
- These objects now cannot be modified by adding more fields to them, but metadata for them can be added as attributes.
42+
- The objects distinguish between two types of attributes:
43+
44+
- R-side attributes (which can be accessed and modified through R function ``attributes(model)`` and ``attributes(model)$field <- val``), which allow arbitrary objects. Many attributes are automatically added by the model building functions, such as evaluation logs (a ``data.table`` with metrics calculated per iteration), which previously were model fields.
45+
- C-level attributes, which allow only JSON-compliant data and which can be accessed and set through function ``xgb.attributes(model)``. These C-level attributes are shareable through serialized models in different XGBoost interfaces, while the R-level ones are specific to the R interface. Some attributes that are standard among language bindings of XGBoost, such as the best interation, are kept as C attributes.
46+
- Previously, models that were just de-serialized from an on-disk format required calling method 'xgb.Booster.complete' on them to finish the full de-serialization process before being usable, or would otherwise call this method on their own automatically automatically at the first call to 'predict'. Serialization is now handled more gracefully, and there are no additional functions/methods involved - i.e. if one saves a model to disk with ``saveRDS()`` and then reads it back with ``readRDS()``, the model will be fully loaded straight away, without needing to call additional methods on it.

0 commit comments

Comments
 (0)